Using an LCD to Display ESP-32’s Program Output

Jingga Mutiara Windyarahma
4 min readFeb 16, 2020

Hi!

In this System Embedded project, we are using a liquid crystal display as a media to show a program’s printed output.

Last Thursday, Scott, Jori, and I tried it out with a simple ‘Hello, World!’ program. Scott has this left-over LCD back from TPB days, but it didn’t have an I2C converter yet. ’Cause we’ve been cautioned that it might be hard to test this out without a converter, we borrowed an LCD from Holly.

As usual, for an Embedded System project, you’d need the basic gang:

  • ESP32 board
  • jumper wires
  • a 16x2 I2C LCD with a I2C converter (without it it’ll be quite a struggle connecting it to ESP)
  • a laptop with Arduino IDE installed
  • a USB cable

First thing first, connect ESP32 with the LCD using I2C (Inter-Integrated Circuit) serial bus configuration as seen on the picture below. We used GPIO 21 and 22 as we are using ESP32. Other counterpart, ESP8266, uses GPIO 4 and 5.

Next, download the LCD I2C libraries from the link below.

Extract the compressed file and move it into Arduino IDE’s ‘libraries’ folder. When you open the IDE after this, the library should have been installed.

Next on, we have to know the LCD’s hardware address. The code below executes it and displays the result on Serial Monitor with 115200 baud rate.

/*********
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 knowing the LCD’s address, set it as the parameter of the ‘lcd’ function in the code below. The code is supposed to print ‘Hello, World!’ on LCD’s display. In our case, the LCD’s address was 0x27.

/*********
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();
}

And voila! Once you’ve uploaded your simple program, your LCD should display the message on its screen. We have a 5-seconds video of it — it worked, but the LCD wasn’t that bright in the first place since it should have gotten a 5 Volt input (whereas ESP’s is 3.3 V), so you might have to squint to see the printed output on the screen.

We’re going to integrate input from weather sensor into an output on LCD’s screen next week. Stay tuned!

--

--