Interfacing 16x2 LCD with ESP32 using I2C
What is a 16x2 LCD Display?
A 16x2 LCD is a popular alphanumeric display that can show 2 lines of 16 characters each. Using an I2C adapter with this LCD reduces the number of required GPIOs from 6 to just 2, making it ideal for microcontroller-based projects like the ESP32.
Working Principle of I2C LCD
The I2C module converts the parallel interface of the LCD to a serial two-wire (I2C) interface. It communicates with the ESP32 over SDA and SCL lines. The LiquidCrystal_I2C library handles the low-level communication, allowing easy display control.
- Connect the I2C LCD to the ESP32 using SDA and SCL pins.
- Install the LiquidCrystal_I2C library in Arduino IDE.
- Initialize the LCD with its I2C address in the code.
- Print messages or sensor values on the LCD display.
Formula: No specific formula, just use `lcd.print()` to display text.
Components Required
- ESP32 development board
- 16x2 LCD with I2C module
- Jumper wires
- Breadboard (optional)
- USB cable
Pin Configuration of 16x2 I2C LCD
- GND: Connects to GND of ESP32
- VCC: Connects to 3.3V or 5V of ESP32
- SDA: Serial Data line
- SCL: Serial Clock line
Typical I2C address for the LCD is 0x27 or 0x3F. Use I2C scanner code to find yours if unsure.
Wiring 16x2 I2C LCD to ESP32
- VCC -> 3.3V or 5V
- GND -> GND
- SDA -> GPIO 21
- SCL -> GPIO 22
Arduino Code for ESP32 + 16x2 I2C LCD
1#include <Wire.h>
2#include <LiquidCrystal_I2C.h>
3
4LiquidCrystal_I2C lcd(0x27, 16, 2);
5
6void setup() {
7 lcd.init();
8 lcd.backlight();
9 lcd.setCursor(0, 0);
10 lcd.print("Hello, ESP32!");
11 lcd.setCursor(0, 1);
12 lcd.print("LCD I2C Ready");
13}
14
15void loop() {
16 // Nothing here for now
17}
Code Explanation (Line-by-Line)
- #include <Wire.h>: Includes the Wire library for I2C communication.
- #include <LiquidCrystal_I2C.h>: Includes the LCD library to control the 16x2 display over I2C.
- LiquidCrystal_I2C lcd(0x27, 16, 2);: Creates an LCD object with I2C address 0x27 and 16x2 display size.
- lcd.init();: Initializes the LCD screen.
- lcd.backlight();: Turns on the LCD backlight.
- lcd.setCursor(0, 0); lcd.print(...);: Sets cursor position and prints text on line 1.
- lcd.setCursor(0, 1); lcd.print(...);: Prints text on the second line of the display.
Applications
- Real-time display of sensor data
- Weather station readouts
- Home automation displays
- IoT dashboards
- DIY menu systems
Conclusion
Interfacing a 16x2 LCD with ESP32 using I2C is an effective way to add visual feedback to your IoT or embedded projects. With just two wires, it simplifies wiring and provides a clean method to display useful data and messages.