In this tutorial, I am going to teach you how to interface I2C Character LCD with Arduino UNO. A character LCD is a common type of LCD that is ideal for displaying text and number based information. What this means is that it cannot be used to display images, hence the word “Character” in the name.
I2C character LCD is just like the standard character LCD with 16-Pins connections. The only difference is that the I2C type has 4-pins which make it easier to work with. This is made possible by a small add-on circuit mounted on the back of the module. The add-on circuit features a PCF8574 chip (for I2C communication). Some new character LCD comes shipped with this add-on circuit but if you have the standard character LCD you can also buy the add-on circuit separately to convert your standard 16-pin LCD to I2C.
Character LCD Specifications
Most character LCDs has similar specifications. They all use the HD44780 Hitachi LCD controller. Difference is the number of rows or columns of characters they can display. In this tutorial we are going to use the I2C 16×2 character LED. This tutorial can as well work for I2C 24X4 characters LED, you only need to change size specification your code.
16×2 I2C Character LCD Specifications
Operating voltage | 5 V |
Controller | Hitachi HD44780 LCD controller |
Default address | 0x27 |
Screen resolution | 2-lines x 16 characters |
Character resolution | 5 x 8 pixels |
You can download full datasheet for the 16×2 I2C Character LCD here
Tools and materials
For this tutorial, you need the following tools, materials and software:
1. Arduino UNO R3 X1 | Buy Here |
2. I2C 16×2 Character LED X 1 | Buy Here |
3. Jumper Wires | Buy Here |
4. Arduino IDE | Download Here |
Circuit Connection
I2C Character LED | Arduino UNO R3 |
GND | GND |
VCC | 5V |
SDA | A4 |
SDL | A5 |
Note: sometimes, when you are using the I2C Character LCD for the first time, there is a possibility that you will not see anything displayed in the LCD; in that case, you may have to adjust the contrast of the LCD. At the add-on circuit at the back of the LCD, there is a trimmer variable resistor that you have to tune with a small screw driver to make the displayed characters to be visible.
Writing the program
First you need to download the LiquidCrystal_I2C library. Note this is different from the LiquidCrystal library for the standard character LCD. And there are different LiquidCrystal_I2C from different authors, but we are using the one by John Rickman, you can download it from the Github page here. Just be certain you don’t have other libraries with same name as LiquidCrystal_I2C because they will interfere. You can just delete the other ones.
This library also works in combination with the Wire.h library which allows you to communicate with I2C devices. If you don’t have it already installed, you can download it here.
If you have downloaded the libraries, you can install them following the screenshot below.
You can as well extract and copy the library to the library folder inside to your Arduino installation location.
I2C LCD Address
Before you can use the I2C Character LCD, you need to know the address. Most I2C LCDs ship with the default address ‘0x27’, but it can be different depending on the batch/manufacturer of the LCD.
Assuming the default 0x27 didn’t work for you, you can find your I2C Character LCD address using the following sketch. The ketch below will scan the I2C Bus and display the address in the serial monitor.
/*I2C_scanner*/
#include <Wire.h>
void setup() {
Wire.begin();
Serial.begin(9600);
while (!Serial);
Serial.println("nI2C Scanner");
}
void loop() {
byte error, address;
int devices;
Serial.println("Scanning for devices...");
devices = 0;
for (address = 1; address < 127; address++ ) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0){
Serial.print("I2C device found at address 0x");
if (address < 16)
Serial.print("0");
Serial.print(address, HEX);
Serial.println(" !");
devices++;
}
else if (error == 4) {
Serial.print("Unknown error at address 0x");
if (address < 16)
Serial.print("0");
Serial.println(address, HEX);
}
}
if (devices == 0)
Serial.println("No I2C devices foundn");
else
Serial.println("donen");
delay(5000);
}
Upload this sketch to the Arduino and run it, you should see the following output in the Serial Monitor (Ctrl + Shift + M).
You see the 0x27? That’s the address of your I2C Character LCD. Now lest proceed with the pragraming proper. You can upload the following example code to the Arduino UNO using the Arduino IDE.
/* How to interface I2C Character LCD with Arduino
For more tutorials and sketches visit: https://diymall.com.ng/diy-projects
*/
//Include the libraries:
#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for I2C Character LCD
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); //create a class with your I2C LCD address and the number of characters and lines your LCD
// can display
void setup(){
lcd.init(); //Initilize the LCD
lcd.backlight(); //ON the LCD backlight
}
void loop(){
lcd.setCursor(0, 0); // Set the cursor on the first character and first line.
lcd.print("Arduino I2C LCD"); // Print the string "Arduino I2C LCD"
lcd.setCursor(3, 1); //Set the cursor on the third character and the second line.
lcd.print("Tutorial");
}
If everything went well, you should see the following message from your LCD as shown below:
Explanations of the code
First, we included the required libraries, wire.h and the LiquidCrystal_I2C .h.
Then we created an LCD object with the LiquidCrystal_I2C class and specify the address and dimensions of our LCD. For this we use the function LiquidCrystal_I2C (address, columns, rows).
Note that we called the display object ‘lcd’. You can give it a different name if you like.
In the setup section, the lcd object is initiated with lcd.init() and the backlight is turned on with lcd.backlight().
In the loop section of the code, the cursor is set to the first character and the first line of the LCD with lcd.setCursor(0,0). Note that counting starts at 0 and the first argument specifies the column. Next the string ‘Arduino I2C LCD’ is printed with lcd.print(“Arduino I2C LCD”). Again lcd.setCursor(3,1) sets the cursor on the fourth column and the second row and lcd.print(“Tutorial”) is used to print the string ‘Tutorial’.
Note that you need to place quotation marks (” “) around the text, since we are printing a text string. When you want to print numbers or value of a variable, you don’t need the quotation marks.
For other functions of the library, you can take a look at the library page on GitHub or GitLab.