I am hoping that I can use the 16×2 LCD display as described in the following link. I am uncertain which pins to use for the SCL/SDA connections however, as per Module 7, Unit 4 of “Learn esp32”. This display doesn’t have the side pins installed.
Thanks for sharing that, Steph.
Jack, then tell me if you were able to make it work.
Regards,
Sara
Thanks Steph, but I was not able to make it work. The display just showed a row of blocks. I’m using the DOIT board rather than his WEMOS LOLIN32, but I assume that should not make a difference as long a the pin assignments are correct.
Jack
Hi.
The board you’re using doesn’t matter as long as you’re using the same pins.
Please, double-check your connections.
Regards,
Sara
Hi Jack,
I was able to find a 16×2 LCD screen that I got from an Arduino Starter Kit. I guess it’s the same as yours: it doesn’t have an I2C controller either.
I didn’t reproduce the same wiring as on the video I mentioned above, but I guess your problem is the library you’re using. There are several LiquidCrystal libraries out there, and I’m not sure if they use the same data pins when using only 4 pins.
I used the most common one: the official Arduino library, which is compatible with the ESP32 thanks to the Arduino core for the ESP32 from Espressif:
Liquid Crystal Library for Arduino
I preferred to make my own wiring, which I took a picture of for you, hoping that you can reproduce it on your side. Don’t take into account the blue LED, nor the DHT11 temperature sensor. Focus on the part of the circuit that is powered in 5V by the ESP32 (thanks to the USB cable), that is to say everything on the bottom breadbord.
The potentiometer of 10 kΩ is connected by its central pin to the V0 pin of the LCD screen.
It allows you to adjust the contrast.
Here’s the pinout of the screen I used:
LCD | ESP32 -----+------- VSS | GND VDD | 5V V0 | central pin of the 10 kΩ potentiometer RS | GPIO 16 (RX2) RW | GND E | GPIO 17 (TX2) D4 | GPIO 5 D5 | GPIO 18 D6 | GPIO 19 D7 | GPIO 21 A | 5V through a 220 Ω resistor K | GND
Pin A corresponds to the anode of the LCD backlight LED. And pin K corresponds to its cathode. I preferred to protect the LED with a 220 Ω resistor to avoid damaging it.
Here’s my demo code:
#include <Arduino.h> #include <Wire.h> #include <LiquidCrystal.h> #define LCD_RS 16 #define LCD_EN 17 #define LCD_D4 5 #define LCD_D5 18 #define LCD_D6 19 #define LCD_D7 21 LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7); void setup() { lcd.begin(16, 2); lcd.clear(); } void loop() { lcd.print("Hello Jack, look"); lcd.setCursor(0, 1); lcd.print("at my wiring... "); }
I hope this will help you move forward.
If not, please don’t hesitate to come back to me.
Steph
Steph, indeed your method worked (thanks!). And thanks for the elegant presentation. I guess there is something amiss with the youtuber’s method. The wiring was the same, save for changes to Tx, Rx, 19 and 21. I found the display data sheet here:
https://components101.com/16×2-lcd-pinout-datasheet
It states D0-D3 are “Four low order bi-directional three-stae data bus lines. Used for data transfer between the MPU and the LCM. These four are not used during 4-bit operation.” What does this mean, and in general how do the Tx/Rx lines work with the data bus?
Also, how did you embed the above image? I haven’t been able to do it.
Jack
Hello Jack,
Here, the use of the TX2/RX2 pins has nothing to do with UART2 hardware serial interface. I could have picked any other pin! I used them because it was more convenient to wire things like this.
Here’s a short article that should help you better understand how the MCU communicates with the LCD screen.
Difference between interfacing character 16×2 lcd in 4-bit and 8-bit mode with microcontroller
If you take a look at the source code of the library I used, you will see how the bit shifting works:
void LiquidCrystal::write4bits(uint8_t value) { for (int i = 0; i < 4; i++) { digitalWrite(_data_pins[i], (value >> i) & 0x01); } pulseEnable(); }
This is the basic library provided by Arduino. The code could be further optimized… I suppose there are some more efficient ones (but I didn’t look for them).
Finally, to add an image in a post, I use the media hosting service imgur.com. And this is how I proceed:
Sorry, my user interface is French… Once the image has been uploaded to imgur.com, I right-click on the image and choose the Copy image address option to capture the hosting URL of the image.
If the width of the image is larger than 680 pixels, you have to set the width to 680 when specifying the image dimensions in your post. The height will be recalculated automatically to respect the original aspect ratio of the image.
I hope I’ve answered all your questions 🙂
Steph
Just to show you that the pins don’t matter on the ESP32 side. I modified my wiring by connecting the RS and E pins of the LCD screen as follows:
LCD | ESP32 -----+------- RS | GPIO 2 E | GPIO 4
I also slightly modified the code to redefine these 2 pins and add a small animation :
#include <Arduino.h> #include <Wire.h> #include <LiquidCrystal.h> // #define LCD_RS 16 // #define LCD_EN 17 #define LCD_RS 2 #define LCD_EN 4 #define LCD_D4 5 #define LCD_D5 18 #define LCD_D6 19 #define LCD_D7 21 LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7); void setup() { lcd.begin(16, 2); lcd.home(); lcd.print("Hello Jack!"); lcd.setCursor(0, 1); lcd.print("New wiring..."); } void loop() { lcd.setCursor(15, 1); lcd.print(char(millis() % 1000 < 500 ? 219 : 32)); }
Here’s what you get:
It may happen that some pins in the ESP32 may cause a conflict at boot time after uploading your code and prevent the MCU from booting normally (I suspect pin 5 here). Simply press the EN button on the board to force a reset.
I also found a library that looks better than Arduino’s, if you want to try it:
New LiquidCrystal (by Francisco Malpartida)
You’ll have to download it by hand if you use the Arduino IDE.
However, it is directly available in PlatformIO IDE.
But it’s not necessary. My code works fine with the default Arduino library.
Thank you, Jack.
I’m glad things are clearer and you can move forward with your project.
Now that you know how to add images to your posts, we’re hoping you’ll be able to show us your project 😉