ESP32 MicroPython LCD 20x4

This tutorial will guide you through using a 20x4 LCD I2C display with an ESP32 board, using MicroPython. You'll learn:

ESP32 MicroPython and LCD I2C 20x4

Hardware Preparation

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×LCD 20x4
1×Jumper Wires
1×Breadboard
1×(Recommended) Screw Terminal Expansion Board for ESP32
1×(Recommended) Power Splitter For ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of LCD I2C 20x4

Pinout

The LCD 20x4 I2C connects using the I2C interface and has 4 pins.

  • GND pin: Connect this to GND (0 volts).
  • VCC pin: Connect this to VCC (5 volts) to power the LCD.
  • SDA pin: Used for I2C data signal.
  • SCL pin: Used for I2C clock signal.
LCD 20x4 I2C Pinout

LCD Coordinate

The LCD I2C 20x4 includes 20 columns and 4 rows. Both columns and rows begin at 0.

ESP32 MicroPython LCD I2C Coordinate

Wiring Diagram

  • How to connect ESP32 and lcd 20x4 using breadboard (powered via USB cable)
The wiring diagram between ESP32 MicroPython LCD 20x4 I2C

This image is created using Fritzing. Click to enlarge image

  • How to connect ESP32 and lcd 20x4 using breadboard (powered via Vin pin)
The wiring diagram between ESP32 MicroPython LCD 20x4 I2C

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and lcd 20x4
How to wire ESP32 and lcd 20x4
LCD I2C ESP32
VCC Vin 5V
GND GND
SDA GPIO21 (SDA)
SCL GPIO22 (SCL)

ESP32 MicroPython Code

The below MicroPython code displays string, integer and float number on LCD 20x4 display.

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-lcd-20x4 """ from machine import I2C, Pin from DIYables_MicroPython_LCD_I2C import LCD_I2C import utime # The I2C address of your LCD (Update if different) I2C_ADDR = 0x27 # Use the address found using the I2C scanner # Define the number of rows and columns on your LCD LCD_COLS = 20 LCD_ROWS = 4 # Initialize I2C i2c = I2C(1, scl=Pin(22), sda=Pin(21), freq=400000) # Initialize LCD lcd = LCD_I2C(i2c, I2C_ADDR, LCD_ROWS, LCD_COLS) # Setup function lcd.backlight_on() lcd.clear() # Main loop function while True: lcd.clear() lcd.set_cursor(0, 0); # Move cursor at the first row, first column lcd.print("LCD 20x4"); # Display text at the first row lcd.set_cursor(0, 1); # Move cursor at the second row, first column lcd.print("I2C Address: 0x27"); # Display text at the second row lcd.set_cursor(0, 2); # Move cursor at the third row, first column lcd.print("DIYables"); # Display text at the third row lcd.set_cursor(0, 3); # Move cursor at the fourth row, first column lcd.print("www.diyables.io"); # Display text at the fourth row utime.sleep(2) lcd.clear() lcd.set_cursor(0, 0) # Move to the beginning of the first row lcd.print("Int: ") lcd.print(str(197)) # Print integer lcd.set_cursor(0, 1) # Move to the beginning of the second row lcd.print("Float: ") lcd.print(str(26.39)) # Print float utime.sleep(2)

Detailed Instructions

Here’s instructions on how to set up and run your MicroPython code on the ESP32 using Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Confirm that MicroPython firmware is loaded on your ESP32 board.
  • If this is your first time using an ESP32 with MicroPython, check out the ESP32 MicroPython Getting Started guide for step-by-step instructions.
  • Connect the LCD 20x4 display to the ESP32 according to the provided diagram.
  • Connect the ESP32 board to your computer with a USB cable.
  • Open Thonny IDE on your computer.
  • In Thonny IDE, go to Tools Options.
  • Under the Interpreter tab, choose MicroPython (ESP32) from the dropdown menu.
  • Make sure the correct port is selected. Thonny IDE usually detects it automatically, but you might need to select it manually (like COM12 on Windows or /dev/ttyACM0 on Linux).
  • Navigate to the Tools Manage packages on the Thonny IDE.
  • Search “DIYables-MicroPython-LCD-I2C”, then find the LCD I2C library created by DIYables.
  • Click on DIYables-MicroPython-LCD-I2C, then click Install button to install LCD I2C library.
ESP32 MicroPython LCD I2C library
  • Copy the above provided code and paste it to the Thonny IDE's editor.
  • Save the script to your ESP32 board by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and MicroPython device. Select MicroPython device
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Check out the result on the LCD 20x4 display.
ESP32 MicroPython 20x4 LCD

ESP32 - 20x4 LCD - Custom Character

It is similar to LCD 16x2, you can refer to the custom character section on ESP32 MicroPython LCD I2C tutorial, and then adapt it for LCD 20x4 display

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!