Bird
0
0
Raspberry Piprogramming~5 mins

LCD display (16x2) with I2C backpack in Raspberry Pi

Choose your learning style9 modes available
Introduction

An LCD display with I2C backpack lets you show text on a small screen using just two wires. It makes your Raspberry Pi projects easy to read and interact with.

You want to display sensor readings like temperature or humidity.
You need a simple status message for your Raspberry Pi project.
You want to show a menu or options without a full monitor.
You want to save GPIO pins by using I2C instead of many wires.
Syntax
Raspberry Pi
from RPLCD.i2c import CharLCD

lcd = CharLCD('PCF8574', 0x27)
lcd.write_string('Hello World!')

Use the RPLCD library to control the LCD easily.

The address 0x27 is common but may differ; check your device.

Examples
Shows 'Hi!' on the LCD screen.
Raspberry Pi
from RPLCD.i2c import CharLCD

lcd = CharLCD('PCF8574', 0x27)
lcd.write_string('Hi!')
Writes 'Line 2 text' on the second line of the LCD at the first position.
Raspberry Pi
from RPLCD.i2c import CharLCD

lcd = CharLCD('PCF8574', 0x3F)
lcd.cursor_pos = (1, 0)
lcd.write_string('Line 2 text')
Clears the screen before writing new text.
Raspberry Pi
from RPLCD.i2c import CharLCD

lcd = CharLCD('PCF8574', 0x27)
lcd.clear()
lcd.write_string('Cleared and new')
Sample Program

This program shows a greeting on the first line and a description on the second line of the LCD. It waits 5 seconds so you can read it, then clears the screen.

Raspberry Pi
from RPLCD.i2c import CharLCD
import time

# Create LCD object with I2C address 0x27
lcd = CharLCD('PCF8574', 0x27)

# Clear the display
lcd.clear()

# Write first line
lcd.cursor_pos = (0, 0)
lcd.write_string('Hello, Pi!')

# Write second line
lcd.cursor_pos = (1, 0)
lcd.write_string('I2C LCD Demo')

# Keep message for 5 seconds
time.sleep(5)

# Clear before exit
lcd.clear()
OutputSuccess
Important Notes

Make sure your Raspberry Pi has I2C enabled in settings.

Check your LCD's I2C address with sudo i2cdetect -y 1 if unsure.

Use lcd.clear() to erase the screen before writing new text.

Summary

LCD with I2C backpack uses fewer wires and is easy to control from Raspberry Pi.

Use the RPLCD Python library to write text and control cursor position.

Always clear the screen before writing new messages for clarity.