Raspberry Pi Pico - LCD I2C
This guide shows you how to connect a 16x2 LCD with an I2C interface to a Raspberry Pi Pico. We will cover:
- How to connect an LCD I2C with Raspberry Pi Pico
- How to program Raspberry Pi Pico to show text on LCD I2C
- How to program Raspberry Pi Pico to show numbers on LCD I2C
- How to program Raspberry Pi Pico to show custom characters on LCD I2C
Hardware Preparation
1 | × | Raspberry Pi Pico W | |
1 | × | Raspberry Pi Pico (Alternatively) | |
1 | × | Micro USB Cable | |
1 | × | LCD I2C | |
1 | × | Jumper Wires | |
1 | × | Breadboard | |
1 | × | (Optional) Screw Terminal Expansion Board for Raspberry Pi Pico |
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 16x2
The 16x2 LCD I2C is a screen with 16 columns and 2 rows. It has an I2C interface and comes with a potentiometer to adjust the screen's contrast.
Pinout
The LCD I2C uses an I2C interface and includes 4 pins.
- GND pin: connect to GND (0 volts).
- VCC pin: connect to VCC for power (5 volts).
- SDA pin: transfers I2C data signal.
- SCL pin: transfers I2C clock signal.
LCD Coordinate
The LCD I2C 16x2 includes 16 columns and 2 rows. The numbering of both columns and rows begins from 0.
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
Raspberry Pi Pico Code
Detailed Instructions
Please follow these instructions step by step:
- Ensure that Thonny IDE is installed on your computer.
- Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
- If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
- Connect the LCD I2C display to the Raspberry Pi Pico according to the provided diagram.
- Connect the Raspberry Pi Pico to your computer using a USB cable.
- Launch the Thonny IDE on your computer.
- On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
- In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
- Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 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 below code and paste it to the Thonny IDE's editor.
- Save the script to your Raspberry Pi Pico by:
- Click the Save button, or use Ctrl+S keys.
- In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
- 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 display.
Video Tutorial
Do More with LCD
Custom Character
To display special characters or symbols such as a heart or an angry bird on an LCD, you need to use the character generator. This is because the lcd.print() function can only show ASCII characters.
The LCD 16x2 can display 32 characters, with 16 positions per line over 2 lines. Each character is made of 40 small squares, set up in 8 rows and 5 columns.
The character generator makes a character (40 pixels). You just need to follow these steps:
Result shown on the screen:
Multiple custom characters
We can create up to 8 special characters (from 0 to 7). Here is how to make and show three of these characters.
Result shown on LCD screen:
Summary: how to use custom character on LCD
- Use the tool above to create binary code for your special character.
- Write down the binary code of your character (as copied from the step above).
- Create a unique character and position it at a number from 0 to 7
- Show a unique symbol on the screen any time you choose.
Troubleshooting on LCD I2C
If the text is not showing on the LCD I2C, please check these problems:
- Adjust the LCD screen brightness by rotating the potentiometer located on the back.
- The I2C address for the LCD can differ between manufacturers. It typically is either 0x27 or 0x3F. Try these addresses one at a time. If they do not work, use this code to find the right I2C address.
The result shown in the Shell at the bottom of Thonny: