0
0
Iot-protocolsHow-ToBeginner · 4 min read

How to Display Sensor Data on LCD with Raspberry Pi

To display sensor data on an LCD with a Raspberry Pi, connect the sensor and LCD to the Pi's GPIO pins, then use Python libraries like Adafruit_DHT for sensors and lcd or RPi.GPIO for the LCD. Read the sensor data in your Python script and update the LCD display with the values in real time.
📐

Syntax

Here is the basic syntax to read sensor data and display it on an LCD using Python on Raspberry Pi:

  • import necessary libraries for sensor and LCD
  • Initialize sensor and LCD objects
  • Read sensor data using sensor library functions
  • Use LCD library methods to write data to the screen
  • Repeat reading and updating in a loop for live display
python
import Adafruit_DHT
from RPLCD.i2c import CharLCD
import time

# Initialize sensor and LCD
sensor = Adafruit_DHT.DHT22
pin = 4  # GPIO pin where sensor is connected
lcd = CharLCD('PCF8574', 0x27)

while True:
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    if humidity is not None and temperature is not None:
        lcd.clear()
        lcd.write_string(f'Temp: {temperature:.1f} C')
        lcd.crlf()
        lcd.write_string(f'Humidity: {humidity:.1f} %')
    else:
        lcd.clear()
        lcd.write_string('Sensor error')
    time.sleep(2)
💻

Example

This example reads temperature and humidity from a DHT22 sensor and shows the values on a 16x2 I2C LCD connected to the Raspberry Pi.

The LCD updates every 2 seconds with fresh sensor data.

python
import Adafruit_DHT
from RPLCD.i2c import CharLCD
import time

sensor = Adafruit_DHT.DHT22
pin = 4
lcd = CharLCD('PCF8574', 0x27)

try:
    while True:
        humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
        lcd.clear()
        if humidity is not None and temperature is not None:
            lcd.write_string(f'Temp: {temperature:.1f} C')
            lcd.crlf()
            lcd.write_string(f'Humidity: {humidity:.1f} %')
        else:
            lcd.clear()
            lcd.write_string('Sensor error')
        time.sleep(2)
except KeyboardInterrupt:
    lcd.clear()
Output
Temp: 23.4 C Humidity: 45.7 %
⚠️

Common Pitfalls

  • Not installing required libraries like Adafruit_DHT and RPLCD.
  • Incorrect GPIO pin number for the sensor.
  • Wrong I2C address for the LCD (check with i2cdetect command).
  • Not enabling I2C interface on Raspberry Pi.
  • Not handling sensor read failures, causing blank or frozen LCD.
python
import Adafruit_DHT
from RPLCD.i2c import CharLCD
import time

# Wrong pin number example
sensor = Adafruit_DHT.DHT22
pin = 17  # Incorrect if sensor is on GPIO4
lcd = CharLCD('PCF8574', 0x27)

while True:
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    lcd.clear()
    if humidity is not None and temperature is not None:
        lcd.write_string(f'Temp: {temperature:.1f} C')
        lcd.crlf()
        lcd.write_string(f'Humidity: {humidity:.1f} %')
    else:
        lcd.clear()
        lcd.write_string('Sensor error')
    time.sleep(2)

# Correct pin usage fixes the issue by matching wiring.
📊

Quick Reference

Tips for displaying sensor data on Raspberry Pi LCD:

  • Use Adafruit_DHT for DHT sensors.
  • Use RPLCD library for I2C LCD control.
  • Enable I2C interface via raspi-config.
  • Check sensor wiring and GPIO pins carefully.
  • Clear LCD before writing new data to avoid overlap.
  • Handle sensor read errors gracefully.

Key Takeaways

Connect sensor and LCD correctly to Raspberry Pi GPIO and enable I2C interface.
Use Python libraries like Adafruit_DHT and RPLCD to read sensor data and control LCD.
Clear the LCD before updating to avoid display issues.
Handle sensor read failures to prevent showing stale or no data.
Verify GPIO pins and I2C addresses match your hardware setup.