Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Lesson 26: I2C LCD 1602
In this lesson, you will learn to connect an I2C LCD 1602 display to a Raspberry Pi Pico W. You’ll understand how to set up I2C communication, display and clear messages on the LCD using MicroPython.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
Universal Maker Sensor Kit |
94 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
Raspberry Pi Pico W |
|
Wiring
Note
To ensure the LCD module operates normally, please power it using the VBUS pin on the Pico.
Code
Note
Open the
26_lcd1602_module.pyfile under the path ofuniversal-maker-sensor-kit-main/pico/Lesson_26_I2C_LCD1602_Moduleor copy this code into Thonny, then click “Run Current Script” or simply press F5 to run it. For detailed tutorials, please refer to Open and Run Code Directly.Here you need to use the
lcd1602.py, please check if it has been uploaded to Pico W, for a detailed tutorial refer to Upload the Libraries to Pico.Don’t forget to click on the “MicroPython (Raspberry Pi Pico)” interpreter in the bottom right corner.
from machine import I2C, Pin
from lcd1602 import LCD
import time
# Initialize I2C communication;
# Set SDA to pin 20, SCL to pin 21, and frequency to 400kHz
i2c = I2C(0, sda=Pin(20), scl=Pin(21), freq=400000)
# Create an LCD object for interfacing with the LCD1602 display
lcd = LCD(i2c)
# Display the first message on the LCD
# Use '\n' to create a new line.
string = "SunFounder\n LCD Tutorial"
lcd.message(string)
# Wait for 2 seconds
time.sleep(2)
# Clear the display
lcd.clear()
# Display the second message on the LCD
string = "Hello\n World!"
lcd.message(string)
# Wait for 5 seconds
time.sleep(5)
# Clear the display before exiting
lcd.clear()
Code Analysis
Setting up I2C Communication
The
machinemodule is used to set up I2C communication. SDA (Serial Data) and SCL (Serial Clock) pins are defined (pin 20 and 21 respectively), along with the I2C frequency (400kHz).from machine import I2C, Pin i2c = I2C(0, sda=Pin(20), scl=Pin(21), freq=400000)
Initializing the LCD Display
The
LCDclass from thelcd1602module is instantiated. This class handles the communication with the LCD display through I2C. AnLCDobject is created using thei2cobject.For more usage of the
lcd1602library, please refer tolcd1602.py.from lcd1602 import LCD lcd = LCD(i2c)
Displaying Messages on the LCD
The
messagemethod of theLCDobject is used to display text on the screen. The\ncharacter creates a new line on the LCD. Thetime.sleep()function pauses execution for a specified number of seconds.string = "SunFounder\n LCD Tutorial" lcd.message(string) time.sleep(2) lcd.clear()
Clearing the Display
The
clearmethod of theLCDobject is called to clear the text from the display.lcd.clear()
Displaying a Second Message
A new message is displayed, followed by a delay and then clearing the screen again.
string = "Hello\n World!" lcd.message(string) time.sleep(5) lcd.clear()