Bird
0
0
Arduinoprogramming~10 mins

PIR motion sensor in Arduino - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - PIR motion sensor
Start
Initialize PIR sensor pin
Read PIR sensor value
Is motion detected?
NoWait and read again
Yes
Turn ON LED or output
Wait some time
Turn OFF LED or output
Loop back to read sensor
The program initializes the PIR sensor pin, reads its value repeatedly, and if motion is detected, it turns on an LED for a short time before turning it off and continuing to check.
Execution Sample
Arduino
int pirPin = 2;
int ledPin = 13;

void setup() {
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  int val = digitalRead(pirPin);
  if (val == HIGH) {
    digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
  }
}
This code reads the PIR sensor on pin 2 and turns on the LED on pin 13 for 1 second when motion is detected.
Execution Table
StepActionpirPin ValueCondition (val == HIGH)LED StateDelay
1Read PIR sensorLOWFalseOFF0
2Check conditionLOWFalseOFF0
3No motion detected, loop againLOWFalseOFF0
4Read PIR sensorHIGHTrueOFF0
5Turn LED ONHIGHTrueON0
6Wait 1000 msHIGHTrueON1000
7Turn LED OFFHIGHTrueOFF0
8Loop back to read sensorUNKNOWNUnknownOFF0
💡 The loop runs forever, reading sensor and turning LED on/off when motion is detected.
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 5After Step 7Final
valundefinedLOWHIGHHIGHHIGHundefined
LED StateOFFOFFOFFONOFFOFF
Key Moments - 3 Insights
Why does the LED turn on only when val == HIGH?
Because the PIR sensor outputs HIGH when motion is detected, as shown in execution_table rows 4 and 5 where val is HIGH and LED turns ON.
What happens if the PIR sensor reads LOW?
The condition val == HIGH is false (rows 1-3), so the LED stays OFF and the program loops to read the sensor again.
Why is there a delay after turning the LED ON?
The delay keeps the LED ON for 1 second so the motion detection is visible, as shown in step 6 before turning LED OFF in step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the LED state at step 5?
ABlinking
BOFF
CON
DUndefined
💡 Hint
Check the LED State column at step 5 in the execution_table.
At which step does the PIR sensor detect motion (val == HIGH)?
AStep 4
BStep 7
CStep 1
DStep 3
💡 Hint
Look at the pirPin Value and Condition columns in the execution_table.
If the delay(1000) is removed, what changes in the execution_table?
ALED stays ON indefinitely
BLED turns ON and OFF immediately without visible delay
CPIR sensor stops detecting motion
DNo change at all
💡 Hint
Consider the Delay column and how it affects LED State timing.
Concept Snapshot
PIR motion sensor code:
- Initialize sensor pin as INPUT
- Read sensor value in loop
- If value is HIGH (motion detected), turn LED ON
- Wait some time (delay)
- Turn LED OFF
- Repeat forever
Full Transcript
This program uses a PIR motion sensor connected to pin 2 and an LED on pin 13. It reads the sensor value repeatedly. When the sensor detects motion, it outputs HIGH. The program checks if the sensor value is HIGH. If yes, it turns the LED ON, waits 1 second, then turns the LED OFF. If no motion is detected (sensor LOW), the LED stays OFF. This loop runs forever, continuously checking for motion and lighting the LED when motion is detected.