Embedded Systems Project: ESP32 I2C LCD Display

Angela Geraldine
5 min readMar 12, 2023

--

The I2C LCD displays each character through a matrix grid of 5×8 pixels. These pixels can display standard text, numbers, or special characters and can also be programmed to display custom characters easily.

In the fifth Embedded Systems project, I will be showing how to write text, number, characters on the I2C LCD.

Components

For this project, I used one 16×2 I2C LCD, one ESP32, and four female-to-female jumper wires. I also used one USB cable to connect ESP32 to Arduino IDE software on computer.

I2C LCD, ESP32, jumper wires (female-to-female)

Schematic

Follow the wiring steps as shown above. All pins should be connected using female-to-female jumper wires.

Wiring I2C LCD to ESP32

I2C LCD Library and Address

We need to install ESP32 add-on that allows you to program the ESP32 using the Arduino IDE and its programming language.
For ESP32 add-on installation steps, you can refer to my first Embedded Systems project.

Other than ESP32 add-on, we need to install the LiquidCrystal_I2C Library.
Follow these steps to install the library.
1. Download the LiquidCrystal_I2C Library from this link.
2. Unzip the file you just downloaded.
3. Rename the folder to LiquidCrystal_I2C.
4. Move the LiquidCrystal_I2C folder to your Arduino IDE installation libraries folder. If you are using Windows, this folder is located in Documents\Arduino\libraries.
5. Restart your Arduino IDE.

After installation, we need to find the LCD I2C address through uploading the sketch below.

#include <Wire.h>

void setup() {
Wire.begin();
Serial.begin(115200);
Serial.println("\nI2C 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.println(address,HEX);
nDevices++;
}
else if (error==4) {
Serial.print("Unknow error at address 0x");
if (address<16) {
Serial.print("0");
}
Serial.println(address,HEX);
}
}
if (nDevices == 0) {
Serial.println("No I2C devices found\n");
}
else {
Serial.println("done\n");
}
delay(5000);
}

The address will be displayed on the Serial Monitor. If nothing is displayed, try to make sure the baud rate in Serial Monitor matches the number in Serial.begin(…)

From the picture above, we can see the I2C address is 0x27.

Code

1. Display Static Text
In Arduino IDE, upload the sketch below to display “Hello, World” on the LCD.

#include <LiquidCrystal_I2C.h>

// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;

// set LCD address, number of columns and rows
// if you don't know your display address, run an I2C scanner sketch
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);

void setup(){
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
}

void loop(){
// set cursor to first column, first row
lcd.setCursor(0, 0);
// print message
lcd.print("Hello, World!");
delay(1000);
// clears the display to print new message
lcd.clear();
// set cursor to first column, second row
lcd.setCursor(0,1);
lcd.print("Hello, World!");
delay(1000);
lcd.clear();
}

If you want to change the text displayed on the LCD, simply change the argument for the lcd.print(…) function.
For example I changed the argument to my name, lcd.print(“Angela Geraldine”).

2. Display Custom Characters
The code below is another sketch to display a heart character on the LCD.

#include <LiquidCrystal_I2C.h>

// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;

// set LCD address, number of columns and rows
// if you don't know your display address, run an I2C scanner sketch
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);

byte heart[8] = {
0b00000,
0b01010,
0b11111,
0b11111,
0b11111,
0b01110,
0b00100,
0b00000
};

void setup(){
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
lcd.createChar(0, heart);
}

void loop(){
lcd.setCursor(0, 0);
lcd.write(0);
delay(1000);
lcd.clear();
}

3. Display Scrolling Text
The LiquidCrystal_I2C library comes with built-in functions that allows you to scroll text. Scrolling text on the LCD is specially useful when you want to display messages longer than 16 characters. Upload the code below to display scrolling text.

#include <LiquidCrystal_I2C.h>

// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;

// set LCD address, number of columns and rows
// if you don't know your display address, run an I2C scanner sketch
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);

String messageStatic = "Hello there!";
String messageToScroll = "It's me! Angela Geraldine :) Welcome to my 5th project!";

// Function to scroll text
// The function acepts the following arguments:
// row: row number where the text will be displayed
// message: message to scroll
// delayTime: delay between each character shifting
// lcdColumns: number of columns of your LCD
void scrollText(int row, String message, int delayTime, int lcdColumns) {
for (int i=0; i < lcdColumns; i++) {
message = " " + message;
}
message = message + " ";
for (int pos = 0; pos < message.length(); pos++) {
lcd.setCursor(0, row);
lcd.print(message.substring(pos, pos + lcdColumns));
delay(delayTime);
}
}

void setup(){
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
}

void loop(){
// set cursor to first column, first row
lcd.setCursor(0, 0);
// print static message
lcd.print(messageStatic);
// print scrolling message
// scrollText(row, message, delayTime, lcdColumns)
scrollText(1, messageToScroll, 250, lcdColumns);
}

There are 4 arguments you can change when calling scrollText() in the loop().
1. row: row number where the text will be display
2. message: message to scroll
3. delayTime: delay between each character shifting. Higher delay times will result in slower text shifting, and lower delay times will result in faster text shifting.
4. lcdColumns: number of columns of your LCD

Result

  1. Display Static Text
Static Text on LCD

2. Display Custom Characters

Custom Character on LCD

3. Display Scrolling Text

Scrolling Text on LCD

I didn’t encounter any errors or difficulties throughout this project as I carefully read the tutorial beforehand. Well, that’s all for I2C LCD Display project, thanks for reading \ʕ•ᴥ•ʔ/

--

--