A regular LCD requires a lot of wires (parallel interface) to be connected with a Microcontroller.The Serial LCD backpack built on PCF8574 IC uses the I2C bus to convert the parallel interface to a serial one.This needs only 2 wires SDA & SCL , apart from the power connections.
The I2C backpack can be soldered on to the LCD .It can be used for 16 x 2 , 16 x 4 & 20 x 4 LCDs that have HD44780 IC .
The blue preset is to adjust the contrast of the LCD. The black jumper on the left is to enable the Backlight of LCD. The I2C device has a HEX address by which a microcontroller can communicate with it.This is set by the 3 bits A0,A1 ,A2 .If no jumper is present , it is HIGH & a jumper means LOW. By default all the 3 jumpers are open . ie., A0,A1 A2 all are 1s.
By setting this address lines you can connect upto 8 devices to the same I2C bus.
Eight programmable slave addresses possible using these three address pins.
The I2C bus has 2 bidirectional active wires SDA & SCL .They are joined to positive supply through a pull up resistor of 4k7.When the bus is idle both lines are pulled high.
The expanders can be configured to have a unique 7 bit address. The first 4 bits are factory configured to
0100 for PCF8574/PCF8574T
0111 for PCF8574A
The lower 3 bits are settings on device pins A2,A1,A0.
The eighth bit specifies whether the following data is a Read or Write operation.
Following table describes how the address is arrived.By default the factory setting is A2,A1,A0 all open leading to all 111.
First 4 bits are factory set to 0100 for PCF8574T/PCF8574 .If the port expander IC is PCF8574A , the first 4 bits will be 0111.
For calculating the address a 0 is added at the MSB (R/W bit not taken into account for calculation)
Hence the default I2C address of the module is HEX 0x27 .
Now let us see how to use this backpack with Arduino UNO.
To start with download the latest Library from this link :
https://github.com/fmalpartida/New-LiquidCrystal
NewliquidCrystal_1.3.4 is the latest file at the time of writing this blog
Unzip the downloaded file & copy the folder NewliquidCrystal.
Paste this folder into your Arduino libraries folder.
Generally the path is C:\ProgramFiles\Arduino\libraries
Now open the Arduino IDE (make sure your Arduino IDE is the latest version).
Under File –> Examples , you can see the NewliquidCrystal
To know the I2C address of the device there is a scan code in Arduino forum.
Before starting the test , make sure that the I2C back pack is soldered properly on to the LCD.Here we make use of a 16 x 2 LCD.
Connections between Arduino & I2C LCD are done as below :
Upload the scanner code on to the Arduino. Open the Serial monitor & set the baud rate to 115200.
The code scans for the I2C devices & lists the address in HEX format.
In our test we’ve connected a backpack I2C with no jumpers on A2,A1,A0 , which results in 0x27
This is the Slave address of the device.
The LCD Library & some sample code can be downloaded from :
http://www.alselectro.com/files/Library_codeexamples.zip
Once the connections are made, open the Arduino IDE .Make sure that you use the IDE later than 1.6.0 version.
The Hello world sketch includes 2 header files – wire.h & LiquidCrystal_I2C.h
A new object named lcd is instantiated using the constructor LiquidCrystal_I2C which takes parameters 0x27,2,1,0,4,5,6,7
Here 0x27 is the slave address of I2C backpack.
The next parameter 2 represents P2 pin of PCF8574 which is connected to EN of the LCD.
The parameter 1 is the P1 pin connected to R/W of LCD.
The parameter 0 is the P0 pin connected to RS of LCD.
4,5,6,7 are the 4 data pins connected to D4,D5,D6,D7 of LCD.
Inside setup , lcd.begin(16,2) ; initializes the 16 x 2 LCD .Then the lcd.setBacklightPin(3,POSITIVE);
makes the P3 pin makes the P3 pin to control the backlight of LCD.
lcd.setBacklightPin(HIGH); makes the P3 pin go High, which turns on the NPN transistor.This provides GND to the LED pin of LCD As the other LED pin is already connected to Vcc through the jumper , the LCD backlight glows.
lcd.home(); takes the cursor to 1st column , 1st Row of LCD.
lcd.print() prints the message.
lcd.setCursor(0,1); takes the cursor to 1st Column , 2nd Row. Make note that the counting starts from 0.
The following is the connection diagram between PCF8574 IC & the LCD .Once you solder the backpack on to LCD, these connections come into effect.
The next sketch is lcd_serialprint.
This code waits for you to type in message in the serial monitor of Arduino & then displays it on the LCD.
Next code shows the capability of connecting more than 1 device to the same I2C bus.
2 LCDs are connected to the same I2C bus A4,A5 of Arduino.External power source is used as Arduino can’t source power to both LCDds.
The backpack connected to 16 x2 LCD has no jumpers soldered to A0,A1,A2 .Hence the address is the default 0x 27.
The backpack connected to 20 x 4 LCD has A0 pin soldered, which makes the address 0x26.
By initiating the LCDs with their slave address, messages can be displayed on to the correct slave LCD.
VIDEO TUTORIAL :
Any chance of simple instructions on how to program the I2C serial 16×2 LCD using assembly language? (in my case a PIC18F45K22) I’m used to following an LCD datasheet and sending it the sequence of bytes and control lines it wants together with appropriate delay loops. I’m not familiar with C and only get a vague idea from the various libraries of how many bytes are needed to send a command or an ASCII character to display. I understand that the PCF8574T automatically initialises the LCD on power-up, and that an I2C address is needed.