import dht
import machine
import utime
# Wiring instructions:
# Connect DHT22 data pin to ESP32 GPIO 21 (or any other GPIO pin you prefer)
# Connect anode (longer leg) of LED to ESP32 GPIO 25 (or any other GPIO pin you prefer)
# Connect cathode (shorter leg) of LED to a current-limiting resistor (e.g., 220Ω)
# Connect the other end of the resistor to ESP32 GND (ground)
dht22 = dht.DHT22(machine.Pin(21)) # Create DHT22 object with the data pin
led_pin = machine.Pin(25, machine.Pin.OUT) # GPIO 25 as output
temperature_threshold = 25.0 # Modify this as needed
while True:
dht22.measure() # Measre temperature and humidity
temperature = dht22.temperature()
# Check if the temperature exceeds the threshold
if temperature > temperature_threshold:
led_pin.on() # Turn on the LED
else:
led_pin.off() # Turn off the LED
# Delay before the next reading
utime.sleep(1) # Delay for 1 second