Arduino - LCD I2C

In this Arduino LCD I2C tutorial, we will learn how to connect an LCD I2C (Liquid Crystal Display) to the Arduino board. LCDs are very popular and widely used in electronics projects for displaying information. There are many types of LCD. This tutorial takes LCD 16x2 (16 columns and 2 rows) as an example. The other LCDs are similar.

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×LCD I2C
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

About LCD I2C 16x2

In the previous tutorial, we had learned how to use the normal LCD. However, wiring between Arduino and the normal LCD is complicated. Therefore, LCD I2C has been created to simplify the wiring. Actually, LCD I2C is composed of a normal LCD, an I2C module and a potentiometer.

Pinout

LCD I2C uses I2C interface, so it has 4 pins:

  • GND pin: needs to be connected to GND (0V).
  • VCC pin: the power supply for the LCD, needs to be connected to VCC (5V).
  • SDA pin: I2C data signal
  • SCL pin: I2C clock signal
LCD I2C Pinout

LCD Coordinate

LCD I2C 16x2 includes 16 columns and 2 rows. the conlums and rows are indexed from 0.

Arduino LCD I2C Coordinate

Wiring Diagram

Arduino LCD I2C Wiring Diagram

This image is created using Fritzing. Click to enlarge image

LCD I2C Arduino Uno, Nano Arduino Mega
Vin 5V 5V
GND GND GND
SDA A4 20
SCL A5 21

How To Program For LCD I2C

Thanks to the LiquidCrystal_I2C library, the using LCD is a piece of cake.

  • Include the library:
#include <LiquidCrystal_I2C.h> // Library for LCD
  • Declare a LiquidCrystal_I2C object with I2C address, the number of columns, the number of rows:
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
  • Initialize the LCD.
lcd.init(); //initialize the lcd lcd.backlight(); //open the backlight
  • Move cursor to the desired position (column_index, row_index)
lcd.setCursor(column_index, row_index);
  • Print a message to the LCD.
lcd.print("Hello World!");

There are many things more that we can do with LCD (see Do More with LCD part)

※ NOTE THAT:

The I2C address of LCD can vary according to the manufacturers. In the code, we used 0x27 that is specified by DIYables manufacturer

Arduino Code

#include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows void setup() { lcd.init(); // initialize the lcd lcd.backlight(); } void loop() { lcd.clear(); // clear display lcd.setCursor(0, 0); // move cursor to (0, 0) lcd.print("Arduino"); // print message at (0, 0) lcd.setCursor(2, 1); // move cursor to (2, 1) lcd.print("GetStarted.com"); // print message at (2, 1) delay(2000); // display the above for two seconds lcd.clear(); // clear display lcd.setCursor(3, 0); // move cursor to (3, 0) lcd.print("DIYables"); // print message at (3, 0) lcd.setCursor(0, 1); // move cursor to (0, 1) lcd.print("www.diyables.io"); // print message at (0, 1) delay(2000); // display the above for two seconds }

Quick Steps

  • Navigate to the Libraries icon on the left bar of the Arduino IDE.
  • Search "LiquidCrystal I2C", then find the LiquidCrystal_I2C library by Frank de Brabander
  • Click Install button to install LiquidCrystal_I2C library.
Arduino LiquidCrystal I2C library
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • See the result on LCD
Arduino display text on LCD
image source: diyables.io
  • Try modifying text and position

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

Do More with LCD

Custom Character

lcd.print() function supports only ASCII characters. If you want to display a special character or symbol (e.g. heart, angry bird), you need to use the below character generator.

LCD 16x2 can display 32 characters (2 rows and 16 columns). Each character is composed of 40 pixels (8 rows and 5 columns).

Arduino LCD 16x2 Pixel

The character generator represents a character (40 pixels). You just need to do the following steps:

Click on each pixel to select/deselect


Copy below custom character code
Replace the customChar[8] in the below code
#include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows byte customChar[8] = { 0b00000, 0b01010, 0b11111, 0b11111, 0b01110, 0b00100, 0b00000, 0b00000 }; void setup() { lcd.init(); // initialize the lcd lcd.backlight(); lcd.createChar(0, customChar); // create a new custom character lcd.setCursor(2, 0); // move cursor to (2, 0) lcd.write((byte)0); // print the custom char at (2, 0) } void loop() { }

Result on LCD:

LCD custom character

Multiple custom characters

We can create up to 8 custom characters (indexed 0 to 7). The below example creates and displays three characters.

#include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows byte customChar0[8] = { 0b00000, 0b01010, 0b11111, 0b11111, 0b01110, 0b00100, 0b00000, 0b00000 }; byte customChar1[8] = { 0b00100, 0b01110, 0b11111, 0b00100, 0b00100, 0b00100, 0b00100, 0b00100 }; byte customChar2[8] = { 0b00100, 0b00100, 0b00100, 0b00100, 0b00100, 0b11111, 0b01110, 0b00100 }; void setup() { lcd.init(); // initialize the lcd lcd.backlight(); lcd.createChar(0, customChar0); // create a new custom character (index 0) lcd.createChar(1, customChar1); // create a new custom character (index 1) lcd.createChar(2, customChar2); // create a new custom character (index 2) lcd.setCursor(2, 0); // move cursor to (2, 0) lcd.write((byte)0); // print the custom char 0 at (2, 0) lcd.setCursor(4, 0); // move cursor to (4, 0) lcd.write((byte)1); // print the custom char 1 at (4, 0) lcd.setCursor(6, 0); // move cursor to (6, 0) lcd.write((byte)2); // print the custom char 2 at (6, 0) } void loop() { }

Result on LCD:

LCD multiple custom characters

Summary: how to use custom character on LCD

  • Use the above character generator to create binary code for the custom character.
  • Declare the binary code for the custom character (copy from above step)
byte customChar[8] = { 0b00000, 0b01010, 0b11111, 0b11111, 0b01110, 0b00100, 0b00000, 0b00000 };
  • Create custom character and assign to an index value (from 0 to 7) in setup() function
lcd.createChar(index, customChar);
  • Print the custom character in LCD anytime, anywhere (in setup() or loop() function)
lcd.setCursor(column, row); // move cursor to a desired position lcd.write((byte)index); // print the custom char at the desired position

Other functions

Add the below functions into loop() function one by one. And add delay(5000) after each function

  • Clear LCD screen
lcd.clear();
  • Move the cursor to the upper-left of the LCD
lcd.home();
  • Move the cursor to the a position (column, row)
lcd.setCursor(column, row);
  • Display the LCD cursor
lcd.cursor();
  • Hides the LCD cursor.
lcd.noCursor();
  • Display the blinking LCD cursor
lcd.blink()
  • Turns off the blinking LCD cursor.
lcd.noBlink()

Challenge Yourself

Use LCD to do one of the following projects:

Troubleshooting on LCD I2C

If the text is not displayed on LCD I2C, please check the following issues:

  1. Adjust the brightness of LCD by rotating potentiometer in the backside of LCD
  2. Depending on manufacturers, the I2C address of LCD may be different. Usually, the default I2C address of LCD is 0x27 or 0x3F. Try these values one by one. If you still failed, run the below code to find the I2C address.
// I2C address scanner program #include <Wire.h> void setup() { Wire.begin(); Serial.begin(9600); Serial.println("I2C Scanner"); } void loop() { byte error, address; int nDevices; Serial.println("Scanning..."); nDevices = 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(" !"); nDevices++; } else if (error==4) { Serial.print("Unknown error at address 0x"); if (address < 16) Serial.print("0"); Serial.println(address,HEX); } } if (nDevices == 0) Serial.println("No I2C devices found"); else Serial.println("done"); delay(5000); // wait 5 seconds for next scan }

The result on Serial Monitor:

COM6
Send
Scanning... I2C device found at address 0x3F ! done Scanning... I2C device found at address 0x3F ! done
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

The Best Arduino Starter Kit

※ OUR MESSAGES