Bird
Raised Fist0
Arduinoprogramming~20 mins

PIR motion sensor in Arduino - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does a PIR motion sensor detect in an Arduino project?
easy
A. Light intensity changes
B. Sound levels in the environment
C. Movement by sensing infrared changes
D. Temperature variations

Solution

  1. Step 1: Understand PIR sensor function

    A PIR sensor detects motion by sensing changes in infrared radiation emitted by objects.
  2. Step 2: Compare with other options

    Sound, temperature, and light sensors detect different things, not motion via infrared.
  3. Final Answer:

    Movement by sensing infrared changes -> Option C
  4. Quick Check:

    PIR sensor = Motion detection [OK]
Hint: PIR sensors detect heat movement, not sound or light [OK]
Common Mistakes:
  • Confusing PIR with sound or light sensors
  • Thinking PIR detects temperature directly
2. Which Arduino code line correctly reads the PIR sensor connected to digital pin 7?
easy
A. int motion = digitalWrite(7);
B. int motion = digitalRead(7);
C. int motion = analogRead(7);
D. int motion = pinMode(7, INPUT);

Solution

  1. Step 1: Identify correct function to read digital input

    digitalRead(pin) reads the digital value from a pin, suitable for PIR sensor output.
  2. Step 2: Eliminate incorrect functions

    analogRead reads analog pins, digitalWrite sets output, pinMode sets pin mode but does not read value.
  3. Final Answer:

    int motion = digitalRead(7); -> Option B
  4. Quick Check:

    Use digitalRead for digital sensor input [OK]
Hint: Use digitalRead to get sensor state from digital pins [OK]
Common Mistakes:
  • Using analogRead on digital pin
  • Using digitalWrite instead of digitalRead
  • Confusing pinMode with reading value
3. What will be the output on the Serial Monitor when the following Arduino code runs and the PIR sensor detects motion?
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);
}
medium
A. Motion detected! printed every second when motion is present
B. No output because Serial.begin is missing
C. Motion detected! printed once and then stops
D. Syntax error due to missing semicolon

Solution

  1. 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.".
  2. Step 2: Check Serial setup and output

    Serial.begin(9600) is correctly called in setup, so output will appear on Serial Monitor continuously.
  3. Final Answer:

    Motion detected! printed every second when motion is present -> Option A
  4. Quick Check:

    Serial prints motion status each second [OK]
Hint: Loop prints sensor state every second with delay(1000) [OK]
Common Mistakes:
  • Thinking Serial.begin is missing
  • Assuming output prints only once
  • Confusing syntax errors
4. Identify the error in this Arduino code snippet using a PIR sensor on pin 9:
void setup() {
  pinMode(9, OUTPUT);
  Serial.begin(9600);
}
void loop() {
  int motion = digitalRead(9);
  if (motion == HIGH) {
    Serial.println("Motion detected");
  }
  delay(500);
}
medium
A. Pin 9 should be set as INPUT, not OUTPUT
B. Serial.begin should be in loop()
C. digitalRead cannot be used on pin 9
D. Missing semicolon after Serial.println

Solution

  1. Step 1: Check pinMode for sensor input

    PIR sensor output is digital input, so pin 9 must be set as INPUT, not OUTPUT.
  2. Step 2: Verify other code parts

    Serial.begin is correctly in setup, digitalRead works on pin 9, and semicolon is present.
  3. Final Answer:

    Pin 9 should be set as INPUT, not OUTPUT -> Option A
  4. Quick Check:

    Input pins must use pinMode(INPUT) [OK]
Hint: Set sensor pins as INPUT, not OUTPUT [OK]
Common Mistakes:
  • Setting sensor pin as OUTPUT
  • Moving Serial.begin to loop
  • Ignoring pinMode settings
5. You want to turn on an LED connected to pin 13 only when the PIR sensor on pin 2 detects motion. Which Arduino code snippet correctly implements this behavior?
hard
A. pinMode(2, INPUT); pinMode(13, OUTPUT); if (digitalRead(2) == LOW) { digitalWrite(13, HIGH); }
B. pinMode(2, OUTPUT); pinMode(13, INPUT); if (digitalRead(13) == HIGH) { digitalWrite(2, HIGH); }
C. pinMode(2, INPUT); pinMode(13, OUTPUT); if (digitalRead(2) == HIGH) { digitalWrite(13, HIGH); }
D. pinMode(2, INPUT); pinMode(13, OUTPUT); digitalWrite(13, digitalRead(2));

Solution

  1. Step 1: Set correct pin modes

    Pin 2 is sensor input, so INPUT; pin 13 is LED output, so OUTPUT.
  2. Step 2: Use digitalRead to control LED

    digitalWrite(13, digitalRead(2)) sets LED HIGH when sensor reads HIGH, LOW otherwise.
  3. 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.
  4. Final Answer:

    pinMode(2, INPUT); pinMode(13, OUTPUT); digitalWrite(13, digitalRead(2)); -> Option D
  5. Quick Check:

    LED state = PIR sensor state [OK]
Hint: Write LED pin to sensor pin state directly for simplicity [OK]
Common Mistakes:
  • Setting sensor pin as OUTPUT
  • Reversing HIGH/LOW logic
  • Confusing input/output pin modes
  • Omitting else clause to turn off LED
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard