#Use the PIR sensor to detect a movemnet
#VCC of PIR to 3.3V Pico
#GND of PIR to GND Pico
#D Pin of PIR to pin 16 Pico
#modules
from machine import Pin, I2C, PWM
from time import sleep
from pico_i2c_lcd import I2cLcd
#create the pir object, IN
pir = Pin(16, Pin.IN)
# PIR sensor setup
pir = Pin(16, Pin.IN)
servo = PWM(Pin(15))
servo.freq(50)
def move_servo(angle):
min_duty = 2000 # 0 degrees
max_duty = 8000 # 180 degrees
duty = int(min_duty + (angle / 180) * (max_duty - min_duty))
servo.duty_u16(duty)
# I2C LCD setup
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
I2C_ADDR = i2c.scan()[0] # auto-detect I2C address
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16) # 2 rows, 16 columns
#continous read from the sensor
#print a message if a movemENt was detected
while True:
if pir.value() == 1:
print("PIR: 1 - Motion Detected!")
lcd.clear()
lcd.putstr("Door Opened")
move_servo(180)
else:
print("PIR: 0 - No Motion")
lcd.clear()
lcd.putstr("Door Closed")
move_servo(0)
sleep(1)