Esp32

I2C LCD with ESP32 using MicroPython and Thonny IDE

ESP32 is a microcontroller board which can interface multiple hardware and sensors. LCD and OLED are great ways of displaying and giving a graphical representation to the required output. This tutorial covers LCD display interfacing with ESP32 boards using MicroPython in Thonny IDE.

What is MicroPython

MicroPython is written in C and it’s a software implementation for Python 3 mainly targeting embedded system applications. However, it cannot fully run the python 3 standard libraries. MicroPython is a variant of Python and specifically designed for embedded systems. Today we will be implementing MicroPython on an ESP32 board using Thonny IDE.

Before we move forward make sure to install Thonny IDE to program the ESP32 board. To download Thonny IDE click here.

What is Liquid Crystal Display (LCD)

LCD or Liquid Crystal Display is a device that operates by applying a variable voltage to a liquid crystal layer which induces changes in optical properties of LCD.

Today we will be covering a 16×2 LCD with ESP32 board, however other sizes of LCD should also work. LCDs having size of 16 cross 2 can display 16 characters inside a single line and a total of two lines are there in this specific model. LCD contains alphanumeric dot matrix display which can display total of 224 characters.

We can also use this LCD directly with ESP32 like we did with Arduino however it requires a tense amount of hard work and one must deal with a bunch of wires. To avoid this, we will be using an I2C module with LCD which only requires two wires to operate that are SDA and SCL.

Additionally, I2C LCD comes with a potentiometer which can adjust LCD brightness without any need of external variable resistor to control LCD brightness.

I2C PIN Function
GND Ground Pin
VCC Power Pin
SDA Pin used for data exchange
SCL Pin used for the synchronous clock

 Parts Required to Interface LCD with ESP32

Before we move further, we recommend you collect the following parts for this tutorial:

  • ESP32 DOIT DEVKIT WROOM
  • 16X2 LCD
  • I2C Serial Interface
  • Breadboard
  • Jumper Wires

Wiring the LCD to the ESP32

Wiring an LCD with ESP32 is simple, just connect the SDA and SCL pin on GPIO pin 21 and 22 respectively.

Following is the pin configuration for reference:

Preparing Thonny IDE for ESP32

As hardware is ready to take the code now open Thonny IDE and upload the code. Before interfacing any I2C device we must know the I2C address of that respective device. I2C devices feature a unique address for them. For many devices the default address for I2C is 0x27 where 0x shows the hex format of the number.

It’s recommended to get the I2C address every time while interfacing a new device.

Getting the LCD Address

Connect ESP32 board with PC and open Thonny IDE. Type the below given code in the Thonny IDE editor. Upload the code in the ESP32 board using the play button at top.

Code

This code is written for Pin 21 (SDA) and Pin 22 (SCL). If you are using any other board like ESP8266 replace the Pin number accordingly.

import machine

sdaPIN=machine.Pin(21) #for ESP32
sclPIN=machine.Pin(22)

i2c=machine.I2C(sda=sdaPIN, scl=sclPIN, freq=10000)

devices = i2c.scan()
if len(devices) == 0:
print("No i2c device !")
else:
print('i2c devices found:',len(devices))
for device in devices:
print("At address: ",hex(device))

Output

Following output will appear in the shell terminal here we can see the I2C address for LCD is 0x27.

I2C LCD MicroPython Libraries

To interface ESP32 with I2C LCD two different MicroPython libraries are required. We will use these two libraries: lcd_api.py and i2c_lcd.py. Copy these two libraries from the given link and create two new files inside Thonny IDE. Save both of these libraries in ESP32 or respective boards as shown in image below.

Step1: Create two new files and paste both libraries code separately. To save it in ESP32 board Go to: File>Save as 

Step2: A popup will appear select MicroPython device. Make sure the ESP32 board is connected to the PC.

Step3: Write the libraries file name and click OK.

Note: Remember, keep the same exact name as lcd_api.py and i2c_lcd.py.

Save the two library files and create a new file with name main.py where we will be writing main MicroPython code for I2C LCD.

Step4: Once all three files are created, we can see them in the Thonny IDE editor. We can close the libraries file and just open main.py to write and upload I2C LCD code.

Display Text on LCD

Write the code given below in Thonny IDE and click the mentioned play button to upload code to ESP32 board.

Code

Writing the given code in the Thonny IDE editor window will help to interface I2C LCD with ESP32 board.

import machine
from machine import Pin, SoftI2C
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
from time import sleep
I2C_ADDR = 0x27
totalRows = 2
totalColumns = 16
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000) #I2C for ESP32
#i2c = I2C(scl=Pin(5), sda=Pin(4), freq=10000) #I2C for ESP8266
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
while True:
lcd.putstr("Linuxhint.com")
sleep(10)
lcd.clear()

The code above started by calling SoftI2C and Pin class from the machine module. We also have added a sleep module to give delay. Next, we called the two important libraries lcd_api.py and i2c_lcd.py we just saved inside the ESP32 folder.

Next I2C address 0x27 is defined after that we define the total rows and columns inside the LCD here in our case it’s 16×2. If using any other size of screen, change the rows and columns.

To display the text a string Linuxhint.com is defined.

Output

In the output we can see the string defined on the LCD screen.

Conclusion

MicroPython is a language designed specifically for embedded system. Using two libraries lcd_api.py and i2c_lcd.py we can easily interface the I2C LCD with ESP32. For programming ESP32 with MicroPython, Thonny IDE is used. This article is a guide on interfacing LCD with ESP32.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.