0
0
Raspberry Piprogramming~10 mins

MotionSensor (PIR) in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - MotionSensor (PIR)
Start Program
Setup GPIO Pin
Wait for Motion
Motion Detected?
NoKeep Waiting
Yes
Print "Motion Detected!"
Repeat Waiting
The program sets up the sensor pin, waits for motion, prints a message when motion is detected, then continues waiting.
Execution Sample
Raspberry Pi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)

while True:
    if GPIO.input(17):
        print("Motion Detected!")
        time.sleep(1)
This code waits for motion on GPIO pin 17 and prints a message when motion is detected.
Execution Table
StepGPIO.input(17)Condition (Motion Detected?)ActionOutput
1FalseNoWait
2FalseNoWait
3TrueYesPrint messageMotion Detected!
4FalseNoWait
5TrueYesPrint messageMotion Detected!
💡 Program runs indefinitely, continuously checking for motion.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
GPIO.input(17)N/AFalseFalseTrueFalseTrue
Key Moments - 2 Insights
Why does the program keep printing "Motion Detected!" multiple times?
Because the loop continuously checks the sensor state and prints whenever motion is detected (see execution_table steps 3 and 5). To avoid repeated prints, you would add a delay or state check.
What happens if GPIO.input(17) is False?
The program does nothing and keeps waiting (see execution_table steps 1, 2, and 4). It only acts when the sensor detects motion.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of GPIO.input(17) at step 3?
AFalse
BTrue
CNone
DError
💡 Hint
Check the 'GPIO.input(17)' column at step 3 in the execution_table.
At which step does the program print "Motion Detected!" for the first time?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Output' column in the execution_table to find when the message first appears.
If GPIO.input(17) never becomes True, what will the program do?
AKeep waiting without printing
BPrint "Motion Detected!" once
CPrint "Motion Detected!" repeatedly
DCrash with error
💡 Hint
Refer to execution_table steps where GPIO.input(17) is False and see the action taken.
Concept Snapshot
MotionSensor (PIR) on Raspberry Pi:
- Setup GPIO pin as input
- Loop: read sensor state
- If motion detected (input True), print message
- Repeat indefinitely
- Use delays or state flags to avoid repeated prints
Full Transcript
This program uses a PIR motion sensor connected to GPIO pin 17 on a Raspberry Pi. It sets up the pin as input and enters an infinite loop. Each loop iteration reads the sensor state. If motion is detected (GPIO.input(17) returns True), it prints "Motion Detected!". Otherwise, it waits silently. The program keeps checking continuously, printing the message each time motion is detected. Beginners often wonder why the message prints multiple times; this happens because the loop runs fast and prints every time the sensor is active. To prevent this, you can add a delay or track previous state. The execution table shows the sensor state and program actions step by step.