To control an LCD with a microcontroller as the Raspberry Pi Pico can be a quite complicated job. Well, if your display is equipped with an IC2 module and specific MicroPython libraries are available, it’s not that difficult to connect to the display to the Raspberry Pi Pico. Learn with this tutorial how to connect and to program an 1602 LCD with a Raspberry Pi Pico.
There are many types of LCD displays. In this tutorial we are using the popular and affordable 1602 LCD. The LCD has an IC2 module soldered on it (see the pictures below). If your LCD is of the same type, but has a different size, it won’t be a problem to continue with this tutorial. You’ll just have to correct some parameters in the MicroPython script. But if it is from a different type or it has no I2C module, you better look for another tutorial.
- Prepare the hardware
– First you need a computer to run Thonny. In this tutorial we’ll use a Raspberry Pi 4 as our computer. And Thonny is a user-friendly Python IDE to interact with the Raspberry Pi Pico board. If you never used Thonny to program the Raspberry Pi Pico, before continuing, you better have a look at our tutorial “How to start programming the Raspberry Pi Pico“.
– Next you need an USB cable with micro-USB plug.
– You also need a Raspberry Pi Pico of course. And as we’ll connect another device to the Pico, we need pin headers soldered to the GPIO-pins of our board.
And finally you’ll need some extra components :
– a breadboard (we are using a 400 points breadboard)
– a 16×2 or 1602 LCD with I2C bus module
– Dupont jumper wires
Visit our shop if you miss any components.
- Get to know the 1602 LCD
In this tutorial we are using the popular and quite basic 16×2 or 1602 LCD. It can display 16 characters per line on 2 lines. Each character is made from a matrix with 5×7 dots. It is equipped with a backlight for easy reading. Besides sending text, thanks to specific commands, we can give instructions to the display, as to switch on/off the backlight for example.
The display we use in this tutorial is equipped with a I2C-module (black part on the picture below). I2C is a communication protocol which allows an easier connection between the display and the Raspberry Pi Pico. Indeed, instead of having to wire all the pins on the top of the screen, we only have to connect the display with 4 wires to our Raspberry Pi Pico.
Each I2C device has its own I2C address. Often this address is hard-wired in the device and will vary from manufacturer to manufacturer. Have a look at our tutorial ‘Find out an I2C address with the Raspberry Pi Pico‘ to get the very basics of I2C.
If you bought one of our kits, the hexadecimal address of the LCD is ‘0x27’. And if you don’t know the address, you can find it out with the help of our tutorial ‘Find out an I2C address with the Raspberry Pi Pico‘. We will need the I2C address from the display to insert it in our MicroPython code.
If you want more information regarding LCD, have a look at the ‘Liquid-crystal display’ webpage of Wikipedia.
Be careful ! Before starting to connect components to the GPIO pins of your Raspberry Pi Pico, make sure it is not connected to your computer.
- Setup the hardware part
– connect the GND of the display to a GND (ground) pin (black cable)
– connect the VCC pin of the display with a 5V pin (red wire)
– connect the SDA pin of the display to GP0 (grey wire)
– connect the SDC pin of the display to GP1 (yellow wire)
- Write the code
The aim here is to get a basic message displayed on our LCD.
Install the Library first
To avoid extensive and complicated code writing, libraries are often used. For our LCD, we will also be using a library. We found the most appropriate library at GitHub from Tyler Peppy. And he based his work on the Python library created by Dave Hylands. As these files from this quite specific library don’t come automatically with MicroPython, we have to install them ourselves.
So, before writing the code, we’ll have to upload the files to our Raspberry Pi Pico. You can download a ZIP-folder containing the 2 files to be installed here.
Once downloaded and unzipped on our computer, we upload these files to our Raspberry Pi Pico. If you don’t know how to do that, have a look at our tutorial ‘Transfer files between computer and Raspberry Pi Pico‘. If you have multiple folders on your Raspberry Pi Pico, make sure you upload them in the same folder as the new file we will create for our main code. And don’t change the filenames of the library of course.
OK, now the library is uploaded to our Raspberry Pi Pico, write or paste following code in the IDE:
from machine import Pin, I2C
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
I2C_ADDR = 0x27
I2C_NUM_ROWS = 2
I2C_NUM_COLS = 16
i2c = I2C(0, sda=machine.Pin(0), scl=machine.Pin(1), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
lcd.putstr("Great! It Works!")
Some explanations about the code :
from machine import Pin, I2C: to partially import the machine module to have access to the GPIO pins and the I2C module.
from lcd_api import LcdApi: to import the first file from the LCD library.
from pico_i2c_lcd import I2cLcd: to import the second file from the LCD library.
I2C_ADDR = 0x27: set the I2C address of the display. Change the I2C address if necessary!
I2C_NUM_ROWS = 2: Our display has 2 rows. Change if your display has another size.
2C_NUM_COLS = 16: Our display has 16 colums. Change if your display has another size.
i2c = I2C(0, sda=machine.Pin(0), scl=machine.Pin(1), freq=400000): to create an I2C object. See the MycroPython documentation for more information.
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS): to define our LCD.
In the next lines we send characters and give instructions to the display. Feel free to change and to experiment yourself.
lcd.backlight_on(): to make sure the backlight is on.
lcd.putstr("Great! It Works!"): to send text to the display.
lcd.move_to(3,1): to change the position of the cursor (x,y). x :shift horizontally and y : shift vertically.
lcd.putstr("freva.com"): sending new text, starting from the new cursor position.
- Run your script
Now, it’s time to save your script. Just make sure you save this MicroPython file in the same folder as the 2 files from the library you uploaded earlier.
And before running the script, it’s important to adjust the contrast of your LCD. If the contrast isn’t adjusted well, it’s possible you don’t see appearing anything. You can adjust it by turning with a small screwdriver at the blue potentiometer at the back of your LCD (see the pictures here above). Make sure the backlight of the display is on to see the result. If the LCD’s contrast is adjusted right, you can just see the darker rectangles for the characters appear.
Then, click on the Run button of the Thonny IDE. If you did everything well, the text as in the picture below should appear.
- Experiment with additional instructions and characters
Besides the commands we used in the last lines of our script, there are more possibilities to communicate with the LCD. If you want to learn more about, have a look at this Github webpage.
Congratulations! With this setup you can integrate a display in your Raspberry Pi Pico projects now. Have fun with it!
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?