Connecting a Character LCD to a Raspberry Pi
Connecting a standard character LCD (like the popular HD44780 16x2 screen) to a Raspberry Pi allows your projects to display real-time data, system status, or custom text without needing a full monitor. This article provides a straightforward guide to wiring the LCD directly to the Pi’s GPIO pins, configuring the necessary Python libraries, and running a basic script to text on the screen. Whether you are building an alarm clock or a network monitor, this step-by-step walkthrough will get your display up and running.
Required Components and Tools
Before starting, gather the following hardware components:
- Raspberry Pi (Any model with GPIO pins, along with an SD card and power supply)
- HD44780-compatible Character LCD (16x2 or 20x4 are most common)
- 10k Ohm Potentiometer (Crucial for adjusting the display contrast)
- Breadboard and Jumper Wires (Male-to-female and male-to-male)
- Soldering Iron and Header Pins (If your LCD did not come with pins pre-soldered)
Wiring the LCD to the Raspberry Pi
Character LCDs can operate in 8-bit mode or 4-bit mode. To save valuable GPIO pins on the Raspberry Pi, this guide uses the 4-bit mode, which requires only six GPIO pins for data and control, plus power and contrast connections.
Carefully connect the pins according to the following mapping guide:
- LCD Pin 1 (VSS/GND) to Raspberry Pi GND
- LCD Pin 2 (VDD/5V) to Raspberry Pi 5V
- LCD Pin 3 (V0/Contrast) to the center pin (wiper) of the 10k potentiometer
- LCD Pin 4 (RS) to Raspberry Pi GPIO 25
- LCD Pin 5 (RW) to Raspberry Pi GND (puts the screen in write mode)
- LCD Pin 6 (E/Enable) to Raspberry Pi GPIO 24
- LCD Pin 11 (D4) to Raspberry Pi GPIO 23
- LCD Pin 12 (D5) to Raspberry Pi GPIO 17
- LCD Pin 13 (D6) to Raspberry Pi GPIO 18
- LCD Pin 14 (D7) to Raspberry Pi GPIO 22
- LCD Pin 15 (A/Backlight Anode) to Raspberry Pi 5V (or 3.3V for a dimmer backlight)
- LCD Pin 16 (K/Backlight Cathode) to Raspberry Pi GND
Connect the remaining two outer pins of the 10k potentiometer to Raspberry Pi 5V and GND. Turning the knob will alter the voltage on LCD Pin 3, allowing you to make the text visible against the backlight.
Setting Up the Software Environment
With the wiring complete, power on your Raspberry Pi and open a terminal window. You need to install the CircuitPython library ecosystem provided by Adafruit, which simplifies controlling character displays.
First, update your package list and install the Python package manager if it isn’t already present:
sudo apt update
sudo apt install python3-pipNext, install the specific library for character LCDs:
pip3 install adafruit-circuitpython-charlcdWriting the Python Control Script
Create a new Python file using your preferred text editor, such as Nano:
nano lcd_test.pyPaste the following Python code into the file. This script initializes the library with the exact GPIO pins used in the wiring step, sets the dimensions of a 16x2 screen, and prints a two-line message.
import time
import board
import digitalio
import adafruit_character_lcd.character_lcd as character_lcd
# Modify these pin definitions if you used different GPIO pins
lcd_rs = digitalio.DigitalInOut(board.D25)
lcd_en = digitalio.DigitalInOut(board.D24)
lcd_d4 = digitalio.DigitalInOut(board.D23)
lcd_d5 = digitalio.DigitalInOut(board.D17)
lcd_d6 = digitalio.DigitalInOut(board.D18)
lcd_d7 = digitalio.DigitalInOut(board.D22)
# Define LCD column and row size
lcd_columns = 16
lcd_rows = 2
# Initialize the LCD class
lcd = character_lcd.Character_LCD_Mono(
lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows
)
# Clear the screen and display the message
lcd.clear()
lcd.message = "Raspberry Pi\nLCD Project"
# Leave the message on screen for 5 seconds, then clear it
time.sleep(5.0)
lcd.clear()Save and exit the editor by pressing CTRL+O,
Enter, and then CTRL+X.
Running the Project and Troubleshooting
Execute the script from your terminal:
python3 lcd_test.pyIf the script runs without errors but nothing appears on the screen, slowly turn the knob on your 10k potentiometer. The contrast setting is highly sensitive, and the text may be completely invisible or hidden behind solid blocks if the contrast voltage is incorrect.
If you still see no output, double-check that your data pins (D4 through D7) are connected to the correct physical layout on the Pi, and verify that the backlight pins (15 and 16) are correctly oriented to power the display’s LEDs.