If you’ve ever tried to connect an LCD display to an Arduino, you might have noticed that it consumes a lot of pins on the Arduino. Even in 4-bit mode, the Arduino still requires a total of seven connections – which is half of the Arduino’s available digital I/O pins. Show
The solution is to use an I2C LCD display. It consumes only two I/O pins that are not even part of the set of digital I/O pins and can be shared with other I2C devices as well. Hardware OverviewA typical I2C LCD display consists of an HD44780 based character LCD display and an I2C LCD adapter. Let’s get to know them one by one. Character LCD DisplayTrue to their name, these LCDs are ideal for displaying only text/characters. A 16×2 character LCD, for example, has an LED backlight and can display 32 ASCII characters in two rows of 16 characters each. If you look closely you can see tiny rectangles for each character on the display and the pixels that make up a character. Each of these rectangles is a grid of 5×8 pixels. Check out our comprehensive tutorial for more information on character LCD displays. I2C LCD AdapterAt the heart of the adapter is an 8-bit I/O expander chip – PCF8574. This chip converts the I2C data from an Arduino into the parallel data required for an LCD display. The board also comes with a small trimpot to make fine adjustments to the display’s contrast. In addition, there is a jumper on the board that supplies power to the backlight. To control the intensity of the backlight, you can remove the jumper and apply external voltage to the header pin that is marked ‘LED’. I2C Address of LCDIf you are using multiple devices on the same I2C bus, you may need to set a different I2C address for the LCD adapter so that it does not conflict with another I2C device. To do this, the adapter has three solder jumpers (A0, A1 and A2) or solder pads. Each of these is used to hardcode the address. If a jumper is shorted with a blob of solder, it sets the address. An important point here is that several companies manufacture the same PCF8574 chip, Texas Instruments and NXP Semiconductors, to name a few. And the I2C address of your LCD depends on the chip manufacturer. If your LCD has Texas Instruments’ PCF8574 chip:According to the Texas Instruments’ datasheet, the three address selection bits (A0, A1 and A2) are placed at the end of the 7-bit I2C address register. Since there are 3 address inputs, which can take 2 states, either HIGH/LOW, we can therefore create 8 (23) different combinations (addresses). By default, all 3 address inputs are pulled HIGH using onboard pullups, giving the PCF8574 a default I2C address of 0100111Binary or 0x27Hex. By shorting the solder jumpers, the address inputs are puled LOW. If you were to short all three jumpers, the address would be 0x20. The range of all possible addresses spans from 0x20 to 0x27. Please see the illustration below. If your LCD has NXP’s PCF8574 chip:According to the NXP Semiconductors’ datasheet, the three address selection bits (A0, A1 and A2) are also placed at the end of the 7-bit I2C address register. But the other bits in the address register are different. Since there are 3 address inputs, which can take 2 states, either HIGH/LOW, we can therefore create 8 (23) different combinations (addresses). By default, all 3 address inputs are pulled HIGH using onboard pullups, giving the PCF8574 a default I2C address of 0111111Binary or 0x3FHex. By shorting the solder jumpers, the address inputs are puled LOW. If you were to short all three jumpers, the address would be 0x38. The range of all possible addresses spans from 0x38 to 0x3F. Please see the illustration below. So your LCD probably has a default I2C address 0x27Hex or 0x3FHex. However it is recommended that you find out the actual I2C address of the LCD before using it. Luckily there’s an easy way to do this. We will see that later in the tutorial. I2C LCD display PinoutAn I2C LCD has only 4 pins that connect it to the outside world. The connections are as follows: GND is a ground pin. Connect it to the ground of the Arduino. VCC supplies power to the module and LCD. Connect it to the Arduino’s 5V output or an external 5V power supply. SDA is the I2C data pin. Connect it to the Arduino’s I2C data pin. SCL is the I2C clock pin. Connect it to the Arduino’s I2C clock pin. Hooking up an Arduino Uno to an I2C LCD displayConnecting an I2C LCD is much easier than connecting a standard LCD. You only need to connect 4 pins instead of 12. Start by connecting the VCC pin to the 5V output on the Arduino and GND to ground. Now we are left with the pins which are used for I2C communication. Note that each Arduino board has different I2C pins that must be connected accordingly. On Arduino boards with the R3 layout, the SDA (data line) and SCL (clock line) are on the pin headers close to the AREF pin. They are also known as A5 (SCL) and A4 (SDA). If you are using a different Arduino board, please refer to the table below. The following diagram shows you how to wire everything up. Adjusting The LCD ContrastAfter wiring up the LCD you’ll need to adjust the contrast of the display. On the I2C module you will find a potentiometer that you can rotate with a small screwdriver. Plug in the Arduino’s USB connector to power the LCD. You will see the backlight lit up. Now as you turn the knob on the potentiometer, you will start to see the first row of rectangles. If that happens, Congratulations! Your LCD is working fine. Once this is done, we can start programming the LCD. Library InstallationTo drive an I2C LCD you must first install a library called LiquidCrystal_I2C. This library is an enhanced version of the LiquidCrystal library that comes with your Arduino IDE. To install the library navigate to Sketch > Include Libraries > Manage Libraries… Wait for Library Manager to download the library index and update the list of installed libraries.
Filter your search by typing ‘liquidcrystal‘. There should be some entries. Look for the LiquidCrystal I2C library by Frank de Brabander. Click on that entry, and then select Install. Determining the I2C AddressThe I2C address of your LCD depends on the manufacturer, as mentioned earlier. If your LCD has a Texas Instruments’ PCF8574 chip, its default I2C address is 0x27Hex. If your LCD has NXP Semiconductors’ PCF8574 chip, its default I2C address is 0x3FHex. So your LCD probably has I2C address 0x27Hex or 0x3FHex. However it is recommended that you find out the actual I2C address of the LCD before using it. Luckily there’s an easy way to do this, thanks to the Nick Gammon. Nick wrote a simple I2C scanner sketch that scans your I2C bus and returns the address of each I2C device it finds.
Load this sketch into your Arduino and then open your Serial Monitor. You will see the I2C address of your I2C LCD display. Please note this address. You will need it in later examples. Basic Arduino Sketch – Hello WorldThe following test sketch will print ‘Hello World!’ on the first line of the LCD and ‘LCD Tutorial’ on the second line. But, before you proceed to upload the sketch, you need to make a small change to make it work for you. You must pass the I2C address of your LCD and the dimensions of the display to the constructor of the LiquidCrystal_I2C class. If you are using a 16×2 character LCD, pass the 16 and 2; If you’re using a 20×4 LCD, pass 20 and 4. You got the point!
Once you are done go ahead and give the sketch a try.
If all goes well, you should see something like this on the display. Code Explanation:The sketch begins by including the LiquidCrystal_I2C library.
First of all an object of LiquidCrystal_I2C class is created. This
object takes three parameters
Once you have declared a LiquidCrystal_I2C object, you can access the object methods specific to the LCD. In ‘setup’ we call three functions. The first function is
After that we set the cursor position to the third column of the first row by calling the function Next, the string ‘Hello World!’ is printed by calling the
Similarly the next two lines of code set the cursor position to the third column of the second row, and print ‘LCD Tutorial’ on the LCD.
Other useful functions of the LibraryThere are some useful functions you can use with
Create and Display Custom CharactersIf you find the characters on the display dull and boring, you can create your own custom characters (glyphs) and symbols for your LCD. They are extremely useful when you want to display a character that is not part of the standard ASCII character set. As discussed earlier in this tutorial a character is made up of a 5×8 pixel matrix, so you need to define your custom character within that matrix. You can use the createChar() function to define a character. To use All these user defined characters are stored in the CGRAM of the LCD. CGROM and CGRAM All Hitachi HD44780 controller based LCDs have two types of memory – CGROM and CGRAM (Character Generator ROM and RAM). CGROM is non-volatile memory and cannot be modified whereas CGRAM is volatile memory and can be modified at any time. CGROM is used to store all permanent fonts that are displayed using their ASCII codes. For example, if we send 0x41 to the LCD, the letter ‘A’ will be printed on the display. CGRAM is another memory used to store user defined characters. This RAM is limited to 64 bytes. For a 5×8 pixel based LCD, only 8 user-defined characters can be stored in CGRAM. And for 5×10 pixel based LCD only 4 user-defined characters can be stored. Custom Character GeneratorCreating custom characters has never been easier! We have created a small application called Custom Character Generator. Can you see the blue grid below? You can click on any 5×8 pixel to set/clear that particular pixel. And as you click, the code for the character is generated next to the grid. This code can be used directly in your Arduino sketch.
Your imagination is limitless. The only limitation is that the LiquidCrystal library only supports eight custom characters. But don’t be discouraged, look at the bright side, at least we have eight characters. Arduino Example CodeThe following sketch shows how you can create custom characters and print them on an LCD.
You will see the following output on the LCD: Code Explanation:After the library is included and the LCD object is created, custom character arrays are defined. The array consists of 8 bytes, each byte representing a row of a 5×8 LED matrix. In this sketch, eight custom characters have been created. Let’s examine the
In setup, a custom character is created using the
Next in the loop, to display the custom character we simply use the |