Raspberry Display Hello world with LCD
After adding the library file, let's write a simple program to let the LCD display "Hello, world!".
Hardware Connection
In this project, the electronic hardware we need to use is as follows:
• Raspberry Pi Pico
• Grove Shield for Pi Pico
• Grove - 16 x 2 LCD
Plug the Pico and the Shield, use a Grove data cable to connect the LCD to I2C1.
Write a Program
First, define the function libraries that we need to use, including the third-party function library lcd1602 that has just been saved in the Pico.
1 from lcd160
2 import LCD1602 2 from machine import I2C, Pin
3 from utime import sleep
The functions available in the lcd library are as follows:
• display() — Enable the display.
• no_display() — Disable the display.
• clear() — Clear the current display and return the cursor.
• setCursor(col, row) — Set the display position, “col" is the number of columns, “row" is the number of rows.
• print(text) — Display characters.
Next, define the pin. Unlike other pin definitions, pin I2C needs to define data lines SCL and SDA respectively.
1 i2c = 12C(1, scl=Pin(7), sda=Pin(6), freq=400000)
It is written as “machine.I2C(id,*,scl,sda,freq=400000)", where:
• “id" identifies a specific I2C peripheral, and its allowed value depends on the specific port/ board.
• “scl" uses the Pin function to specify the pin used for SCL.
• “sda" uses the Pin function to specify the pin used for SDA.
• “freq" is the integer of the maximum frequency of SCL, which is generally adjusted according to the different devices used. Here we set it to 40000.
Finally, use the functions in the library to display “Hello, World" on the LCD, and the program is completed. The complete program is as follows:
1 from lcd1602 import LCD1602
2 from machine import I2C, Pin
3 from utime import sleep
4 i2c = I2C(1, scl=Pin(7), sda=Pin(6), freq=400000)
5 d = LCD1602(i2c, 2, 16) #label data type, number of lines and characters per line
6
7 d.display ( ) #enable display
8 sleep(1)
9 d.clear()#clear display
10 d.print('Hello ') #display characters
11
12 sleep(1)
13 d.setCursor(0, 1)#set display position, row and column start from o
14 d.print('world ').
Use a USB cable to connect Pico to the computer, click the “run" button to save the program to any location, you can see the program in action.