from machine import Pin, PWM
from time import sleep
led = Pin(1, Pin.OUT) # Declare LED as an output
# creating a PIR object, setting it as IN
pir = Pin(10, Pin.IN)
buzzer = PWM(Pin(5)) # Declare buzzer on GPIO5 as PWM output
buzzer.freq(1000) # Set buzzer frequency (adjust as needed)
buzzer.duty_u16(0) # Turn off the buzzer initially
motion_detected = False # Initialize motion detected flag
# continuously read data from the PIR sensor
# print a status whether a motion is detected or not
while True:
if pir.value() == 1:
if not motion_detected:
led.value(1) # Turn LED ON
buzzer.duty_u16(32768) # Turn on the buzzer (adjust duty cycle for desired sound)
motion_detected = True
print(f"PIR value: {pir.value()} Status: Motion detected!")
else:
if motion_detected:
led.value(0) # Turn LED OFF
buzzer.duty_u16(0) # Turn off the buzzer
motion_detected = False
print(f"PIR value: {pir.value()} Status: Waiting for movement...")
# PIR sensor would check for movement every second
sleep(1)