Hello readers, we hope you all are doing great. Welcome to the 1st lecture of Section 4 in the ESP32 Programming Series. In this section, we will interface the ESP32 module with common Embedded modules(i.e. LCD, Keypad, RTC etc.).
In today's tutorial, we will interface ESP32 with a 16x2 LCD and will display data using both Data Mode and I2C Mode. LCD is the most commonly used embedded module in IoT Projects. It is used to display different types of data i.e. sensor readings, warning messages, notifications etc.
Before going forward, let's first have a look at what is LCD and How it works:
Where To Buy? | ||||
---|---|---|---|---|
No. | Components | Distributor | Link To Buy | |
1 | ESP32 | Amazon | Buy Now |
LCD(Liquid Crystal Display) is a type of electronic display module that is used in a wide variety of applications and devices such as calculators, computers, mobile phones, TVs, etc. There are different types of LCDs available for commercial use. Today, we are going to use the most simple one i.e. 16x2 LCD, shown in the below figure:
This 16x2 LCD has 16 columns and 2 rows, so in total of 32 blocks to display characters. Each Block can display a single character at a time. We can display text in different styles on this LCD i.e. blinking, scrolling etc. Another variant of this LCD is 20x4 LCD and as the name suggests, it has 20 columns and 4 rows and so can display 80 characters at a time. The operating principle of both of these LCDs is quite similar. So, if you are working with a 20x4 LCD, you can still follow this tutorial.
Let's have a look at the LCD pinout:
Both 16x2 and 20x4 LCDs have 16 pins each, used to control 7 write on these LCDs. Among these 16 pins, we have:
LCD Pinout and its working is shown in the below table:
LCD Pinout |
||||
---|---|---|---|---|
Pin No. | Name | Working | ||
1 |
GND(Ground) |
Connected to Ground Terminal. |
||
2 |
Vcc(+5V) |
Connected to +5V. |
||
3 |
VE |
To Control the LCD Contrast. |
||
4 |
RS(Register Select) | If RS=0(GND), LCD operates in Data Mode and we can write characters on the LCD. |
||
If RS=1(+5V), LCD Command Mode gets activated and we can send commands to LCD i.e. erase, new line etc.. |
||||
5 |
R/W(Read & Write) | R/W=0(GND) enables the write operation on the LCD. (So, we normally keep this pin LOW, as we are interested in printing on the LCD). | ||
R/W=1(+5V) enables the read operation on the LCD. |
||||
6 |
EN(Enable) |
Enables the LCD to operate, so it should be kept HIGH. |
||
7 |
Data Pin 0 |
LCD has a total of 8 Data Pins(D0-D7) | ||
8 |
Data Pin 1 |
|||
9 |
Data Pin 2 |
|||
10 |
Data Pin 3 |
|||
11 |
Data Pin 4 |
|||
12 |
Data Pin 5 |
|||
13 |
Data Pin 6 |
|||
14 |
Data Pin 7 |
|||
15 |
LED+ |
Connected to +5V. Turn on the backlight LED. |
||
16 |
LED- |
Connected to GND. |
Now, let's interface the LCD with ESP32:
There are two methods to interface ESP32 with a 16x2 LCD:
In the Data Mode, we use the LCD Data Pins and send data serially, while in the I2C mode, we solder an I2C adapter with the LCD, which acts as a bridge and maps I2C data coming from the microcontroller to the Data Pins. Let's first interface ESP32 and LCD via Data Pins:
As we discussed earlier, LCD has 8 Data Pins used to communicate with the Microcontroller. There are two ways to send data from the Microcontroller to the LCD:
In complex projects, where you are dealing with multiple sensors & modules, its quite difficult to spare 8 Pins for LCD interfacing. So, normally 4-Pin method is preferred, which we are going to design next:
Here are the components required to interface LCD with ESP32:
Now, let's design the ESP32 LCD Circuit Diagram:
[Image]
As you can see in the above figure:
Here's our hardware setup for ESP32 LCD Interfacing:
[Image]
Now let's design the Programming Code to print a simple message on the LCD:
We are using Arduino IDE to compile and upload code in the ESP32 module. If you haven't installed it yet, please read How to Install ESP32 in Arduino IDE. Here's the code to print a message on the LCD:
#include
LiquidCrystal lcd(22,23,5,18,19,21);
void setup()
{
lcd.begin(16, 2);
lcd.clear();
// go to row 0 column 5, note that this is indexed at 0
lcd.setCursor(5,0);
lcd.print("ESP32");
// go to row 1 column 0, note that this is indexed at 0
lcd.setCursor(0,1);
lcd.print (" TheEnggProjects");
}
void loop()
{
}
#include
LiquidCrystal lcd(22,23,5,18,19,21);
In the Setup() Function:
lcd.begin(16, 2);
So, if you are using a 20x4 LCD, you should change its arguments to 20 and 4, as shown below:
lcd.begin(20, 4);
lcd.clear();
lcd.setCursor(5,0);
lcd.print("ESP32");
lcd.setCursor(0,1);
lcd.print (" TheEnggProjects");
As you can see, the LCD code is quite simple and I hope now you can easily print on the LCD. So, let's check the results:
Fig. 6
Sol. : Check the EN pin.
Fig. 7
This concludes the tutorial. We hope you found this of some help and also hope to see you soon with a new tutorial on ESP32.