In IoT, LCD is rarely required, but some times its useful to monitor errors and connection related issues. In this tutorial we are interfacing I2C LCD with ESP8266 or ESP32. Both code examples are given.
We have very few IOs on ESP8266 and ESP32. I2C based display interface uses only two IO lines.
ESP8266 (NodeMCU) I2C LCD Interface
Parts Required
- I2C LCD Display Module PCF8574
- 16×2 LCD Display
- Connection Wires
- Single row female connector
- NodeMCU
Step 1: Installing I2C LCD Library for ESP8266 and ESP32
This library is tested for different types of LCD displays like 16×2, 16×4, 20×2, 20×4 with both ESP32 and ESP8266, it also works with other ESP modules.
ESP8266 with 20×4 i2c, 1602 LCD adaptable to others, tested with ESP-201 and ESP-01 Compatible with the Arduino IDE 1.6.6 Library https://github.com/agnunez/ESP8266-I2C-LCD1602
Step 1: Download Library
Step 2: Install it in Arduino IDE
Click on Sketch >> Include Library >> Add .ZIP Library
Then give your downloaded zip file path.
Step 2: Interface ESP8266 With I2C LCD Module
I2C LCD Module Pinout
You can adjust contrast using Blue color potentiometer. LCD default address is 0x27.
I2C LCD Module ESP8266
GND <——————-> GND
VCC <——————-> Vin
SDA <——————-> D2 (NodeMCU) GPIO 4
SCL <——————-> D1 (NodeMCU) GPIO 5
Step 3: Arduino IDE Code for ESP8266 I2C LCD Interfacing
This is simple code to display text, cursor and scrolling text. Upload this code it will demonstrate many functions of library such as scrolling, display on off, cursor positioning
/* * https://circuits4you.com * I2C LCD Interfacing with ESP8266 and ESP32 * */ #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display int show=0; void setup() { lcd.init(); // initialize the lcd // Print a message to the LCD. lcd.backlight(); lcd.setCursor(3,0); lcd.print("Hello, world!"); lcd.setCursor(0,1); lcd.print("circuits4you.com"); } void loop() { if (show == 0) { lcd.setBacklight(255); lcd.home(); lcd.clear(); lcd.print("Hello LCD"); lcd.setCursor(0,1); lcd.print("circuits4You.com"); delay(1000); lcd.setBacklight(0); delay(400); lcd.setBacklight(255); } else if (show == 1) { lcd.clear(); lcd.print("Cursor On"); lcd.cursor(); } else if (show == 2) { lcd.clear(); lcd.print("Cursor Blink"); lcd.blink(); } else if (show == 3) { lcd.clear(); lcd.print("Cursor OFF"); lcd.noBlink(); lcd.noCursor(); } else if (show == 4) { lcd.clear(); lcd.print("Display Off"); lcd.noDisplay(); } else if (show == 5) { lcd.clear(); lcd.print("Display On"); lcd.display(); } else if (show == 7) { lcd.clear(); lcd.setCursor(0, 0); lcd.print("*** first line."); lcd.setCursor(0, 1); lcd.print("*** second line."); } else if (show == 8) { lcd.scrollDisplayLeft(); } else if (show == 9) { lcd.scrollDisplayLeft(); } else if (show == 10) { lcd.scrollDisplayLeft(); } else if (show == 11) { lcd.scrollDisplayRight(); } delay(2000); show = (show + 1) % 12; }
Results
After uploading you will see some text on LCD. for arduino I2C LCD Interface see this example.
ESP32 I2C LCD Interfacing
Same code and Library works for ESP32. only difference is of connections.
Step 1: Download Library I2C-LCD-ESP8266-Library
Step 2: Make connections with ESP32
I2C LCD Module ESP8266
GND <——————-> GND
VCC <——————-> Vin
SDA <——————-> GPIO 21
SCL <——————-> GPIO 22
Step 3: ESP32 Arduino IDE Code for I2C LCD
Upload code
/* * https://circuits4you.com * I2C LCD Interfacing with ESP32 * */ #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display int show=0; void setup() { lcd.init(); // initialize the lcd // Print a message to the LCD. lcd.backlight(); lcd.setCursor(3,0); lcd.print("Hello, world!"); lcd.setCursor(0,1); lcd.print("circuits4you.com"); } void loop() {}
Results
Upload code and see LCD display is working. If you are looking for low cost solution you can use below referances
- 3-Wire LCD interface without I2C Module with Arduino
- NodeMCU 3-Wire LCD Interfacing
- OLED Display Interfacing with ESP32
- NodeMCU OLED Display Example
More on I2C
I2C is a serial protocol for two-wire interface to connect low-speed devices like microcontrollers, EEPROMs, A/D and D/A converters, I/O interfaces and other similar peripherals in embedded systems. It was invented by Philips and now it is used by almost all major IC manufacturers.
I2C LCD Module
It is based on PCF8574 I2C IO expander, Datasheet PCF8574.