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 the basics of displaying text on an LCD screen using a Raspberry Pi. We’ll start by showing you how to connect a standard LCD to the Raspberry Pi using the I2C interface. You’ll learn how to set up the LCD with simple parameters like the Raspberry Pi model and I2C address. Then, we’ll walk you through writing a basic Python script to display messages like “Hello World!” on the screen. This straightforward project is aimed at beginners, offering a foundational introduction to interfacing hardware with the Raspberry Pi and basic Python programming.

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

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

Raspberry Pi 5

BUY

I2C LCD 1602

BUY

Wiring

../_images/Lesson_26_LCD1602_Pi_bb.png

Code

import time
from LCD import LCD

# Initialize the LCD with specific parameters: Raspberry Pi revision, I2C address, and backlight status
lcd = LCD(2, 0x27, True)  # Using Raspberry Pi revision 2, I2C address 0x27, backlight enabled

# Display messages on the LCD
lcd.message("Hello World!", 1)        # Display 'Hello World!' on line 1
lcd.message("    - Sunfounder", 2)    # Display '    - Sunfounder' on line 2

# Keep the messages displayed for 5 seconds
time.sleep(5)

# Clear the LCD display
lcd.clear()

Code Analysis

  1. Import Libraries

    Import the time module for creating delays and the LCD module for controlling the LCD.

    For more detail about the LCD library, please refer to sterlingbeason/LCD-1602-I2C.

    import time
    from LCD import LCD
    
  2. Initialize the LCD

    Create an LCD object with specific parameters: the Raspberry Pi revision, the I2C address of the LCD, and the backlight status. In this case, Raspberry Pi revision 2(and higher version), I2C address 0x27, and backlight enabled.

    lcd = LCD(2, 0x27, True)
    
  3. Display Messages on the LCD

    Use the message method of the LCD object to display text on the LCD. The first argument is the text, and the second argument is the line number.

    lcd.message("Hello World!", 1)
    lcd.message("    - Sunfounder", 2)
    
  4. Keep the Messages Displayed

    Pause the program for 5 seconds, keeping the messages on the LCD during this time.

    time.sleep(5)
    
  5. Clear the LCD Display

    After the delay, clear the display using the clear method of the LCD object.

    lcd.clear()