Raspberry Pi - LCD
This tutorial instructs you how to use Raspberry Pi with LCD display, and print text, special characters on it.
Hardware Preparation
1 | × | Raspberry Pi 4 Model B | |
1 | × | LCD I2C | |
1 | × | Jumper Wires | |
1 | × | (Optional) Screw Terminal Adapter for Raspberry Pi | |
1 | × | (Optional) Power Adapter for Raspberry Pi 4B | |
1 | × | (Optional) Plastic Case for Raspberry Pi 4B |
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.
Additionally, some of these links are for products from our own brand, DIYables.
Overview of LCD I2C 16x2
The LCD I2C module is a alternative to the standard LCD. It simplifies the wiring between Raspberry Pi and the LCD. The LCD I2C module also has a built-in potentiometer, which can be used to adjust the contrast of LCD.
The LCD I2C Pinout
The LCD I2C needs to be connected to the I2C interface of Raspberry Pi. It has 4 pins:
- GND pin: This needs to be connected to GND (0V).
- VCC pin: This is the power supply for the LCD, and it needs to be connected to VCC (5V).
- SDA pin: This is the I2C data signal.
- SCL pin: This is the I2C clock signal.
LCD Coordinate
The LCD I2C 16x2 has 16 columns and 2 rows, with the columns and rows being numbered starting from 0.
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
LCD I2C | Raspberry Pi |
---|---|
Vin | 5V |
GND | GND |
SDA | GPIO2 (pin 3) |
SCL | GPIO3 (pin 5) |
Raspberry Pi Code
Detailed Instructions
- Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
- Make sure your Raspberry Pi is connected to the same local network as your PC.
- Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
- If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
- Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
- Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update
sudo apt-get install python3-rpi.gpio
- Prior to utilizing the LCD I2C with a Raspberry Pi, we need to enable I2C interface on Raspberry Pi. See How to enable I2C interface on Raspberry Pi
- Install the LCD I2C library by running the following command:
pip install lcddriver
- Create a Python script file lcd.py and add the following code:
"""
This Raspberry Pi code was developed by newbiely.com
This Raspberry Pi code is made available for public use without any restriction
For comprehensive instructions and wiring diagrams, please visit:
https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-lcd
"""
import lcddriver
from time import sleep
# I2C address 0x27, 16 column and 2 rows
LCD = lcddriver.lcd()
def display_message(line1, line2, duration):
LCD.lcd_clear()
LCD.lcd_display_string(line1, 1)
LCD.lcd_display_string(line2, 2)
sleep(duration)
try:
while True:
display_message("Newbiely", "newbiely.com", 2)
display_message("DIYables", "www.diyables.io", 2)
except KeyboardInterrupt:
pass
finally:
LCD.lcd_clear()
- Save the file and run the Python script by executing the following command in the terminal:
python3 lcd.py
- Check the LCD for the outcome.
The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.