# 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
from time import sleep
from dht import DHT22
from machine import I2C, Pin
from pico_i2c_lcd import I2cLcd
toneA = 440
toneB = 494
toneC = 523
toneD = 587
toneE = 659
toneF = 698
toneG = 784
tones = [toneA, toneB, toneC, toneD, toneE, toneF, toneG]
def play_buzzer_and_sleep(freq, duration_ms, duty_value=512):
buzzer= PWM(Pin(26))
buzzer.freq(freq)
buzzer.duty_u16(duty_value)
sleep(duration_ms)
buzzer.duty_u16(0)
def buzzerFunc():
buzzer= PWM(Pin(26))
buzzer.freq(500)
buzzer.duty_u16(1000)
sleep(1)
buzzer.duty_u16(0)
# creating a DHT object
# change DHT22 to DHT11 if DHT11 is used
dht = DHT22(Pin(22))
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
# getting I2C address
I2C_ADDR = i2c.scan()[0]
# creating an LCD object using the I2C address and specifying number of rows and columns in the LCD
# LCD number of rows = 2, number of columns = 16
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
RELAY_CTRL_PIN = 28
relay = Pin(RELAY_CTRL_PIN, Pin.OUT)
# 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.move_to(1,0)
lcd.putstr(f"Suhu :{temp} C")
lcd.move_to(1,1)
lcd.putstr(f"Humty :{hum} %")
sleep(5) # "Hello world!" text would be displayed for 5 secs
lcd.clear()
sleep(0.2) # clear the text for 1 sec then print the text again
# 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(1)
if(temp > 28 or hum > 30):
relay.high()
buzzerFunc()
else:
relay.low()
None