# main.py - Motion detection with LED and Buzzer on Raspberry Pi Pico W
# Using 'time.sleep' instead of 'utime.sleep'

from machine import Pin
from time import sleep # Import sleep directly from the time module

# Define the GPIO pins used
pir_sensor_pin = Pin(28, Pin.IN, Pin.PULL_DOWN) # PIR sensor output connected to GP28
led_pin = Pin(15, Pin.OUT)              # LED connected to GP15
buzzer_pin = Pin(14, Pin.OUT)            # Buzzer connected to GP14

# Ensure LED and Buzzer are off initially
led_pin.off()
buzzer_pin.off()

print("Motion detection system starting...")

try:
    while True:
        # Read the value from the PIR sensor
        motion_detected = pir_sensor_pin.value()

        if motion_detected:
            print("Motion Detected!")

            # Turn on the LED and Buzzer
            led_pin.on()
            buzzer_pin.on()

            # Keep them on for 2 seconds using sleep()
            sleep(2) # Call sleep directly

            # Turn off the LED and Buzzer
            led_pin.off()
            buzzer_pin.off()
            print("LED and Buzzer turned off.")

            # Add a small delay to prevent immediate re-triggering
            sleep(0.5) # Call sleep directly

        # Small delay in the main loop to reduce CPU usage
        sleep(0.1) # Call sleep directly

except KeyboardInterrupt:
    print("Program stopped.")
    # Clean up GPIO pins
    led_pin.off()
    buzzer_pin.off()
$abcdeabcde151015202530fghijfghij
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT