//Motion detection
from machine import Pin
from time import sleep
motion = False
def handle_interrupt(pin):
global motion
motion = True
global interrupt_pin
interrupt_pin = pin
led = Pin(26, Pin.OUT)
pir = Pin(22, Pin.IN)
pir.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)
while True:
if motion:
print('Motion detected!')
led.value(1)
sleep(1)
led.value(0)
motion = False
///////////////////////////////////////////////
// LCD Temp
from machine import Pin,PWM,I2C
from time import sleep
from dht import DHT22
from pico_i2c_lcd import I2cLcd
dht = DHT22(Pin(22))
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
I2C_ADDR = i2c.scan()[0]
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
def buzzer():
buzzer = PWM(Pin(21))
buzzer.freq(500)
buzzer.duty_u16(1000)
sleep(1)
buzzer.duty_u16(0)
while True:
dht.measure()
temp = dht.temperature()
hum = dht.humidity()
print(f"Temperature: {temp}°C Humidity: {hum}% ")
sleep(2)
if (temp > 20 and hum > 30):
buzzer()
else:
None
lcd.putstr(f"Temp:{temp} C Humidity:{hum}%")
sleep(2)
lcd.clear()
sleep(1)
//////////////////////////////////////////////////////
//LED Blink
from machine import Pin
from time import sleep
led1 = Pin(18, Pin.OUT)
led2 = Pin(19, Pin.OUT)
led3 = Pin(20, Pin.OUT)
buz = Pin(21, Pin.OUT)
def ledBlink(le):
le.on()
sleep(0.5)
le.off()
sleep(0.5)
while True:
ledBlink(led1)
ledBlink(led2)
ledBlink(buz)
ledBlink(led3)
ledBlink(led2)
ledBlink(buz)