I2C LCD With ESP32
I2C LCD With ESP32
on Arduino IDE
This work shows how to use the I2C LCD (Liquid Crystal Display) with the ESP32 using
Arduino IDE.
16×2 I2C Liquid Crystal Display
The advantage of using an I2C LCD is that the wiring is really simple. You just need to wire
the SDA and SCL pins. Additionally, it comes with a built-in potentiometer you can use to
adjust the contrast between the background and the characters on the LCD. On a “regular”
LCD you need to add a potentiometer to the circuit to adjust the contrast.
Parts Required
ESP32 DOIT DEVKIT V1 Board
16×2 I2C Liquid Crystal Display (LCD)
Female to female jumper wires
GND GND
VCC VIN
SDA GPIO 21
SCL GPIO 22
#include <LiquidCrystal_I2C.h>
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();
}
Display Scrolling Text on the LCD
Scrolling text on the LCD is specially useful when you want to display messages longer than
16 characters. The library comes with built-in functions that allows you to scroll
text. However, many people experience problems with those functions because:
The function scrolls text on both rows. So, you can’t have a fixed row and a scrolling
row;
It doesn’t work properly if you try to display messages longer than 16 characters.
The following sketch displays a static message in the first row and a scrolling message
longer than 16 characters in the second row.
#include <LiquidCrystal_I2C.h>
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(1, messageToScroll, 250, lcdColumns);
}
After reading the previous section, you should be familiar on how this sketch works, so we’ll just
take a look at the newly created function: scrollText()