import time
import RPi.GPIO as GPIO
pir_pin = 4
led_pin = 18
def motion_detected():
# Your code to handle motion detection (e.g., turn on LED)
GPIO.output(led_pin, GPIO.HIGH)
print("Motion Detected!")
GPIO.setmode(GPIO.BCM)
GPIO.setup(pir_pin, GPIO.IN)
GPIO.add_event_detect(pir_pin, GPIO.RISING, callback=motion_detected)
GPIO.setup(led_pin, GPIO.OUT)
try:
while True:
GPIO.wait_for_edge(pir_pin, GPIO.FALLING) # Wait for motion to stop (optional)
GPIO.output(led_pin, GPIO.LOW) # Turn off LED after motion stops (optional)
except KeyboardInterrupt:
GPIO.cleanup()