Driving 1602 LCD with PCF8574 / PCF8574A
Last updated
Last updated
LCDs are widely used in various applications for displaying feedback to users. One of the most commonly accessible display is the Hitachi HD44780 1602 LCD display. In this tutorial, we'll show how to connect the LinkIt 7697 with a 1602 I2C LCD display module (with PCF8574A built-it) and give examples about how to use the library to drive it.
Based on the work of Francisco Malpartida, we removed unused files and ported it to fit the LinkIt 7697 Arduino BSP. You can download the library and follow the instructions in the "Install Drivers" section to install it into the Arduino development environment. After the installation is done, you can see there is an example under the File / Examples / LiquidCrystal_I2C / HelloLinkIt7697 sub-menu. Before going into details of the HelloLinkIt7697 example, let's first look at the hardware setup and usages of the library.
Four pins are needed from the LinkIt 7697 to connect to the LCD module:
5V
GND
I2C Clock (P8)
I2C Data (P9)
and they are wiring to:
VCC
GND
SCL
SDA
pins of the module, respectively. The figures for the connection are shown below:
[Back view of the LCD module and pins for the connection]
After the wiring is done, you can execute the HelloLinkIt7697 example mentioned above to see how the display works as this video:
When running this example, you might encounter issues and not see expected result displayed:
Nothing is displayed on the LCD: you can adjust the variable resistor (indicated by the yellow rectangle) on the module to modify the contrast of the display.
Only rectangles are displayed on the first row of the LCD: the I2C address of the module is configured incorrectly in the software. Make sure the part number of the driving IC on the module. In the example sketch, it assumes the IC is PCF8574A, which uses the address of 0x3F. If the driving IC on the module is PCF8574 (without an "A" in the name), the address would be 0x27. You'll see how to assign the I2C address in the code later. For more details about the I2C address of the module, you can refer to the appendix section of this article.
By using the LiquidCrystal_I2C library, developers can easily access the 1602 display module. It provides APIs to control different functionalities of the display. Here we introduce basic ideas of them.
The constructor initializes the device and assigns the I2C address of the device to the software. For example, we assign the address 0x3F for the LCD with PCF8574A:
If the driving IC on the LCD module is PCF8574 (not PCF8574A), make sure the address is set to 0x27 instead of 0x3F for the LCD to work correctly.
This defines the display region. For a 1602 LCD, the API is called with (16, 2) as the input parameters:
Set the cursor back to the position 0 of the display.
Clear all contents and settings of the display.
Define the cursor position for displaying characters.
Output characters to the display from the position of the cursor. You can use ASCII characters as inputs or define customized bitmaps for displaying. For displaying ASCII characters, simply call the API with a ASCII string:
To display customized characters, you need to register the customized bitmaps first by the following API.
Each character on the 1602 LCD is a 5x8 matrix and it is defined as a 8-byte array, which uses the lower 5 bits of each byte to define the dots on a bitmap. Here we give an example of defining a "up-arrow" bitmap:
So it can be coded as:
By calling the createChar() API, you can register this bitmap with a desired index:
With above statement, the up-arrow bitmap is registered to the index 3. So you can show the up-arrow character on the LCD by calling:
Limitation
The 1602 LCD supports up to 8 customized bitmaps. So the valid range of the registered index is between 0 to 7.
Turn on / off the display (not the backlight). One of the applications of this operation is to blink the texts displayed while keeping the backlight on.
Turn on / off the backlight (not the display).
Move all characters on the display left / right by one position. This is done by changing the coordinate space of the display, not by modifying the coordinate of each character.
Define the cursor moving direction.
As shown in the example video above, we combined APIs mentioned in the previous section together to make the HelloLinkIt7697 example sketch. You can refer to the source codes to see how to put operations provided by the library into real display applications.
As indicated in the datasheet, the default I2C device address of PCF8574A is 0x3F:
and the address of PCF8574 is 0x27:
For the library, the 7-bit I2C address format is used so that's why we assign 0x3F / 0x27 as the address for the device. However, the module also provides flexibilities for developers to customize the address to prevent from address collision on the same I2C bus. This is achieved by short-circuiting the A0 / A1 / A2 pads indicated by the yellow rectangle below:
The address bits (A0 / A1 / A2) of the PCF8574A / PCF8574 are controlled by the A0 / A1 / A2 pins on the chip and it's pulled high on the module by default. The schematic diagram is:
Therefore, if you mount a 0R resistor on the A0 pads of the module, for example, it will make the A0 bit to 0 and this changes the I2C address of this device from 0x3F to 0x3E on PCF8574A (or from 0x27 to 0x26 on PCF8574) according to the address map listed above. By following the same idea, you can modify the address bit A1 and A2 in the same way to customize your own I2C device address.