# Project objectives:
# Read temperature and humidity values from the DHT22 sensor
# Display the sensor readings in the console
# Print a "Hello world!" text on the LCD screen to test its functionality
# Learn how to use I2C communication between the LCD and Raspberry Pi Pico
# Get familiarized with the pico_i2c_lcd and lcd_api modules
#
# Hardware connections used:
# DHT22 VCC Pin to 3.3V
# DHT22 SDA Pin to GPIO Pin 15
# 10k ohm pull-up resistor from DHT22 SDA Pin to 3.3V
# DHT22 GND Pin to GND
# LCD GND Pin to Raspberry Pi Pico GND
# LCD VCC Pin to Raspberry Pi Pico VBUS
# LCD SDA Pin to Raspberry Pi Pico GPIO Pin 0
# LCD SCL Pin to Raspberry Pi Pico GPIO Pin 1
#
# Programmer: Adrian Josele G. Quional
# Modules
from machine import Pin, I2C
from time import sleep
from dht import DHT22 # if the sensor is DHT11, import DHT11 instead of DHT22
from pico_i2c_lcd import I2cLcd
# DHT22 sensor setup
dht = DHT22(Pin(15))
# LCD setup
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
I2C_ADDR = i2c.scan()[0]
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
# Continuously read sensor data and display on console, then print "Hello world!" on LCD
while True:
# Read sensor data
dht.measure()
temp = dht.temperature()
hum = dht.humidity()
# Display sensor data on console
print(f"Temperature: {temp}°C Humidity: {hum}% ")
# Display temperature and humidity on LCD
lcd.clear()
lcd.putstr("Temperature: {}C".format(temp))
lcd.move_to(0, 1) # Move cursor to the second line
lcd.putstr("Humidity: {}%".format(hum))
sleep(5) # Display temperature and humidity for 5 seconds
lcd.clear()
sleep(1) # Clear LCD for 1 second before repeating