Visualize the ESP32 : bring LCD I2C 16x2 to life

Garin Ichsan Nugraha
Garin ESP32 DEVKIT
Published in
2 min readFeb 16, 2020

This time we exercise how to use a monitor to connect with ESP32. We use OLED or LCD 16x2 with 16 pins that we use I2C converter to communicate it with ESP32. It’s really easy to do guys, so just try it!

First, you need to know the schematics like this picture below, I used GPIO 21 for SDA (orange) and GPIO 22 (yellow) for SCL, you can use another pin as you want as long as that pin is compatible.

Then you need to install the LiquidCrystal_I2C library in Arduino IDE and run this code first

/*********
Rui Santos
Complete project details at https://randomnerdtutorials.com
*********/
#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);
}

After that you’ll get the LCD’s address, mostly it will be 0x27. but it can be different depend on the factory that produce the LCD.

Use that address to this code below and try to run it.

*********
Rui Santos
Complete project details at https://randomnerdtutorials.com
*********/
#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 run it correctly you’ll get your LCD print some word like this :

Fun isn’t it?!

See ya in our next project!!

--

--