from machine import Pin
from time import sleep
from dht import DHT22
from RPLCD.i2c import CharLCD
# Initialize the LCD display
lcd = CharLCD('PCF8574', 0x27)
lcd.clear()
# Initialize the DHT22 sensor (assume it's connected to GP15)
sensor = DHT22(Pin(15))
while True:
# Measure temperature and humidity
sensor.measure()
temp = sensor.temperature()
humidity = sensor.humidity()
# Clear LCD to update it with the new information
lcd.clear()
# Display the temperature and humidity
lcd.write_string(f"Temp: {temp}°C\n")
lcd.write_string(f"Humidity: {humidity}%")
# Check temperature range and update the LCD accordingly
lcd.set_cursor(0, 1) # Move to the second line for temperature warning
if temp > 32:
lcd.write_string("Temp is too high.")
elif temp == 32:
lcd.write_string("Temp is perfect.")
elif temp < 32:
lcd.write_string("Temp is too low.")
# Check humidity range and update the LCD accordingly
lcd.set_cursor(0, 2) # Move to the third line for humidity warning
if humidity > 60:
lcd.write_string("Too humid.")
elif humidity == 60:
lcd.write_string("Humidity is perfect.")
elif humidity < 60:
lcd.write_string("Lacking humidity.")
# Wait for a moment before updating again
sleep(2)