Controlling an LCD display with the Raspberry Pi

Raspberry Pi LCD Display – Control HD44780 via GPIO and I2C with Python

A 16×2 character LCD display on the Raspberry Pi – a classic. Display the time, IP address, sensor values or status messages on a small screen, without a monitor and without SSH. In this tutorial I’ll show you two methods: direct GPIO wiring (as in the original from 2014) and the much easier I2C method with a PCF8574 adapter. For both we use the modern Python library RPLCD – no need to write your own LCD code.

Looking for a full touchscreen instead of a character display? The Raspberry Pi display comparison lists five models with a clear buying recommendation.

Materials

Method 1 – Direct GPIO wiring:

Links marked with * are affiliate links, which makes them advertising. If you buy through one, I earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases. Learn more

Method 2 – I2C adapter (recommended):

The I2C method is much simpler: only 4 cables instead of 11, no potentiometer needed (contrast is set in software) and the code is cleaner. If you don’t have a display yet, buy a set with PCF8574 adapter directly.

Method 1: Wiring directly via GPIO

The HD44780 LCD has 16 pins. We use 4-bit mode, so only data pins D4–D7 need to be wired. This pin assignment matches the proven original setup:

LCD pin LCD function Connection Raspberry Pi PIN
01 GND (VSS) GND PIN 6
02 +5V (VDD) 5V PIN 2
03 Contrast (V0) Potentiometer (middle pin) – (poti ends to 5V/GND)
04 RS GPIO7 PIN 26
05 RW GND (write only) PIN 6
06 E (Enable) GPIO8 PIN 24
07–10 D0–D3 not needed (4-bit mode) –
11 D4 GPIO25 PIN 22
12 D5 GPIO24 PIN 18
13 D6 GPIO23 PIN 16
14 D7 GPIO18 PIN 12
15 LED+ (backlight) 5V PIN 2
16 LED– (backlight) GND PIN 6

Method 2: Wiring with I2C adapter (recommended)

The PCF8574 I2C adapter sits directly on the back of the LCD display and reduces the wiring to just 4 cables:

I2C adapter pin Raspberry Pi PIN number
GND GND PIN 6
VCC 5V PIN 2
SDA SDA (GPIO2) PIN 3
SCL SCL (GPIO3) PIN 5

Enable I2C (method 2 only)

I2C must be enabled on the Raspberry Pi:

sudo raspi-config

Navigate to Interface Options → I2C → Yes. Then reboot:

sudo reboot

After rebooting, check whether the I2C adapter is detected:

sudo i2cdetect -y 1

You should see an entry at address 0x27 (or 0x3f on some modules). Note down the address – you’ll need it in the Python script.

Install RPLCD

Instead of writing our own LCD code, we use the mature Python library RPLCD, which supports both methods:

sudo apt update
sudo apt install -y python3-pip python3-smbus i2c-tools

Raspberry Pi OS won’t let pip install packages system-wide: sudo pip install stops with an externally-managed-environment error. Create a virtual environment instead. Don’t leave out --system-site-packages, though. Without it, the GPIO method can’t find the preinstalled GPIO library and fails with No module named 'RPi'.

Note: The commands in this guide use the user pi and the folder /home/pi. Newer installations don’t have that user any more, because you pick your own username during setup. So replace pi with your username everywhere, including User= in the service file further down. Run whoami to see your username.

python3 -m venv --system-site-packages /home/pi/lcd-env
/home/pi/lcd-env/bin/pip install RPLCD smbus2

Python script: Direct GPIO method

For direct wiring, use RPLCD.gpio:

from RPLCD.gpio import CharLCD
import RPi.GPIO as GPIO

lcd = CharLCD(
    cols=16,
    rows=2,
    pin_rs=7,
    pin_e=8,
    pins_data=[25, 24, 23, 18],
    numbering_mode=GPIO.BCM
)

lcd.write_string('Hello Raspberry!')
lcd.cursor_pos = (1, 0)
lcd.write_string('raspberry.tips')

Save the script as /home/pi/lcd.py and test it:

/home/pi/lcd-env/bin/python3 /home/pi/lcd.py

If you see a dark area but no text – turn the potentiometer until the contrast is right.

Python script: I2C method

For the I2C adapter the code is even simpler:

from RPLCD.i2c import CharLCD

# Check I2C address with: sudo i2cdetect -y 1
# Usually 0x27 or 0x3f
lcd = CharLCD('PCF8574', 0x27)

lcd.write_string('Hello Raspberry!')
lcd.cursor_pos = (1, 0)
lcd.write_string('raspberry.tips')

If your I2C address is 0x3f, just replace 0x27 accordingly.

Display time and IP address

The classic proof of concept – time on line 1, IP address on line 2, updated every second. Here’s the I2C version (for GPIO, just swap the LCD import and initialisation):

#!/usr/bin/env python3
import time
import socket
import subprocess
from RPLCD.i2c import CharLCD

lcd = CharLCD('PCF8574', 0x27)

def get_ip():
    try:
        result = subprocess.run(
            ['hostname', '-I'],
            capture_output=True, text=True
        )
        ip = result.stdout.strip().split()[0]
        return ip
    except Exception:
        return 'no IP'

lcd.clear()

while True:
    current_time = time.strftime('%d.%m.%Y %H:%M')
    ip = get_ip()

    lcd.cursor_pos = (0, 0)
    lcd.write_string(current_time.ljust(16))
    lcd.cursor_pos = (1, 0)
    lcd.write_string(('IP:' + ip).ljust(16))

    time.sleep(1)

Save as /home/pi/lcd.py and test:

/home/pi/lcd-env/bin/python3 /home/pi/lcd.py

Autostart via systemd

Instead of the old update-rc.d, we now use systemd services. Create a new service file:

sudo nano /etc/systemd/system/lcd.service

Add the following content:

[Unit]
Description=LCD Display Output
After=network.target

[Service]
Type=simple
User=pi
ExecStart=/home/pi/lcd-env/bin/python3 /home/pi/lcd.py
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

Save with Ctrl+X, Y, Enter. Then enable and start the service:

sudo systemctl daemon-reload
sudo systemctl enable lcd.service
sudo systemctl start lcd.service

Check status:

sudo systemctl status lcd.service

Conclusion

With RPLCD and an I2C adapter, controlling an LCD display on the Raspberry Pi is much simpler today than it was in 2014. Four cables, a few lines of Python, and you have a working display. For permanent operation, go with the I2C variant – less cable clutter, more stable and easier to extend.

Frequently Asked Questions: LCD Display on the Raspberry Pi

What is the difference between direct GPIO and I2C for the LCD?

Direct GPIO needs 6–10 pins; I2C only needs 4 wires via a PCF8574 adapter. For new projects, always use I2C: simpler wiring, no pin conflicts, same RPLCD library support.

Does RPLCD work with all 16×2 LCD displays?

Yes – all HD44780-compatible displays are supported, which covers virtually all standard character LCDs. I2C address is usually 0x27 or 0x3F, verifiable with i2cdetect -y 1.

How do I display special characters or umlauts on the LCD?

HD44780 doesn’t support UTF-8 natively. Use RPLCD’s create_char() for custom 5×8 pixel characters (max 8), or simply replace umlauts with ae/oe/ue in your display strings.

Can I connect multiple LCD displays to one Raspberry Pi?

Yes – with I2C, up to 8 displays per bus via PCF8574 address jumpers (A0–A2, addresses 0x20–0x27). Direct GPIO wiring gets impractical with multiple displays.

Similar Posts