from machine import Pin, ADC
import time
import dht
# Initialize pins
humidifier = Pin(2, Pin.OUT) # Humidifier controlled by humidity
fan = Pin(18, Pin.OUT) # Fan controlled by temperature
uv_lamp = Pin(12, Pin.OUT) # UV lamp controlled by temperature
photo_led = Pin(27, Pin.OUT) # LED controlled by photoresistor
# Initialize DHT22 sensor
dht22_sensor = dht.DHT22(Pin(14))
# Initialize photoresistor (using ADC pin 34)
photoresistor = ADC(Pin(34))
photoresistor.atten(ADC.ATTN_11DB) # Configure to read the full range of values
# Define thresholds
fantemp_threshold = 25.0 # Temperature threshold for fan
uvtemp_threshold = 20.0 # Temperature threshold for UV lamp
humidity_threshold = 40.0 # Humidity threshold for humidifier
adc_threshold = 2000 # Light level threshold for LED controlled by photoresistor
while True:
# Read DHT22 sensor values
dht22_sensor.measure()
temperature = dht22_sensor.temperature()
humidity = dht22_sensor.humidity()
# Read photoresistor value
adc_value = photoresistor.read()
# Control humidifier based on humidity
if humidity < humidity_threshold:
humidifier.on()
else:
humidifier.off()
# Control fan based on temperature
if temperature < fantemp_threshold:
fan.off()
else:
fan.on()
# Control UV lamp based on temperature
if temperature < uvtemp_threshold:
uv_lamp.on()
else:
uv_lamp.off()
# Control LED based on light level
if adc_value > adc_threshold:
photo_led.on()
else:
photo_led.off()
# Print sensor values for debugging
print("Temperature:", temperature, "C")
print("Humidity:", humidity, "%")
print("ADC Value :", adc_value)
# Wait before the next measurement
time.sleep(2)