Consider this Python code snippet running on a Raspberry Pi with a PIR motion sensor connected to GPIO pin 17. What will be printed when motion is detected?
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.IN) try: while True: if GPIO.input(17): print("Motion Detected") else: print("No Motion") time.sleep(1) except KeyboardInterrupt: GPIO.cleanup()
Think about what the while True loop does and how GPIO.input(17) behaves when motion is detected.
The code continuously checks the PIR sensor input every second. When motion is detected, GPIO.input(17) returns True, so "Motion Detected" is printed repeatedly until motion stops.
When a PIR motion sensor is connected to a Raspberry Pi GPIO pin, what does the digital signal HIGH (1) from the sensor indicate?
Think about what a motion sensor is designed to detect and how it signals that event.
A PIR sensor outputs a HIGH signal when it detects motion in its sensing area. LOW means no motion detected.
Examine the code below. It raises a RuntimeError: 'No access to /dev/mem. Try running as root!'. What is the cause?
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.IN) if GPIO.input(17): print("Motion Detected") else: print("No Motion")
Consider what permissions are needed to access hardware pins on Raspberry Pi.
Accessing GPIO pins requires root privileges. Running the script without sudo causes this RuntimeError.
Choose the correct Python code snippet to set up GPIO pin 4 as input for a PIR sensor with an internal pull-down resistor enabled.
Remember the direction for sensor input and the correct pull resistor for PIR sensors.
PIR sensors usually require input mode with pull-down resistor to avoid floating signals. Option A correctly sets this.
Given this code snippet, how many times will "Motion Detected" be printed if motion is continuously detected for exactly 3 seconds?
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
start = time.time()
while time.time() - start < 3:
if GPIO.input(17):
print("Motion Detected")
time.sleep(0.5)Calculate how many 0.5 second intervals fit into 3 seconds.
The loop runs while time elapsed is less than 3 seconds. Each iteration waits 0.5 seconds. 3 / 0.5 = 6 iterations, so "Motion Detected" prints 6 times.