Arduino I2C LCD Backpack Introductory Tutorial
Assume that you are moving towards a complex microcontroller project bundled with blinkers, beepers, and a display panel. To link a standard 16×2 LCD directly with the microcontroller, for instance Arduino, you would need atleast 6 I/O pins to talk to the LCD. However, if you use an LCD module with I2C interface, you only need 2 lines to process the display information. Now a days, it is not necessary to buy an expensive I2C LCD for this task because readymade serial backpack modules for standard LCDs are available at reasonable rates. You can use them with LCD modules that have a HD44780 compatible interface with various screen sizes by attaching to the back of the LCD module. This allows connection to your Arduino (or other microcontroller) using only four channels. Yippee!
I2C LCD Backpack
Hitachi’s HD44780 based 16×2 character LCD are very cheap and widely available, and is an essential part for any project that displays information. Using the LCD backpack, desired data can be displayed on the LCD through the I2C bus. In principle, such backpacks are built aorund PCF8574 (from NXP) which is a general purpose bidirectional 8 bit I/O port expander that uses the I2C protocol. The PCF8574 is a silicon CMOS circuit provides general purpose remote I/O expansion (an 8-bit quasi-bidirectional) for most microcontroller families via the two-line bidirectional bus (I2C-bus). Note that most backpack modules are centered around PCF8574T (SO16 package of PCF8574 in DIP16 package) with a default slave address of 0×27. If your backpack holds a PCF8574AT chip, then the default slave address will change to 0x3F. In short, your backpack is based on PCF8574T and the address connections (A0-A1- A2) are not bridged with solder it will have the slave address 0×27.
Reference circuit diagram of an Arduino-compatible LCD backpack is shown below. What follows next is information on how to use one of these inexpensive backpacks to interface with a microcontroller in ways it was exactly intended.
I2C LCD DisplayNow let’s get started. At first you need to solder the backpack to your LCD module. Ensure that the backpack pins are straight and fit in the LCD module, then solder in the first pin while keeping the backpack in the same plane with the LCD. Once you have finished the soldering work , get four jumper wires and connect the LCD module to your Arduino as per the instruction given below.
Arduino Setup
For this experiment it is necessary to download and install the “Arduino I2C LCD” library. First of all, rename the existing “LiquidCrystal” library folder in your Arduino libraries folder as a backup, and proceed to the rest of the process.
Next, copy-paste this example sketch for the experiment into the blank code window, verify, and then upload.
If you are 100% sure that everything is okay, but you don’t see any characters on the display, try to adjust the contrast control pot of the backpack and set it a position where the characters are bright and the background does not have dirty boxes behind the characters. Following is a partial view of author’s experiment with the above described code. Since the display used by the author is a very clear bright “black on yellow” type, it is very difficult to get a good catch due to polarization effects.
- /*
- Project: I2C LCD Backpack Arduino Experiment
- By: T.K.Hareendran/TechNode Protolabz
- For: http://www.electroschematics.com
- Includes: Library from https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads*
- Hardware/Controller: See article
- Software/Sketch: Precisely adapted – Ref: Internet
- Date: December 2015
- */
- #include "Wire.h" // For I2C
- #include "LCD.h" // For LCD
- #include "LiquidCrystal_I2C.h" // Added library*
- //Set the pins on the I2C chip used for LCD connections
- //ADDR,EN,R/W,RS,D4,D5,D6,D7
- LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7); // 0x27 is the default I2C bus address of the backpack-see article
- void setup()
- {
- // Set off LCD module
- lcd.begin (16,2); // 16 x 2 LCD module
- lcd.setBacklightPin(3,POSITIVE); // BL, BL_POL
- lcd.setBacklight(HIGH);
- }
- void loop()
- {
- lcd.home (); // Set cursor to 0,0
- lcd.print("protolabz"); // Custom text
- lcd.setCursor (0,1); // Go to home of 2nd line
- lcd.print(millis());
- delay(1000); // Blinks of backlight
- lcd.setBacklight(LOW); // Backlight off
- delay(500);
- lcd.setBacklight(HIGH); // Backlight on
- delay(1000);
- }
Lab Note
The hardware configuration described have been tested with an Arduino UNO R3, a 16×2 LCD, and an I2C LCD backpack purchased from an eBay seller. Further, the example sketch has been fully tested with a 4 bit interface as described using Arduino IDE0022. Source code for the library used here and its official documentation can be downloaded from the download section of this repository which comes in source and with examples that will get you started. Additionally you have a full description of the library in the docs folder in HTML format that you can browse. Have fun