How to Use I2C LCD Display with Raspberry Pi: Simple Guide
To use an
I2C LCD display with a Raspberry Pi, connect the display's SDA and SCL pins to the Pi's corresponding pins, enable I2C in Raspberry Pi settings, and use a Python library like smbus to send text commands. Install necessary packages, find the LCD's I2C address, and write Python code to display messages on the screen.Syntax
Using an I2C LCD with Raspberry Pi involves these main parts:
- Importing libraries: Use
smbusfor I2C communication andtimefor delays. - Setting up I2C bus: Initialize the bus with
bus = smbus.SMBus(1)for Raspberry Pi models with I2C bus 1. - LCD address: The I2C address of the LCD (usually
0x27or0x3F). - Sending commands/data: Use
bus.write_byte_data(address, command, value)to control the LCD.
python
import smbus import time bus = smbus.SMBus(1) # Use I2C bus 1 LCD_ADDRESS = 0x27 # Common I2C address for LCD # Example command to clear display bus.write_byte_data(LCD_ADDRESS, 0x00, 0x01) # Clear display command
Example
This example shows how to initialize the I2C LCD and display "Hello, Pi!" on the first line.
python
import smbus import time # LCD constants LCD_ADDRESS = 0x27 LCD_WIDTH = 16 # Characters per line LCD_CHR = 1 # Mode - Sending data LCD_CMD = 0 # Mode - Sending command LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line ENABLE = 0b00000100 # Enable bit bus = smbus.SMBus(1) # I2C bus def lcd_toggle_enable(bits): bus.write_byte(LCD_ADDRESS, (bits | ENABLE)) time.sleep(0.0005) bus.write_byte(LCD_ADDRESS, (bits & ~ENABLE)) time.sleep(0.0001) def lcd_send_byte(bits, mode): high_bits = mode | (bits & 0xF0) | 0x08 low_bits = mode | ((bits << 4) & 0xF0) | 0x08 bus.write_byte(LCD_ADDRESS, high_bits) lcd_toggle_enable(high_bits) bus.write_byte(LCD_ADDRESS, low_bits) lcd_toggle_enable(low_bits) def lcd_init(): lcd_send_byte(0x33, LCD_CMD) # Initialize lcd_send_byte(0x32, LCD_CMD) # Set to 4-bit mode lcd_send_byte(0x06, LCD_CMD) # Cursor move direction lcd_send_byte(0x0C, LCD_CMD) # Turn cursor off lcd_send_byte(0x28, LCD_CMD) # 2 line display lcd_send_byte(0x01, LCD_CMD) # Clear display time.sleep(0.0005) def lcd_message(message): message = message.ljust(LCD_WIDTH, " ") for char in message: lcd_send_byte(ord(char), LCD_CHR) # Main program lcd_init() lcd_send_byte(LCD_LINE_1, LCD_CMD) lcd_message("Hello, Pi!")
Output
The LCD screen shows: Hello, Pi! (left aligned on the first line)
Common Pitfalls
Common mistakes when using I2C LCD with Raspberry Pi include:
- Not enabling I2C interface in Raspberry Pi settings (
sudo raspi-config). - Wrong wiring of SDA and SCL pins (SDA to SDA, SCL to SCL on Pi).
- Incorrect I2C address; use
sudo i2cdetect -y 1to find the correct address. - Not using pull-up resistors if your LCD module doesn't have them built-in.
- Sending commands without proper delays causing the LCD to miss instructions.
python
## Wrong way: Using wrong I2C address LCD_ADDRESS = 0x3F # If actual address is 0x27, this won't work ## Right way: Detect address first # Run in terminal: sudo i2cdetect -y 1 # Use detected address in code LCD_ADDRESS = 0x27
Quick Reference
Summary tips for using I2C LCD with Raspberry Pi:
- Enable I2C in Raspberry Pi config.
- Connect SDA to SDA (GPIO 2), SCL to SCL (GPIO 3).
- Use
sudo i2cdetect -y 1to find LCD address. - Use Python
smbuslibrary to send commands. - Initialize LCD before sending text.
- Use delays between commands for reliability.
Key Takeaways
Enable I2C interface on Raspberry Pi before connecting the LCD.
Use the correct I2C address found with i2cdetect for communication.
Connect SDA and SCL pins properly to Raspberry Pi GPIO pins 2 and 3.
Use Python smbus library to send commands and data to the LCD.
Add small delays between commands to ensure the LCD processes them.