A PIR motion sensor detects movement by sensing changes in infrared light. It helps your Arduino know when someone or something moves nearby.
PIR motion sensor in Arduino
int sensorPin = 2; // PIR sensor output pin int ledPin = 13; // Built-in LED pin void setup() { pinMode(sensorPin, INPUT); pinMode(ledPin, OUTPUT); Serial.begin(9600); } void loop() { int sensorValue = digitalRead(sensorPin); if (sensorValue == HIGH) { digitalWrite(ledPin, HIGH); // Turn LED on Serial.println("Motion detected!"); } else { digitalWrite(ledPin, LOW); // Turn LED off Serial.println("No motion."); } delay(500); }
The PIR sensor output pin connects to a digital input pin on Arduino.
Use digitalRead() to check if motion is detected (HIGH) or not (LOW).
int pirPin = 7; void setup() { pinMode(pirPin, INPUT); Serial.begin(9600); } void loop() { if (digitalRead(pirPin) == HIGH) { Serial.println("Motion!"); } delay(1000); }
int pirPin = 3; int buzzerPin = 9; void setup() { pinMode(pirPin, INPUT); pinMode(buzzerPin, OUTPUT); } void loop() { if (digitalRead(pirPin) == HIGH) { digitalWrite(buzzerPin, HIGH); // Sound buzzer } else { digitalWrite(buzzerPin, LOW); // Stop buzzer } }
This program reads the PIR sensor connected to pin 2. When motion is detected, it lights up the built-in LED and prints "Motion detected!" to the serial monitor. When no motion is detected, the LED turns off and it prints "No motion." The delay slows down the readings to twice per second.
int pirPin = 2; // PIR sensor output pin int ledPin = 13; // Built-in LED pin void setup() { pinMode(pirPin, INPUT); pinMode(ledPin, OUTPUT); Serial.begin(9600); } void loop() { int motion = digitalRead(pirPin); if (motion == HIGH) { digitalWrite(ledPin, HIGH); // Turn LED on Serial.println("Motion detected!"); } else { digitalWrite(ledPin, LOW); // Turn LED off Serial.println("No motion."); } delay(500); }
Make sure the PIR sensor has time to calibrate after powering on (usually about 30 seconds).
Use the serial monitor in Arduino IDE to see printed messages.
The built-in LED on pin 13 is useful for quick testing without extra parts.
A PIR motion sensor detects movement by sensing infrared changes.
Connect the sensor output to a digital input pin and use digitalRead() to check for motion.
Use the sensor to trigger actions like turning on lights or sounding alarms.
Practice
Solution
Step 1: Understand PIR sensor function
A PIR sensor detects motion by sensing changes in infrared radiation emitted by objects.Step 2: Compare with other options
Sound, temperature, and light sensors detect different things, not motion via infrared.Final Answer:
Movement by sensing infrared changes -> Option CQuick Check:
PIR sensor = Motion detection [OK]
- Confusing PIR with sound or light sensors
- Thinking PIR detects temperature directly
Solution
Step 1: Identify correct function to read digital input
digitalRead(pin) reads the digital value from a pin, suitable for PIR sensor output.Step 2: Eliminate incorrect functions
analogRead reads analog pins, digitalWrite sets output, pinMode sets pin mode but does not read value.Final Answer:
int motion = digitalRead(7); -> Option BQuick Check:
Use digitalRead for digital sensor input [OK]
- Using analogRead on digital pin
- Using digitalWrite instead of digitalRead
- Confusing pinMode with reading value
const int pirPin = 8;
void setup() {
pinMode(pirPin, INPUT);
Serial.begin(9600);
}
void loop() {
int motion = digitalRead(pirPin);
if (motion == HIGH) {
Serial.println("Motion detected!");
} else {
Serial.println("No motion.");
}
delay(1000);
}Solution
Step 1: Analyze the loop behavior
The loop reads the PIR sensor pin every second and prints "Motion detected!" if motion is HIGH, else "No motion.".Step 2: Check Serial setup and output
Serial.begin(9600) is correctly called in setup, so output will appear on Serial Monitor continuously.Final Answer:
Motion detected! printed every second when motion is present -> Option AQuick Check:
Serial prints motion status each second [OK]
- Thinking Serial.begin is missing
- Assuming output prints only once
- Confusing syntax errors
void setup() {
pinMode(9, OUTPUT);
Serial.begin(9600);
}
void loop() {
int motion = digitalRead(9);
if (motion == HIGH) {
Serial.println("Motion detected");
}
delay(500);
}Solution
Step 1: Check pinMode for sensor input
PIR sensor output is digital input, so pin 9 must be set as INPUT, not OUTPUT.Step 2: Verify other code parts
Serial.begin is correctly in setup, digitalRead works on pin 9, and semicolon is present.Final Answer:
Pin 9 should be set as INPUT, not OUTPUT -> Option AQuick Check:
Input pins must use pinMode(INPUT) [OK]
- Setting sensor pin as OUTPUT
- Moving Serial.begin to loop
- Ignoring pinMode settings
Solution
Step 1: Set correct pin modes
Pin 2 is sensor input, so INPUT; pin 13 is LED output, so OUTPUT.Step 2: Use digitalRead to control LED
digitalWrite(13, digitalRead(2)) sets LED HIGH when sensor reads HIGH, LOW otherwise.Step 3: Eliminate other options
pinMode(2, OUTPUT); pinMode(13, INPUT); if (digitalRead(13) == HIGH) { digitalWrite(2, HIGH); } has wrong pin modes and logic. pinMode(2, INPUT); pinMode(13, OUTPUT); if (digitalRead(2) == HIGH) { digitalWrite(13, HIGH); } turns LED on for motion but lacks else clause to turn off when no motion. pinMode(2, INPUT); pinMode(13, OUTPUT); if (digitalRead(2) == LOW) { digitalWrite(13, HIGH); } reverses logic.Final Answer:
pinMode(2, INPUT); pinMode(13, OUTPUT); digitalWrite(13, digitalRead(2)); -> Option DQuick Check:
LED state = PIR sensor state [OK]
- Setting sensor pin as OUTPUT
- Reversing HIGH/LOW logic
- Confusing input/output pin modes
- Omitting else clause to turn off LED
Ready to prove it?
Pass the test and this pattern moves unknown
