Random Nerd Tutorials

How to Use I2C LCD with ESP32 on Arduino IDE (ESP8266 compatible)


This tutorial shows how to use the I2C LCD (Liquid Crystal Display) with the ESP32 using Arduino IDE. We’ll show you how to wire the display, install the library and try sample code to write text on the LCD: static text, and scroll long messages. You can also use this guide with the ESP8266.

16×2 I2C Liquid Crystal Display

For this tutorial we’ll be using a 16×2 I2C LCD display, but LCDs with other sizes should also work.

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

To follow this tutorial you need these parts:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Wiring the LCD to the ESP32

This display uses I2C communication, which makes wiring really simple.

Wire your LCD to the ESP32 by following the next schematic diagram. We’re using the ESP32 default I2C pins (GPIO 21 and GPIO 22).


You can also use the following table as a reference.


Wiring the LCD to the ESP8266

You can also wire your LCD to the ESP8266 by following the next schematic diagram. We’re using the ESP8266 default I2C pins (GPIO 4 and GPIO 5).

You can also use the following table as a reference.


Preparing the Arduino IDE For ESP32

There’s an add-on for the Arduino IDE that allows you to program the ESP32 using the Arduino IDE and its programming language. Follow one of the next guides to prepare your Arduino IDE to work with the ESP32:

Preparing the Arduino IDE For ESP8266

There’s also an add-on for the Arduino IDE that allows you to program the ESP8266 using the Arduino IDE. Read: How to Install the ESP8266 Board in Arduino IDE.

Installing the LiquidCrystal_I2C Library

There are several libraries that work with the I2C LCD. We’re using this library by Marco Schwartz. Follow the next steps to install the library:

  1. Click here to download the LiquidCrystal_I2C library. You should have a .zip folder in your Downloads
  2. Unzip the .zip folder and you should get LiquidCrystal_I2C-master folder
  3. Rename your folder from LiquidCrystal_I2C-master to LiquidCrystal_I2C
  4. Move the LiquidCrystal_I2C folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Getting the LCD Address

Before displaying text on the LCD, you need to find the LCD I2C address. With the LCD properly wired to the ESP32, upload the following I2C Scanner sketch.

After uploading the code, open the Serial Monitor at a baud rate of 115200. Press the ESP32 EN button. The I2C address should be displayed in the Serial Monitor.

In this case the address is 0x27. If you’re using a similar 16×2 display, you’ll probably get the same address.

Display Static Text on the LCD

Displaying static text on the LCD is very simple. All you have to do is select where you want the characters to be displayed on the screen, and then send the message to the display.

Here’s a very simple sketch example that displays “Hello, World!“.

It displays the message in the first row, and then in the second row.

In this simple sketch we show you the most useful and important functions from the LiquidCrystal_I2C library. So, let’s take a quick look at how the code works.

How the code works

First, you need to include the LiquidCrystal_I2C library.

#include <LiquidCrystal_I2C.h>

The next two lines set the number of columns and rows of your LCD display. If you’re using a display with another size, you should modify those variables.

int lcdColumns = 16;
int lcdRows = 2;

Then, you need to set the display address, the number of columns and number of rows. You should use the display address you’ve found in the previous step.

LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);

In the setup(), first initialize the display with the init() method.


Then, turn on the LCD backlight, so that you’re able to read the characters on the display.


To display a message on the screen, first you need to set the cursor to where you want your message to be written. In the following line, we set the cursor to the first column, first row.

lcd.setCursor(0, 0);

Note: 0 corresponds to the first column, 1 to the second column, and so on…

Then, you can finally print your message on the display using the print() method.

lcd.print("Hello, World!");

We wait one second, and then we clean the display with the clear() method.


After that, we set the cursor to a new position: first column, second row.


And then, the process is repeated.

So, here’s a summary of the functions to manipulate and write on the display:

  • lcd.init(): initializes the display
  • lcd.backlight(): turns the LCD backlight on
  • lcd.setCursor(int column, int row): sets the cursor to the specified column and row
  • lcd.print(String message): displays the message on the display
  • lcd.clear(): clears the display

This example works well to display static text no longer than 16 characters.

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.

So, we’ve created a sample sketch with a function you can use in your projects to scroll longer messages.

The following sketch displays a static message in the first row and a scrolling message longer than 16 characters in the second row.

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()

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));

To use this function you should pass four arguments:

  • row: row number where the text will be display
  • message: message to scroll
  • 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.
  • lcdColumns: number of columns of your LCD

In our code, here’s how we use the scrollText() function:

scrollText(1, messageToScroll, 250, lcdColumns);

We’re displaying the messageToScroll variable in the second row (1 corresponds to the second row), with a delay time of 250 ms (the GIF image is speed up 1.5x).

Wrapping Up

In summary, in this tutorial we’ve shown you how to use an I2C LCD display with the ESP32/ESP8266 with Arduino IDE: how to display static text and scrolling text. This tutorial also works with Arduino board, you just need to change the pin assignment to use the Arduino I2C pins.

We have other tutorials with ESP32 that you may find useful:

We hope you’ve found this tutorial useful. If you like ESP32 and you want to learn more, we recommend enrolling in Learn ESP32 with Arduino IDE course.

Thanks for reading.

Learn ESP32 with Arduino IDE

This is our complete guide to program the ESP32 with Arduino IDE, including projects, tips, and tricks! The registrations are open, so SIGN UP NOW »

Recommended Resources

Home Automation using ESP8266 »
Build IoT projects and home automation gadgets with the ESP8266 Wi-Fi module.

Build a Home Automation System »
Learn how to build a automation system using open-source hardware and software from scratch.

Arduino Step-by-Step Projects »
Build 25 cool Arduino projects with our course even with no prior experience!

Leave a Comment:

Add Your Reply

Enroll in our Electronics and Programming Courses:

ESP32, ESP8266, Arduino and much more.