# Project objectives:
# Read temperature and humidity values from the DHT22 sensor
# Display the sensor readings in the console
#
# 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
#
# Programmer: Adrian Josele G. Quional
# modules
from machine import Pin , PWM , 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
# creating a DHT object
# change DHT22 to DHT11 if DHT11 is used
dht = DHT22(Pin(19))
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
I2C_ADDR = i2c.scan()[0]
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
def buzzerFunc():
buzzer = PWM(Pin(15))
buzzer.freq(500)
buzzer.duty_u16(1000)
sleep(1)
buzzer.duty_u16(0)
def display_lcd(text):
lcd.clear()
lcd.putstr(text)
# continuously get sensor readings while the board has power
while True:
# getting sensor readings
dht.measure()
temp = dht.temperature()
hum = dht.humidity()
# displaying values to the console
print(f"Temperature: {temp}°C Humidity: {hum}% ")
lcd_text = f"Temp: {temp:.1f}°C\nHumidity: {hum:.1f}%"
display_lcd(lcd_text)
# format method or string concatenation may also be used
#print("Temperature: {}°C Humidity: {:.0f}% ".format(temp, hum))
#print("Temperature: " + str(temp) + "°C" + " Humidity: " + str(hum) + "%")
# delay of 2 secs because DHT22 takes a reading once every 2 secs
sleep(2)
if(temp > 20 and hum > 30):
buzzerFunc()
else:
None