Challenge - 5 Problems
PIR Sensor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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); }
Attempts:
2 left
💡 Hint
Think about what happens inside the loop when the sensor reads HIGH.
✗ Incorrect
When the PIR sensor detects motion, digitalRead returns HIGH. The code turns on the LED and prints "Motion detected!" every second while motion continues.
❓ Predict Output
intermediate2: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); }
Attempts:
2 left
💡 Hint
Consider what happens when you set a sensor input pin as OUTPUT.
✗ Incorrect
Setting the PIR sensor pin as OUTPUT means the Arduino controls the pin voltage. digitalRead will read the output state, which defaults LOW unless set HIGH. Since the code never sets it HIGH, it reads LOW always.
🔧 Debug
advanced2: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); }
Attempts:
2 left
💡 Hint
Check the if statement condition carefully.
✗ Incorrect
The if condition uses a single equals sign (=), which assigns HIGH to motion instead of comparing. This makes the condition always true, so the LED stays on regardless of sensor input.
📝 Syntax
advanced2: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); }
Attempts:
2 left
💡 Hint
Look for missing punctuation in the setup function.
✗ Incorrect
The line 'pinMode(pirPin, INPUT)' is missing a semicolon at the end, causing a syntax error. Adding the semicolon fixes it.
🚀 Application
expert3: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?
Attempts:
2 left
💡 Hint
Debouncing means ignoring rapid repeated signals for a short time.
✗ Incorrect
Option D uses a timer to ignore repeated motion signals within 2 seconds, stabilizing the LED output and avoiding flicker caused by noisy sensor signals.
