In this tutorial you will learn how to build a simple serial 16×2 LCD display that is controlled via Arduino board by only two wires. The magic behind is done by the PCF8574 chip, an I/O expander that communicates with the micro-controller by using I2C protocol. The PCF8574 is a quick and easy solution to extending and adding output/input ports to Arduino. The chip connects to a standard I2C bus and adds an additional 8 output ports. A total of 8 LCD displays can be connected to the same two wire I2C bus with each board having a different address.
Firstly, you have to download the library, there are different downloads for the 1.0 and 0022 Arduino versions:
library for Arduino 1.0
library for Arduino 0022 and older
After that, extract the files to your libraries folder inside your Arduino installation.
Get your breadboard and build the circuit show in the first picture.
Open your Arduino IDE and go to File > Examples > LiquidCrystal_I2C and select “Hello World”. The following code will open:
LiquidCrystal_I2C lcd(0x20,16,2); // set the LCD address to 0x20 for a 16 chars and 2 line display
lcd.init(); // initialize the lcd
// Print a message to the LCD.
Understanding How PCF8574 Works
The PCF8574 is a 8 bits I/O port expander that uses the I2C protocol. That protocol communicates using a 2-wire serial interface, where one wire is a serial clock (SCL) and the other is for serial data (SDA). With this IC, you can use only 2 ports of your Arduino board to control up to 8 digital I/O ports. In the I2C protocol, each IC has a different address, in the case of PCF8574, you have a 3 bits address, which enable you to connect up to 8 devices, having 64 ports in total.
We can take a look on the PCF8574 pinout in the data datasheet:
Where: A0,A1,A2 are address pins
P0,P1,P2,P3,P4,P5,P6,P7 are digital I/O ports (each one is 1 bit)
SDA,SCL are the I2C pins to communication with the microcontroller.
To make the IC work, it is necessary to set and address, again we can find this information on the datasheet:
Where: L means LOW or 0V(GND), H means HIGH or VCC
To define an address set A0, A1 and A2 to H or L as you desire. Remember that each IC should has a different address.
Checking the data interface, we can see how the I2C interface will work on the PCF8574:
If you send a I/O data byte, it will turn on or off the corresponding pins P1 to P7 according to the byte send. For example, if you send the number 32, it will be 0010000 in binary, which will turn on only the P5 output. For reading, the same is true, if you receive the number 32, means that only the port P5 was on at that moment.