Let’s display the temperature and humidity from the DHT sensor on an LCD screen using Python on a Raspberry Pi.
Project: Display Sensor Data on 16×2 LCD with I2C
What You Need:
- Raspberry Pi
- DHT11 or DHT22 sensor
- I2C 16×2 LCD module (with I2C backpack)
- 10kΩ pull-up resistor (if using raw DHT)
- Breadboard and jumper wires
Wiring the I2C LCD to Raspberry Pi:
Enable I2C on Raspberry Pi:
bash
sudo raspi-config
Go to Interface Options > I2C > Enable
Then reboot:
bash
sudo reboot
Step 1: Install Required Libraries
bash
sudo apt update
sudo apt install python3-pip i2c-tools
pip3 install adafruit-circuitpython-charlcd
pip3 install Adafruit_DHT
Step 2: Find Your I2C Address
Run:
bash
i2cdetect -y 1
Look for an address like 0x27 or 0x3F (that’s your LCD address).
Step 3: Python Script
Create a file:
bash
nano lcd_dht.py
Paste this code (make sure to adjust your I2C address if needed):
python
import time
import board
import busio
import adafruit_character_lcd.character_lcd_i2c as character_lcd
import Adafruit_DHT
# Set up DHT sensor
sensor = Adafruit_DHT.DHT11
dht_pin = 4
# Set up I2C and LCD
i2c = busio.I2C(board.SCL, board.SDA)
lcd_columns = 16
lcd_rows = 2
lcd = character_lcd.Character_LCD_I2C(i2c, lcd_columns, lcd_rows)
# Clear screen
lcd.clear()
try:
while True:
humidity, temperature = Adafruit_DHT.read_retry(sensor, dht_pin)
if humidity is not None and temperature is not None:
lcd.clear()
lcd.message = f"Temp:{temperature:.1f}CnHum:{humidity:.1f}%"
else:
lcd.clear()
lcd.message = "Sensor Error"
time.sleep(2)
except KeyboardInterrupt:
lcd.clear()
lcd.message = "Goodbye!"
time.sleep(2)
lcd.clear()
Save and exit (Ctrl+X, then Y, then Enter).
Run It:
bash
python3 lcd_dht.py
You should now see the temperature and humidity values update every 2 seconds on your LCD!
What You Learn:
- I2C device communication
- Combining input (DHT) and output (LCD)
- Building real-time display systems