Driving 1602 LCD with PCF8574 / PCF8574A

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.

Table of contents

Download and install the library

Download

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.

Hardware setup

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:

Trouble-shooting

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.

LiquidCrystal_I2C library

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.

LiquidCrystal_I2C()

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:

// set the I2C address of the LCD controller
//    0x3F for PCF8574A
//    0x27 for PCF8574
LiquidCrystal_I2C lcd(0x3F);

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.

begin()

This defines the display region. For a 1602 LCD, the API is called with (16, 2) as the input parameters:

// init the 1602 (2 rows / 16 columns) LCD
lcd.begin(16, 2);

home()

Set the cursor back to the position 0 of the display.

clear()

Clear all contents and settings of the display.

setCursor()

Define the cursor position for displaying characters.

print()

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:

// display ASCII characters directly
lcd.print("LinkIt 7697");

To display customized characters, you need to register the customized bitmaps first by the following API.

createChar()

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:

// define the up-arrow bitmap
const uint8_t up_arrow[8] = {0x04, 0x0E, 0x1F, 0x04, 0x04, 0x04, 0x00, 0x00};

By calling the createChar() API, you can register this bitmap with a desired index:

// register the bitmap to index 3
lcd.createChar(3, (uint8_t *)up_arrow);

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:

// show the bitmap (with index 3) on the LCD
lcd.print(char(3));

Limitation

The 1602 LCD supports up to 8 customized bitmaps. So the valid range of the registered index is between 0 to 7.

display() / noDisplay()

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.

backlight() / noBacklight()

Turn on / off the backlight (not the display).

scrollDisplayLeft() / scrollDisplayRight()

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.

leftToRight() / rightToLeft()

Define the cursor moving direction.

HelloLinkIt7697 example

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.

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

// set the I2C address of the LCD controller
//    0x3F for PCF8574A
//    0x27 for PCF8574
LiquidCrystal_I2C lcd(0x3F);

// define the custom bitmaps
// up to 8 bitmaps are supported
const uint8_t my_bitmap[][8] =
{
  // chequer
  {0x15, 0x0A, 0x15, 0x0A, 0x15, 0x0A, 0x15, 0x00},
  // up arrow
  {0x04, 0x0E, 0x1F, 0x04, 0x04, 0x04, 0x00, 0x00},
  // down arrow
  {0x00, 0x00, 0x04, 0x04, 0x04, 0x1F, 0x0E, 0x04},
  // rectangle
  {0x00, 0x1F, 0x11, 0x11, 0x11, 0x11, 0x1F, 0x00},
  // up-left arrow
  {0x1F, 0x1E, 0x1C, 0x1A, 0x11, 0x00, 0x00, 0x00},
  // up-right arrow
  {0x1F, 0x0F, 0x07, 0x0B, 0x11, 0x00, 0x00, 0x00},
  // down-left arrow
  {0x00, 0x00, 0x00, 0x11, 0x1A, 0x1C, 0x1E, 0x1F},
  // down-right arrow
  {0x00, 0x00, 0x00, 0x11, 0x0B, 0x07, 0x0F, 0x1F},
};

#define LONG_DELAY()  delay(1000)
#define SHORT_DELAY() delay(500)
#define ANIM_DELAY()  delay(400)

void setup()
{
  int i;
  int bitmap_size = sizeof(my_bitmap) / sizeof(my_bitmap[0]);

  // init the 1602 (2 rows / 16 columns) LCD
  lcd.begin(16, 2);

  // register the custom bitmaps
  for (i = 0; i < bitmap_size; i++)
  {
    lcd.createChar(i, (uint8_t *)my_bitmap[i]);
  }

  // move the cursor to 0
  lcd.home();
  lcd.print("          Hello!");
  
  // scroll left for 5 positions
  for (int i = 0; i < 5; i++)
  {
    lcd.scrollDisplayLeft();
    
    ANIM_DELAY();
  }

  // clear the LCD contents and the scrolling offset
  lcd.clear();

  // turn off the backlight
  lcd.noBacklight();
  
  LONG_DELAY();

  // turn on the backlight
  lcd.backlight();
  
  lcd.print("     Hello!     ");
  // change to the starting position of the next line
  lcd.setCursor ( 0, 1 );
  lcd.print("  LinkIt 7697   ");

  // blinks the texts
  for (i = 0; i < 2; i++)
  {
    ANIM_DELAY();
    lcd.noDisplay();
    
    ANIM_DELAY();
    lcd.display();
  }
  
  LONG_DELAY();
  LONG_DELAY();

  // clear all LCD contents and settings
  lcd.clear();
  
  SHORT_DELAY();
}

void loop()
{
  int i, ch_map;

  // clear the LCD and set the string direction to left-to-right
  lcd.clear();
  lcd.leftToRight();  
  lcd.print("    LeftToR");
  
  LONG_DELAY();

  lcd.setCursor(4, 0);
  lcd.print("12345678");
  
  SHORT_DELAY();
  
  lcd.setCursor(4, 0);

  // display the custom characters. randomly select a character bitmap
  for (i = 0; i < 8; i++)
  {
    ch_map = random(8);
    
    lcd.print(char(ch_map));

    ANIM_DELAY();
  }

  lcd.setCursor(0, 1);
  lcd.print("    RightToL");
  LONG_DELAY();

  // set the string direction to right-to-left
  lcd.rightToLeft();
  lcd.setCursor(11, 1);
  lcd.print("12345678");
  SHORT_DELAY();
  
  lcd.setCursor(11, 1);

  // display the custom characters. randomly select a character bitmap
  for (i = 0; i < 8; i++)
  {
    ch_map = random(8);

    lcd.print(char(ch_map));

    ANIM_DELAY();
  }
}

Appendix

The I2C address of the module

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.

Last updated