Bird
0
0
Arduinoprogramming~20 mins

PIR motion sensor in Arduino - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PIR Sensor Mastery
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 Arduino code snippet using a PIR motion sensor connected to pin 7 and an LED on pin 13. What will be printed to the Serial Monitor when motion is detected?
Arduino
int pirPin = 7;
int ledPin = 13;

void setup() {
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int motion = digitalRead(pirPin);
  if (motion == HIGH) {
    digitalWrite(ledPin, HIGH);
    Serial.println("Motion detected!");
  } else {
    digitalWrite(ledPin, LOW);
    Serial.println("No motion.");
  }
  delay(1000);
}
AMotion detected! (printed repeatedly every second while motion is present)
BNo motion. (printed repeatedly every second regardless of motion)
CMotion detected! printed once, then no output
DNo output at all
Attempts:
2 left
💡 Hint
Think about what happens inside the loop when the sensor reads HIGH.
Predict Output
intermediate
2:00remaining
What happens if the PIR sensor pin is set as OUTPUT?
Given this Arduino code snippet, what will be the behavior of the PIR sensor reading?
Arduino
int pirPin = 7;

void setup() {
  pinMode(pirPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int motion = digitalRead(pirPin);
  Serial.println(motion);
  delay(1000);
}
AThe sensor reading will always be LOW (0)
BThe sensor reading will always be HIGH (1)
CThe sensor reading will be unpredictable and may cause errors
DThe code will not compile due to pinMode error
Attempts:
2 left
💡 Hint
Consider what happens when you set a sensor input pin as OUTPUT.
🔧 Debug
advanced
2:00remaining
Why does the LED stay on even without motion?
This Arduino code is supposed to turn on an LED when the PIR sensor detects motion, but the LED stays on even without motion. What is the cause?
Arduino
int pirPin = 7;
int ledPin = 13;

void setup() {
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int motion = digitalRead(pirPin);
  if (motion == HIGH) {
    digitalWrite(ledPin, HIGH);
    Serial.println("Motion detected!");
  } else {
    digitalWrite(ledPin, LOW);
    Serial.println("No motion.");
  }
  delay(1000);
}
AThe LED pin is not set as INPUT
BThe condition uses assignment (=) instead of comparison (==), so it always evaluates true
CThe PIR sensor pin is not set as OUTPUT
DThe Serial.begin baud rate is incorrect
Attempts:
2 left
💡 Hint
Check the if statement condition carefully.
📝 Syntax
advanced
2:00remaining
Which option fixes the syntax error in this PIR sensor code?
This code snippet has a syntax error. Which option fixes it?
Arduino
int pirPin = 7;
int ledPin = 13;

void setup() {
  pinMode(pirPin, INPUT)
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int motion = digitalRead(pirPin);
  if (motion == HIGH) {
    digitalWrite(ledPin, HIGH);
    Serial.println("Motion detected!");
  } else {
    digitalWrite(ledPin, LOW);
    Serial.println("No motion.");
  }
  delay(1000);
}
ARemove the Serial.begin(9600) line
BChange pinMode(pirPin, INPUT) to pinMode(pirPin, OUTPUT)
CAdd a semicolon after pinMode(pirPin, INPUT)
DChange delay(1000) to delay(1,000)
Attempts:
2 left
💡 Hint
Look for missing punctuation in the setup function.
🚀 Application
expert
3:00remaining
How to debounce PIR sensor signals in Arduino code?
PIR sensors sometimes give noisy signals causing flickering LED output. Which code snippet best implements a simple debounce to stabilize the LED output?
A
Use an interrupt on pirPin to toggle LED state:

volatile bool ledState = false;

void setup() {
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(pirPin), toggleLED, CHANGE);
  Serial.begin(9600);
}

void loop() {}

void toggleLED() {
  ledState = !ledState;
  digitalWrite(ledPin, ledState);
  Serial.println(ledState ? "Motion detected!" : "No motion.");
}
B
Add a delay(5000) inside loop to slow down sensor reading:

void loop() {
  int motion = digitalRead(pirPin);
  if (motion == HIGH) {
    digitalWrite(ledPin, HIGH);
    Serial.println("Motion detected!");
  } else {
    digitalWrite(ledPin, LOW);
    Serial.println("No motion.");
  }
  delay(5000);
}
C
Use analogRead on pirPin and check if value > 512:

void loop() {
  int motion = analogRead(pirPin);
  if (motion > 512) {
    digitalWrite(ledPin, HIGH);
    Serial.println("Motion detected!");
  } else {
    digitalWrite(ledPin, LOW);
    Serial.println("No motion.");
  }
}
D
Use a delay after detecting motion before reading sensor again:

int pirPin = 7;
int ledPin = 13;
unsigned long lastMotionTime = 0;
const unsigned long debounceDelay = 2000;

void setup() {
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int motion = digitalRead(pirPin);
  unsigned long currentTime = millis();
  if (motion == HIGH && (currentTime - lastMotionTime) > debounceDelay) {
    digitalWrite(ledPin, HIGH);
    Serial.println("Motion detected!");
    lastMotionTime = currentTime;
  } else if ((currentTime - lastMotionTime) > debounceDelay) {
    digitalWrite(ledPin, LOW);
    Serial.println("No motion.");
  }
}
Attempts:
2 left
💡 Hint
Debouncing means ignoring rapid repeated signals for a short time.