import dht
from machine import Pin, I2C
import utime
from pico_i2c_lcd import I2cLcd # Import the I2C LCD library
# Define pin numbers for DHT22 sensor and LEDs
DHT_PIN = 14 # DHT22 sensor connected to pin 14
RED_LED_PIN_TEMP = 15 # Red LED connected to pin 15 for temperature
YELLOW_LED_PIN_TEMP = 16 # Yellow LED connected to pin 16 for temperature
RED_LED_PIN_HUM = 17 # Red LED connected to pin 17 for humidity
GREEN_LED_PIN_HUM = 22 # Green LED connected to pin 18 for humidity
I2C_SCL_PIN = 21 # GPIO21 for I2C SCL
I2C_SDA_PIN = 22 # GPIO22 for I2C SDA
# Initialize DHT22 sensor
dht22 = dht.DHT22(Pin(DHT_PIN))
# Initialize LED pins
red_led_temp = Pin(RED_LED_PIN_TEMP, Pin.OUT)
yellow_led_temp = Pin(YELLOW_LED_PIN_TEMP, Pin.OUT)
red_led_hum = Pin(RED_LED_PIN_HUM, Pin.OUT)
green_led_hum = Pin(GREEN_LED_PIN_HUM, Pin.OUT)
# Initialize I2C bus
i2c = I2C(0, scl=Pin(I2C_SCL_PIN), sda=Pin(I2C_SDA_PIN))
# Initialize LCD display
lcd = I2cLcd(i2c, 0x27, 2, 16) # Change the address (0x27) according to your LCD address
def read_sensor_data():
dht22.measure()
return dht22.temperature(), dht22.humidity()
def update_lcd(temperature, humidity):
lcd.clear()
lcd.putstr("Temp: {:.1f} C".format(temperature))
lcd.move_to(0, 1)
lcd.putstr("Humidity: {:.1f}%".format(humidity))
def main():
while True:
temperature, humidity = read_sensor_data()
# Temperature control
if temperature < 40:
yellow_led_temp.on()
red_led_temp.off()
else:
yellow_led_temp.off()
red_led_temp.on()
print("Temperature above 40°C! Alert!")
# Humidity control
if humidity < 20:
red_led_hum.on()
green_led_hum.off()
print("Too hot! Humidity below 20%")
elif humidity > 30:
red_led_hum.off()
green_led_hum.on()
else:
red_led_hum.off()
green_led_hum.off()
print("Temperature:", temperature, "°C")
print("Humidity:", humidity, "%")
# Update LCD display
update_lcd(temperature, humidity)
utime.sleep(2) # Adjust delay as needed
if __name__ == "__main__":
main()