In this quick tutorial we are going to connect an 16X2 character LCD module to ESP 32. Usually as ESP 32 has built in WiFi we can display the data through the internet using online dashboard on Computers or mobile devices but there are some cases where the data should be shown on simple displays connected directly to board like printing the sensor data, simple count down, displaying scrolling text and more. Mainly we can use these modules when there is no internet connectivity used in a project. So lets start by knowing the basics, pin out diagrams, circuit diagrams, with few examples.
This interfacing can be done through two methods:
- By connecting directly to LCD module with out using I2C module.
- By connecting with the help of I2C adapter.
Table of Contents
Requirements (Bill of Materials):
Product Name | Quantity | ||
---|---|---|---|
LCD 16X2 module with or without I2C adapter | 1 | https://amzn.to/3BNSRup | https://amzn.to/363ki7F |
10K potentiometer (if without I2C adapter) | 1 | https://amzn.to/34RnDGx | https://amzn.to/3vPQSEP |
ESP 32 WiFi and Bluetooth Board | 1 | https://amzn.to/3s0Sut6 | https://amzn.to/3Kw2np2 |
Few Connecting Wires | https://amzn.to/3H2BV4e | https://amzn.to/3J0WVu2 | |
5V power supply (Micro USB or External). | 1 | https://amzn.to/3s1a8g3 | https://amzn.to/364yInH |
I2C Adapter Module:
This module has a PCF8574T chip which can handle 16X2 and 20X4 displays and has an inbuilt potentiometer to adjust the contrast. with a jumper for Turning on and off the Backlight of the display.
ESP32 Pinout:
Method 1) Interfacing 16X2 LCD module with ESP 32 without using I2C adapter
In this method we are connecting the LCD module to ESP by using 12 connecting wires from 16 pins on the LCD Display. This method is relatively complex when compared with another method as it includes bunch of connecting wires which allocates more number of pins on ESP32.
Lets connect them by following the below Schematic diagram.
Circuit Diagram:
As you can see from the above circuit diagram we have added a 10 K Potentiometer which is used to adjust the contrast of the LCD display so the the characters are visible clearly. We used the LCD display which need a voltage of 5V we connected it to the VIN of the ESP where we can get 5V while microUSB is connected to the ESP 32. If you are using an external power supply 5v to ESP through VIN then connect the same wire to the LCD where 5V is required.
Look at the below table for detailed pin connections between ESP32, LCD and potentiometer.
LCD Pin –>ESP32 Pins
- PIN01-VSS -> GND
- PIN02-VDD -> 5V
- PIN03 V0-> 10K Pot (Middle pin)
- PIN04 RS-> GPIO19
- PIN05 RW-> GND
- PIN06 E -> GPIO23
- PIN07 D0-> NOT USED
- PIN08 D1-> NOT USED
- PIN09 D2-> NOT USED
- PIN10 D3-> NOT USED
- PIN11 D4-> GPIO18
- PIN12 D5-> GPIO17
- PIN13 D6-> GPIO16
- PIN14 D7-> GPIO15
- PIN15 A-> 5V
- PIN16 K-> GND
Program code:
To run this program you need to install a library Liquidcrystal, If it not installed on your Arduino IDE download from this Link.
Copy and paste the below code on Arduino IDE and Select the COM port of ESP32 and Board as ESP 32 Wrover module and Click on Upload.
// include the library code: #include <LiquidCrystal.h> // initialize the library with the numbers of the interface pins LiquidCrystal lcd(19, 23, 18, 17, 16, 15); void setup() { // set up the LCD's number of columns and rows: lcd.begin(16, 2); // Print a message to the LCD. lcd.print("circuitschools."); } void loop() { // set the cursor to column 0, line 1 // (note: line 1 is the second row, since counting begins with 0): lcd.setCursor(0, 1); // print the number of seconds since reset: lcd.print(millis() / 1000); }
After Uploading the code the LCD display shows the below output.
For more Example codes like scrolling text and adding custom character please refer our previous article where we explained them along with the code. here
Method 2:Interfacing 16X2 LCD module with ESP 32 using I2C adapter
In this method we are using an I2C adapter which is connected to LCD display with 16 pins and outputs only 4 pins, 2 for data and another 2 for power and ground. The I2C adapter has inbuilt potentiometer where we can adjust the contrast of the display and also has a jumper to turn on/off the LCD backlight.
Lets connect them by observing the below schematic diagram.
Circuit Diagram:
As you can see from the above circuit diagram the VCC and GND pins are connected to the 5V and GND respectively and the other two data pins SDA and SCL are connected to D21 and D22 respectively. to know which are the SDA and SCL pins of your ESP 32 Board check the pin out diagram.
Before getting into work we need to find the I2C address of the I2C adapter generally it will be 0x27 for for some modules it will be different so copy and paste the below code to find the I2C address in the serial monitor.
Code to get the I2C address:
#include <Wire.h> //include Wire.h library void setup() { Wire.begin(); // Wire communication begin Serial.begin(9600); // The baudrate of Serial monitor is set in 9600 while (!Serial); // Waiting for Serial Monitor Serial.println("\nI2C address Scanner CircuitSchools.com"); } void loop() { byte error, address; //variable for error and I2C address int devicecount; Serial.println("Scanning..."); devicecount = 0; for (address = 1; address < 127; address++ ) { // The i2c_scanner uses the return value of // the Write.endTransmisstion to see if // a device did acknowledge to the 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.print(address, HEX); Serial.println(" !"); devicecount++; } else if (error == 4) { Serial.print("Unknown error at address 0x"); if (address < 16) Serial.print("0"); Serial.println(address, HEX); } } if (devicecount == 0) Serial.println("No I2C devices found\n"); else Serial.println("done\n"); delay(5000); // wait 5 seconds for the next I2C scan }
After Uploading the code the serial monitor will show the following output:
Now you can prepare the code for displaying the data on the LCD module.
Program code:
The display module with I2c adapter requires an additional library, which you can install it from Arduino IDE library manager or you can download the latest library below.
- LiquidCrystal_I2C Library – link
// #include < Wire .h> we are removing this because it is already added in liquid crystal library #include <LiquidCrystal_I2C.h> // Create the lcd object address 0x3F and 16 columns x 2 rows LiquidCrystal_I2C lcd (0x27, 16,2); // void setup () { // Initialize the LCD connected lcd. begin (); // Turn on the backlight on LCD. lcd. backlight (); // print the Message on the LCD. lcd. print ( "CIRCUITSCHOOLS." ); } void loop () { //Here cursor is placed on first position (col: 0) of the second line (row: 1) lcd. setCursor (0, 1); // We write the number of seconds elapsed lcd. print ( millis () / 1000); lcd. print ( "SECONDS" ); delay (100); }
after uploading the code the output text will be displayed on the LCD Module as shown in below image.
For more examples please refer the link provided in the method 1.
Video Demonstration:
If you have any doubts please feel free to ask below and if you like our articles please follow us on social networks which inspires us to make more interesting projects.