from machine import Pin, ADC, PWM, I2C
from time import sleep
import dht
from pico_i2c_lcd import I2cLcd
ldr = ADC(26)
led = Pin(15, Pin.OUT)
pir = Pin(16, Pin.IN)
servo = PWM(Pin(13))
sensor = dht.DHT11(Pin(14))
I2C_ADDR = 0x27
I2C_NUM_ROWS = 2
I2C_NUM_COLS = 16
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
servo.freq(50)
def set_servo_angle(angle):
duty = int((angle / 18) + 2)
servo.duty_u16(duty * 65536 /100)
lcd.putstr("Smart Home Ready!")
sleep(2)
lcd.clear()
while True:
light_value = ldr.read_u16()
light_level = light_value / 65535 * 100
try:
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
except:
temp, hum = 0, 0
motion = pir.value()
if light_value < 30000 or motion == 1:
led.value(1)
else:
led.value(0)
if motion == 1:
set_servo_angle(90)
else:
set_servo_angle(0)
lcd.clear()
lcd.putstr("T:{}C H:{}%\n".format(temp, hum))
lcd.putstr("Light:{:.0f}%".format(light_level))
print("Light:", light_level, "Temp:", temp, "Humidity:", hum, "Motion:", motion)
sleep(1)