A MotionSensor (PIR) detects movement by sensing changes in infrared light. It helps your Raspberry Pi know when someone or something moves nearby.
MotionSensor (PIR) in Raspberry Pi
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) PIR_PIN = 7 GPIO.setup(PIR_PIN, GPIO.IN) try: while True: if GPIO.input(PIR_PIN): print("Motion Detected!") else: print("No Motion") time.sleep(1) except KeyboardInterrupt: GPIO.cleanup()
Use GPIO.setmode(GPIO.BCM) to refer to pins by their Broadcom number.
Always clean up GPIO pins with GPIO.cleanup() to avoid warnings.
GPIO.setup(7, GPIO.IN) if GPIO.input(7): print("Motion detected!")
import time while True: if GPIO.input(7): print("Motion!") time.sleep(0.5)
This program continuously checks the PIR sensor on pin 7. It prints "Motion Detected!" when it senses movement, otherwise "No Motion". Press Ctrl+C to stop safely.
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) PIR_PIN = 7 GPIO.setup(PIR_PIN, GPIO.IN) print("Starting motion sensor. Press Ctrl+C to stop.") try: while True: if GPIO.input(PIR_PIN): print("Motion Detected!") else: print("No Motion") time.sleep(1) except KeyboardInterrupt: print("Stopping motion sensor.") GPIO.cleanup()
Make sure your PIR sensor is connected correctly: power, ground, and signal to the correct GPIO pin.
Sometimes PIR sensors need a few seconds to warm up before detecting motion.
Use try-except to handle stopping the program safely and cleaning GPIO pins.
A PIR MotionSensor detects movement by sensing infrared changes.
Use GPIO input to read the sensor state on your Raspberry Pi.
Always clean up GPIO pins when your program ends.
Practice
What does a PIR MotionSensor detect when connected to a Raspberry Pi?
Solution
Step 1: Understand PIR sensor function
A PIR sensor detects motion by sensing changes in infrared radiation emitted by objects.Step 2: Compare options with sensor purpose
Only Movement by sensing infrared changes matches the PIR sensor's function of detecting movement via infrared changes.Final Answer:
Movement by sensing infrared changes -> Option CQuick Check:
PIR sensor = Movement detection [OK]
- Confusing PIR with temperature sensors
- Thinking PIR detects sound or light
- Assuming PIR measures humidity
Which of the following is the correct way to set up a GPIO pin as input for a PIR sensor in Python using RPi.GPIO?
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
# What goes here?
Solution
Step 1: Identify pin mode for PIR sensor input
PIR sensor output should be read as input on the Raspberry Pi GPIO pin.Step 2: Match correct GPIO setup syntax
GPIO.setup(pin_number, GPIO.IN) sets the pin as input, which is needed to read sensor data.Final Answer:
GPIO.setup(17, GPIO.IN) -> Option AQuick Check:
GPIO input setup = GPIO.setup(pin, GPIO.IN) [OK]
- Using GPIO.OUT instead of GPIO.IN
- Calling GPIO.input() instead of GPIO.setup() to configure pin
- Trying to write output to sensor pin
What will be the output of this Python code snippet when the PIR sensor detects motion?
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
PIR_PIN = 4
GPIO.setup(PIR_PIN, GPIO.IN)
if GPIO.input(PIR_PIN):
print("Motion Detected")
else:
print("No Motion")
Solution
Step 1: Understand GPIO.input behavior
GPIO.input(PIR_PIN) returns True (1) if motion is detected, False (0) otherwise.Step 2: Analyze if condition with PIR sensor detecting motion
If motion is detected, GPIO.input returns True, so the code prints "Motion Detected".Final Answer:
Motion Detected -> Option AQuick Check:
GPIO.input True = Motion Detected [OK]
- Assuming GPIO.input returns string
- Confusing True/False logic in if statement
- Not setting up GPIO pin before reading
Identify the error in this PIR sensor code snippet and choose the fix:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
PIR_PIN = 18
GPIO.setup(PIR_PIN, GPIO.IN)
try:
while True:
if GPIO.input(PIR_PIN):
print("Motion!")
time.sleep(1)
except KeyboardInterrupt:
print("Exit")
Solution
Step 1: Check for GPIO cleanup after program ends
GPIO pins should be cleaned up to reset their state when the program exits.Step 2: Add GPIO.cleanup() in the except block
Adding GPIO.cleanup() ensures pins are properly reset on KeyboardInterrupt.Final Answer:
Add GPIO.cleanup() in except block -> Option DQuick Check:
Always cleanup GPIO pins on exit [OK]
- Forgetting GPIO.cleanup() causes warnings on rerun
- Setting input pin as output
- Removing sleep causes high CPU usage
You want to count how many times motion is detected in 10 seconds using a PIR sensor on GPIO pin 23. Which code snippet correctly implements this?
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
PIR_PIN = 23
GPIO.setup(PIR_PIN, GPIO.IN)
count = 0
start_time = time.time()
while time.time() - start_time < 10:
if GPIO.input(PIR_PIN):
count += 1
time.sleep(0.5) # debounce delay
print(count)
GPIO.cleanup()
Solution
Step 1: Analyze loop timing and counting logic
The loop runs for 10 seconds, checking PIR input and incrementing count on detection.Step 2: Understand debounce with time.sleep(0.5)
The 0.5 second sleep prevents counting the same motion multiple times quickly.Step 3: Confirm GPIO cleanup and print
After loop ends, count is printed and GPIO.cleanup() resets pins properly.Final Answer:
Counts motion detections with debounce, prints count after 10 seconds -> Option BQuick Check:
Debounce delay + timed loop = correct count [OK]
- Missing debounce causes inflated counts
- Setting pin as output instead of input
- No exit condition causes infinite loop
Ready to prove it?
Pass the test and this pattern moves unknown
