1602A LCD and I²C module are quite cheaper and easy to setup and code. Earlier, we have published guides on how to wire and setup 1602A LCD with Arduino UNO (which have not used the I²C module) which occupies 8 Pins on your Arduino. We have also explained about different protocols, interfaces like I²C, Serial Peripheral Interface and discussed interfacing 1602A LCD display using I²C Serial Interface.
The commonly sold 1602A LCD blue background, white text is cheap China clone of Hitachi’s model. China company named YwRobot manufactures somewhat better finishing I²C modules (YwRobot is popular for breadboard power supply module). Usually the I²C module is soldered on the back of 1602A LCD and sold. The I²C module has PCF8574 IC which provides general-purpose remote I/O expansion via the two-wire bidirectional I²C-bus serial clock (SCL) and serial data (SDA). PCF8574 is very useful IC and has wider usage in day to day commercial electronics such as LED signs boards.
The hardware making the connection just easy (2 data pins and 2 power pins) :
---
Module’s GND will connect to ESP32’s GND
Module’s 3v/VCC will connect to ESP32’s VCC
Module’s SDA will connect to ESP32’s pin 21
Module’s SCL will connect to ESP32’s pin 22
We actually need 5V supply for the I²C module to display properly. ESP32 gives 3.3 volts. So, use external 5V supply (that is why we linked to the breadboard power supply in above paragraph, also we can use computer SMPS for regulated voltage). We need to common the GND of external power supply, ESP32’s GND and module’s GND to complete the circuit. The external power supply thing is easy but takes time to setup. 3.3v someway works for testing.
The module has a built-in potentiometer, you can use it to adjust the contrast between the background and the characters on the LCD.
You can try this code for quick display testing :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <Wire.h> #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x3F,16,2); // LCD address is 0x3F, 16 is chars, 2 is 2 line display void setup() { lcd.init(); lcd.backlight(); lcd.setCursor(0,0); lcd.print("Hello world"); lcd.setCursor(1,0); lcd.print("It Works!"); } void loop() { } |
As you are using ESP32, you should learn a bit more than the Arduino UNO users. The I²C controller has inbuilt address bit (which is used to control I²C bus). The default address is 0x27 or 0x3f. We used 0x3F in the above code.
The technically correct method is to check the address of the I²C controller using some code like the below (and Arduino IDE’s open serial monitor to check) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <Wire.h> void setup() { Serial.begin (9600); Serial.println ("Scanning I2C device... Wire.begin(); for (byte i = 0; i <50; i++) { Wire.beginTransmission (i); if (Wire.endTransmission () == 0) { Serial.print ("Address found->"); Serial.print (" (0x"); Serial.print (i, HEX); Serial.println (")"); Count++; } Serial.print ("Found "); Serial.print (count, DEC); Serial.println (" device"); } void loop() {} |
You can see, how much ESP32 makes the coding part easier. We needed no pin numbers to mention. ESP32’s Pin 21
is SDA pin and Pin 22 is SCL.