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:
- How to connect a 20x4 LCD I2C to an ESP32
- How to write MicroPython code to display text, integers, floating-point numbers, and custom characters on a 20x4 LCD I2C
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 |
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
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 Coordinate
The LCD I2C 20x4 includes 20 columns and 4 rows. Both columns and rows begin at 0.
Wiring Diagram
- How to connect ESP32 and lcd 20x4 using breadboard (powered via USB cable)
This image is created using Fritzing. Click to enlarge image
- How to connect ESP32 and lcd 20x4 using breadboard (powered via Vin pin)
This image is created using Fritzing. Click to enlarge image
- How to connect ESP32 and lcd 20x4 using screw terminal block breakout board (powered via USB cable)
- How to connect ESP32 and lcd 20x4 using screw terminal block breakout board (powered via Vin pin)
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.
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.
- 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 - 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