import machine
import time
# Define the pin connected to the PIR sensor
pir_pin = machine.Pin(20, machine.Pin.IN) # You can change the pin number according to your wiring
# Define the pin connected to the LED
led_pin = machine.Pin(5, machine.Pin.OUT) # You can change the pin number according to your wiring
while True:
try:
# Read the PIR sensor value
motion_detected = pir_pin.value()
if motion_detected:
print("Motion detected!")
led_pin.on() # Turn on the LED
else:
print("No motion detected.")
led_pin.off() # Turn off the LED
except Exception as e:
print("Error:", e)
# Wait for a short period before reading the sensor again
time.sleep(0.5)