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!
3.4 Liquid Crystal Display¶
LCD1602 is a character type liquid crystal display, which can display 32 (16*2) characters at the same time.
As we all know, though LCD and some other displays greatly enrich the man-machine interaction, they share a common weakness. When they are connected to a controller, multiple IOs will be occupied of the controller which has no so many outer ports. Also it restricts other functions of the controller. Therefore, LCD1602 with an I2C bus is developed to solve the problem.
Here we will use the I2C0 interface to control the LCD1602 and display text.
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 |
---|---|---|
Kepler Kit |
450+ |
You can also buy them separately from the links below.
SN |
COMPONENT |
QUANTITY |
LINK |
---|---|---|---|
1 |
1 |
||
2 |
Micro USB Cable |
1 |
|
3 |
1 |
||
4 |
Several |
||
5 |
1 |
Schematic
Wiring
Code
Note
Open the
3.4_liquid_crystal_display.py
file under the path ofkepler-kit-main/micropython
or copy this code into Thonny, then click “Run Current Script” or simply press F5 to run it.Don’t forget to click on the “MicroPython (Raspberry Pi Pico)” interpreter in the bottom right corner.
For detailed tutorials, please refer to Open and Run Code Directly.
Here you need to use the library called
lcd1602.py
, please check if it has been uploaded to Pico W, for a detailed tutorial refer to 1.4 Upload the Libraries to Pico.
from machine import I2C, Pin
from lcd1602 import LCD
import time
# Initialize I2C communication;
i2c = I2C(1, sda=Pin(6), scl=Pin(7), 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()
After the program runs, you will be able to see two lines of text appear on the LCD in turn, and then disappear.
Note
When the code is running, if the screen is blank, you can turn the potentiometer on the back to increase the contrast.
How it works?
Setting up I2C Communication
The
machine
module 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(1, sda=Pin(6), scl=Pin(7), freq=400000)
Initializing the LCD Display
The
LCD
class from thelcd1602
module is instantiated. This class handles the communication with the LCD display through I2C. AnLCD
object is created using thei2c
object.For more usage of the
lcd1602
library, please refer tolcd1602.py
.from lcd1602 import LCD lcd = LCD(i2c)
Displaying Messages on the LCD
The
message
method of theLCD
object is used to display text on the screen. The\n
character 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
clear
method of theLCD
object 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()