# PIR Motion Detection with Raspberry Pi
# COMP-556 Assignment 1 - Response Time Comparison
# Modified to match Arduino code functionality and measure response time
import RPi.GPIO as GPIO
import time
# Pin configuration - Using consistent pin numbering
LED_PIN = 18 # GPIO 18 for LED
PIR_PIN = 17 # GPIO 17 for PIR sensor
# Setup GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIR_PIN, GPIO.IN)
GPIO.setup(LED_PIN, GPIO.OUT)
print("Raspberry Pi PIR Motion Detection - Response Time Test")
# Initialize state variables
state = False # Equivalent to LOW in Arduino
try:
while True:
start_time = time.time() # Start timing measurement
pir_value = GPIO.input(PIR_PIN) # Read PIR sensor
if pir_value == GPIO.HIGH: # Motion detected
GPIO.output(LED_PIN, GPIO.HIGH) # Turn LED ON
response_time = (time.time() - start_time) * 1000000 # Convert to microseconds
if not state: # If state was LOW
print(f"Motion detected! Response Time: {response_time:.2f} μs")
state = True # Update state to HIGH
else: # No motion detected
GPIO.output(LED_PIN, GPIO.LOW) # Turn LED OFF
if state: # If state was HIGH
print("Motion stopped!")
state = False # Update state to LOW
time.sleep(0.5) # Delay between readings (500ms like Arduino)
except KeyboardInterrupt:
print("Program stopped by user")
GPIO.cleanup() # Clean up GPIO