Interfacing 16x2 LCD with Raspberry Pi using I2C
Learn how to connect and display text on a 16x2 LCD using Raspberry Pi and the I2C communication protocol. Display messages, sensor data, and more using Python.
1. Introduction to LCD and I2C Communication
The 16x2 LCD display is one of the most common components used to show characters or sensor outputs. By interfacing it with Raspberry Pi through I2C, you reduce the number of GPIO pins used, making your project neat and manageable.
Why Use I2C with LCD?
- Reduces required GPIO pins from 12 to 4
- Simplifies wiring and code
- Allows multiple I2C devices on the same bus
2. Components Required
Basic Components:
- Raspberry Pi (any model with GPIO)
- 16x2 LCD Display
- I2C Module (backpack) for LCD
- Breadboard and jumper wires
Optional Tools:
- Multimeter for debugging
- Resistors if LCD does not have a built-in potentiometer
3. LCD and I2C Module Pinout
The I2C backpack is soldered onto the LCD to simplify connections and enable I2C communication.
4. Wiring Diagram
Connect the following pins:
- GND → Raspberry Pi GND
- VCC → Raspberry Pi 5V
- SDA → GPIO 2 (Pin 3)
- SCL → GPIO 3 (Pin 5)
5. Enabling I2C on Raspberry Pi
- Open terminal and run: sudo raspi-config
- Go to Interfacing Options > I2C > Enable
- Reboot the Pi using: sudo reboot
6. Installing Required Libraries
Install I2C and LCD libraries:
sudo apt-get update
sudo apt-get install -y python3-smbus i2c-tools
sudo pip3 install RPLCD
7. Python Code to Display Text
- Create a file: nano lcd_display.py
- Paste the following code:
from RPLCD.i2c import CharLCD
from time import sleep
lcd = CharLCD('PCF8574', 0x27)
lcd.write_string("Hello, IoT World!")
sleep(3)
lcd.clear()
lcd.write_string("Raspberry Pi Rocks!")
Run Command: python3 lcd_display.py
LCD displays 'Hello, IoT World!' then 'Raspberry Pi Rocks!'
8. Find LCD I2C Address
- Run the command: sudo i2cdetect -y 1
- Note the I2C address (usually 0x27 or 0x3f)
9. Applications
Real-life Uses:
- Display temperature/humidity from sensors
- Create digital clocks
- Show system status, IP address, or notifications
10. FAQs: LCD with Raspberry Pi
Q: What if nothing displays on LCD?
A: Adjust the contrast potentiometer on the I2C module or check I2C address.
Q: Why is my LCD garbled?
A: Clear the LCD and add delays after each command.
Q: Can I use multiple LCDs?
A: Yes, if each has a unique I2C address. Use an I2C multiplexer if needed.
11. Conclusion
- Using LCD with Raspberry Pi via I2C saves GPIO pins
- Perfect for IoT dashboards and local data display
- Python makes it easy to update messages dynamically