from machine import Pin, I2C, ADC
import utime
import dht
# Initialize the DHT22 sensor
sensor = dht.DHT22(Pin(16)) # Adjust the pin number according to your connection
# Initialize the LCD
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
I2C_ADDR = 0x27 # I2C address of the LCD
i2c = I2C(1, scl=Pin(15), sda=Pin(14), freq=400000) # Adjust the pins according to your setup
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
# Initialize the buzzer, motor, and LED
buzzer = Pin(26, Pin.OUT) # Adjust the pin numbers according to your setup
motor = Pin(26, Pin.OUT)
led = Pin(27, Pin.OUT)
# Initialize the potentiometer
potentiometer = ADC(Pin(28)) # Adjust the pin number according to your setup
# Temperature limits
upperLimit = 30 # Default upper temperature limit
lowerLimit = 20 # Default lower temperature limit
def update_lcd(temperature, humidity):
lcd.clear()
lcd.putstr("Temp: {} C\nHumidity: {}%".format(temperature, humidity))
def map_value(value, in_min, in_max, out_min, out_max):
return (value - in_min) * (out_max - out_min) // (in_max - in_min) + out_min
while True:
# Read temperature and humidity from DHT22 sensor
try:
sensor.measure()
temperature = sensor.temperature()
humidity = sensor.humidity()
except OSError as e:
print('Failed to read sensor.')
# Read the potentiometer value
pot_value = potentiometer.read_u16()
pot_value_mapped = map_value(pot_value, 0, 65535, 10, 40) # Map the potentiometer value to temperature range
# Update the LCD display
update_lcd(temperature, humidity)
# Control devices based on temperature
if temperature > upperLimit:
buzzer.value(1)
motor.value(1)
led.value(0)
elif temperature < lowerLimit:
buzzer.value(0)
motor.value(0)
led.value(1)
else:
buzzer.value(0)
motor.value(0)
led.value(0)
# Adjust temperature limits based on potentiometer value
upperLimit = pot_value_mapped + 5 # Add some buffer
lowerLimit = pot_value_mapped - 5 # Add some buffer
utime.sleep(1)