0
0
Raspberry Piprogramming~20 mins

MotionSensor (PIR) in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Motion Sensor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output when the PIR sensor detects motion?

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?

Raspberry Pi
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()
ANo Motion (printed once when motion is detected)
BMotion Detected (printed repeatedly every second while motion is present)
CMotion Detected (printed once and program exits)
DNo output at all
Attempts:
2 left
💡 Hint

Think about what the while True loop does and how GPIO.input(17) behaves when motion is detected.

🧠 Conceptual
intermediate
1:30remaining
What does the PIR sensor output signal represent?

When a PIR motion sensor is connected to a Raspberry Pi GPIO pin, what does the digital signal HIGH (1) from the sensor indicate?

AThe sensor is in standby mode
BThe sensor is powered off
CThe sensor detected motion in its range
DThe sensor detected no motion
Attempts:
2 left
💡 Hint

Think about what a motion sensor is designed to detect and how it signals that event.

🔧 Debug
advanced
2:00remaining
Why does this PIR sensor code raise a RuntimeError?

Examine the code below. It raises a RuntimeError: 'No access to /dev/mem. Try running as root!'. What is the cause?

Raspberry Pi
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")
AThe PIR sensor is not connected to the correct pin
BThe GPIO library is not imported correctly
CThe GPIO pin number 17 is invalid on Raspberry Pi
DThe program is not run with root privileges required to access GPIO
Attempts:
2 left
💡 Hint

Consider what permissions are needed to access hardware pins on Raspberry Pi.

📝 Syntax
advanced
1:30remaining
Which option correctly sets up a PIR sensor input on GPIO pin 4 with pull-down resistor?

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.

AGPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
BGPIO.setup(4, GPIO.IN)
CGPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
DGPIO.setup(4, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN)
Attempts:
2 left
💡 Hint

Remember the direction for sensor input and the correct pull resistor for PIR sensors.

🚀 Application
expert
2:00remaining
How many times will 'Motion Detected' print if motion lasts 3 seconds?

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)
A6 times
B3 times
C4 times
D5 times
Attempts:
2 left
💡 Hint

Calculate how many 0.5 second intervals fit into 3 seconds.