Interfacing I2C LCD with ESP32 | ESP32 I2C LCD Tutorial - JohnHau/mis GitHub Wiki

https://www.electronicshub.org/esp32-i2c-lcd/

February 27, 2021 By Ravi Teja In this project, we will learn how to interface a PCF8574 I2C LCD with ESP32 Development Board. We will see how to configure the I2C Pins in ESP32, download necessary libraries for Arduino IDE, understand how ESP32 I2C LCD interface works and display some data on the 16×2 LCD.

If you want to connect I2C LCD with ESP8266 NodeMCU board, then I made a separate tutorial just for that. Check it out.

image

Outline Introduction A Brief Note on PCF8574 I2C LCD Module ESP32 I2C LCD Interface Components Required Circuit Diagram Preparing Arduino IDE Finding the Slave Address of I2C LCD Module Displaying Simple Text Code Displaying ADC Result on LCD Circuit Diagram Code Conclusion

Introduction There are many display devices like OLED Display, 128×64 Graphical Display, Nokia 5110 Display etc. But for displaying simple text and number, all we need is a 16×2 or 20×4 Character Display.

The main disadvantage of 16×2 LCD Display is it requires at least 6 GPIO pins of a Microcontroller to function properly. This is not even the 8-bit mode but reduced 4-bit mode (add another 4 GPIO pins for 8-bit mode).

Using that many pins of a microcontroller just for a simple character LCD Display is not desirable especially if you are designing a project with lot of sensors and devices connected to the microcontroller.

The PCF8574 I2C LCD Module is designed to overcome this drawback.

A Brief Note on PCF8574 I2C LCD Module As the name suggests, the PCF8574 I2C LCD Module is a PCF8574 IC based module designed to convert regular 16×2 (or 20×4) character LCD Displays in to I2C based devices i.e., instead of using 8-bit or 4-bit parallel interface to send data, we can use I2C bus to send data to the display.

image

If you notice the board layout and pins of this I2C LCD Module, you can observe that it is a modified version of the regular IO Expander Module, which is also based on PCF8574 IC.

This I2C LCD Module includes the POT to adjust the contrast of the LCD and pull-up resistors for SDA and SCL lines. So, you don’t need any additional connections.

image

Just plugin the I2C LCD Module at the back of the 16×2 LCD and make connections between the microcontroller and the I2C LCD Module. That’s it.

ESP32 I2C LCD Interface The first thing we need to figure out for ESP32 I2C LCD Interface to work is the I2C pins of ESP32. If you remember, in the ESP32 Pinout Tutorial, I mentioned that the ESP32 Microcontroller has two I2C interfaces and any GPIO pin can be configured to be used as an I2C Pin.

image

But after using all the GPIO pins for various purposes like SPI, UART, ADC etc., GPIO 22 and GPIO 21 are free (without any important alternative functions). So, in this project, we will be using GPIO 22 as SCL and GPIO 21 as SDA of the I2C interface.

Another important point to note is that the 16×2 LCD runs on standard TTL voltage levels i.e., 4.7V to 5.3V. We cannot apply 3.3V as VCC to the I2C LCD Module even though the PCF8574 IC works at this voltage.

So, keeping this in mind, the following table shows all the connections between I2C LCD Module and ESP32

image

As you can see from the table, the VCC of the I2C LCD Module is connected to VIN pin of the ESP32 Development Board. This is connected to USB Supply when ESP32 is powered through USB. So, we will get around 5V on this pin.

image

Circuit Diagram The following image shows the circuit diagram for demonstrating ESP32 I2C LCD Interface.

image

Preparing Arduino IDE You have to download one library called ‘LiquidCrystal_I2C’ in order to work with I2C LCD Module. If you already installed this library, you can skip this step. If not, then follow along. Open Arduino IDE and open the library manager by selecting Tools -> Manage Libraries. . .

image

image

image

Finding the Slave Address of I2C LCD Module Since we are working with I2C interface, the slave address is one of the important things we need to be aware of. Here, the ESP32 Microcontroller is the master and the PCF8574 I2C LCD Module is the slave.

You can refer to the datasheet of PCF8574 IC and calculate the slave address but for now we will go with the easy way i.e., a small program which determines the slave address automatically.

In fact, we used the same code with Arduino and ESP8266 for other I2C related projects. After making all the necessary connections between ESP32 and I2C LCD Module, upload the following code and open the serial monitor of Arduino IDE.

#include <Wire.h>

void setup() { Wire.begin(); Serial.begin(115200); while (!Serial); }

void loop() { byte error, address; int I2CDevices;

Serial.println("Scanning for I2C Devices…");

I2CDevices = 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.print(address, HEX);
  Serial.println(" !");

  I2CDevices++;
}
else if (error == 4)
{
  Serial.print("Unknown error at address 0x");
  if (address < 16)
  {
    Serial.print("0");
  }
  Serial.println(address, HEX);
}

} if (I2CDevices == 0) { Serial.println("No I2C devices found\n"); } else { Serial.println("****\n"); } delay(5000); }

image

Displaying Simple Text Let us use this slave address and display some basic text on the 16×2 LCD display using I2C LCD Module. Use the following code and see the output.

Code #include <Wire.h> #include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 16, 2);

void setup() { lcd.init(); lcd.backlight(); lcd.setCursor(0,0); lcd.print(" I2C LCD with "); lcd.setCursor(0,1); lcd.print(" ESP32 DevKit "); //delay(2000); }

void loop() {

}

image

Displaying ADC Result on LCD For the next code, let us make a small circuit involving a potentiometer and ADC of the ESP32. I connected a 10 KΩ POT to ADC 2 Channel 3 of ESP32. It is associated with GPIO 15 Pin (D15 on ESP32 DevKit Board).

In the code, I configured GPIO 15 as Analog Input pin and the displayed the result of ADC on the LCD.

Circuit Diagram The following image shows the circuit diagram for connecting 10 KΩ Potentiometer to ESP32 Development Board and displaying the result of ADC on the I2C LCD.

image

Code #include <Wire.h> #include <LiquidCrystal_I2C.h> const int potPin = 15;

// variable for storing the potentiometer value int potValue = 0; LiquidCrystal_I2C lcd(0x3F, 16, 2);

void setup() { lcd.init(); lcd.backlight(); lcd.setCursor(0,0); lcd.print(" I2C LCD with "); lcd.setCursor(0,1); lcd.print(" ESP32 DevKit "); delay(2000); lcd.clear(); lcd.setCursor(0,0); lcd.print("ADC Value ="); }

void loop() { potValue = analogRead(potPin); lcd.setCursor(12,0); lcd.print(potValue); delay(1000); }

Conclusion A simple tutorial on interfacing PCF8574 I2C LCD Module with ESP32 Development Board. You learned the importance of I2C LCD Module, I2C pins on ESP32, how the ESP32 I2C LCD Interface works and how to display some text on the LCD.

You can even display some special characters or make a scrolling text.

⚠️ **GitHub.com Fallback** ⚠️