# 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
#
# Developer: Ts Zulkifli Zaki
# modules
from machine import Pin
from time import sleep
from dht import DHT22
# creating a DHT object
dht = DHT22(Pin(15))
# continuously get sensor readings while the board has power
while True:
# getting sensor readings
dht.measure()
temp = dht.temperature()
humid = dht.humidity()
# displaying values to the console if values are available
if humid is not None and temp is not None:
print(f"Temperature: {temp}°C Humidity: {humid}% ")
else:
print("Failed to retrieve data from sensor.")
# delay of 2 secs because DHT22 takes a reading once every 2 secs
sleep(2)