In our projects, the ability to display information is crucial for user interaction and data visualization. One popular display option is the 16×2 LCD (Liquid Crystal Display), which provides a simple yet effective way to showcase text and basic graphics. In this article, we will explore how to interface an ESP32 microcontroller with a 16×2 LCD display, enabling you to display messages and data in your projects.
Things you need
- ESP32 Development Board
- 16×2 LCD Display Module
- Breadboard
- Jumper Wires
- 10kΩ Potentiometer
Working
The 16×2 LCD display consists of two rows, each capable of displaying up to 16 characters. The display is controlled through a Hitachi HD44780 or a compatible controller. It communicates with the microcontroller using a parallel or I2C interface.
Circuit Diagram
To interface ESP32 with the 16×2 LCD display, we use the I2C communication protocol which is more convenient. The I2C protocol allows for easy and efficient communication between the microcontroller and the display, requiring only two wires (SDA and SCL) for data transfer.
Code
Make sure to install the “LiquidCrystal_I2C” library in the Arduino IDE by going to “Sketch” -> “Include Library” -> “Add .zip library” and select the zip file which you have downloaded”. The library can be downloaded from the button below.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address (refer to the datasheet for your specific module)
LiquidCrystal_I2C lcd(0x27, 16, 2);
int counter = 0;
void setup() {
// put your setup code here, to run once:
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the LCD backlight
lcd.setCursor(0, 0); // Set the cursor to the first column of the first row
lcd.print("Electronics Simplified"); // Print a message
}
void loop() {
// put your main code here, to run repeatedly:
lcd.setCursor(0,1);
lcd.print("Count:");
lcd.print(counter);
delay(1000);
lcd.clear();
counter++;
}
i2c Scanner (optional)
Mostly the i2c address is 0x27 on most devices. But in come cases you might need to use the i2c scanner in order to find your address. You can refer the code below:
#include <Wire.h>
void setup() {
Wire.begin();
Serial.begin(115200);
Serial.println("\nI2C Scanner");
Serial.println("Please wait");
}
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);
}
On finding the right address, you can replace it in the code. Please find the line which says
LiquidCrystal_I2C lcd(0x27, 16, 2);
and change the address which says 0x27 in my case to whatever your serial monitor showed up on running the i2c scanner code.
Conclusion
Interfacing the ESP32 with a 16×2 LCD display opens up new possibilities for displaying information in your embedded projects. By leveraging the I2C communication protocol, you can easily integrate the display, providing a user-friendly interface for visualizing data and messages. With the information and code provided in this article, you are now equipped to incorporate a 16×2 LCD display into your ESP32 projects. Have fun experimenting and creating!