Bird
Raised Fist0
Arduinoprogramming~20 mins

IR sensor for obstacle detection in Arduino - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
IR Sensor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1:30remaining
What is the output of this IR sensor reading code?

Consider this Arduino code snippet that reads an IR sensor value and prints if an obstacle is detected.

const int irSensorPin = A0;
int sensorValue = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  sensorValue = analogRead(irSensorPin);
  if (sensorValue > 500) {
    Serial.println("Obstacle detected");
  } else {
    Serial.println("No obstacle");
  }
  delay(1000);
}

Assuming the sensor reads 600, what will be printed?

Arduino
const int irSensorPin = A0;
int sensorValue = 600;

void setup() {
  Serial.begin(9600);
}

void loop() {
  // sensorValue = analogRead(irSensorPin); // Simulated as 600
  if (sensorValue > 500) {
    Serial.println("Obstacle detected");
  } else {
    Serial.println("No obstacle");
  }
  delay(1000);
}
AError: sensorValue undefined
BNo obstacle
C600
DObstacle detected
Attempts:
2 left
💡 Hint

Check the condition comparing sensorValue to 500.

🧠 Conceptual
intermediate
1:00remaining
Which component is essential for IR obstacle detection?

Which of the following components is essential to detect obstacles using an IR sensor on Arduino?

AUltrasonic transmitter
BInfrared LED and photodiode
CTemperature sensor
DLight bulb
Attempts:
2 left
💡 Hint

Think about what emits and detects infrared light.

🔧 Debug
advanced
2:00remaining
Why does this IR sensor code not detect obstacles correctly?

Look at this Arduino code snippet:

const int irPin = A0;
int val = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  val = analogRead(irPin);
  if (val = 700) {
    Serial.println("Obstacle detected");
  } else {
    Serial.println("No obstacle");
  }
  delay(500);
}

Why does it always print "Obstacle detected" regardless of sensor value?

Arduino
const int irPin = A0;
int val = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  val = analogRead(irPin);
  if (val = 700) {
    Serial.println("Obstacle detected");
  } else {
    Serial.println("No obstacle");
  }
  delay(500);
}
AThe condition uses assignment (=) instead of comparison (==)
BThe sensor pin is not initialized
CSerial.begin is missing
DDelay is too short
Attempts:
2 left
💡 Hint

Check the if statement condition carefully.

📝 Syntax
advanced
1:30remaining
Which option fixes the syntax error in this IR sensor code?

Find the option that fixes the syntax error in this Arduino code:

void loop() {
  int sensorVal = analogRead(A0)
  if sensorVal > 400 {
    Serial.println("Obstacle");
  }
}
AChange analogRead to digitalRead
BRemove the if statement entirely
CAdd semicolon after analogRead line and parentheses around if condition
DAdd curly braces after analogRead
Attempts:
2 left
💡 Hint

Check missing punctuation and if statement syntax.

🚀 Application
expert
2:00remaining
How many times will "Obstacle detected" print in this loop?

Given this Arduino code snippet:

const int irPin = A0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  for (int i = 0; i < 5; i++) {
    int val = analogRead(irPin);
    if (val > 600) {
      Serial.println("Obstacle detected");
    }
  }
  delay(1000);
}

If the sensor reads 650 every time, how many times will "Obstacle detected" print each loop() call?

A5 times
B1 time
C0 times
DInfinite times
Attempts:
2 left
💡 Hint

Look at the for loop and condition inside it.

Practice

(1/5)
1. What does an IR sensor detect in an obstacle detection system?
easy
A. Visible light intensity
B. Magnetic fields around objects
C. Sound waves bouncing off objects
D. Reflected infrared light from nearby objects

Solution

  1. Step 1: Understand IR sensor function

    IR sensors emit infrared light and detect the light reflected back from objects.
  2. Step 2: Identify what is detected

    The sensor measures reflected infrared light, not visible light, sound, or magnetic fields.
  3. Final Answer:

    Reflected infrared light from nearby objects -> Option D
  4. Quick Check:

    IR sensor = reflected infrared light [OK]
Hint: IR sensors detect reflected infrared light, not visible or sound [OK]
Common Mistakes:
  • Confusing IR sensor with light or sound sensors
  • Thinking it detects visible light
  • Assuming it detects magnetic fields
2. Which Arduino code snippet correctly reads an analog IR sensor value from pin A0?
easy
A. int sensorValue = analogRead(A0);
B. int sensorValue = digitalRead(A0);
C. int sensorValue = analogWrite(A0);
D. int sensorValue = digitalWrite(A0);

Solution

  1. Step 1: Identify correct function for analog reading

    analogRead(pin) reads analog sensor values from specified pin.
  2. Step 2: Check pin and function usage

    Pin A0 is analog input; analogRead(A0) is correct syntax.
  3. Final Answer:

    int sensorValue = analogRead(A0); -> Option A
  4. Quick Check:

    Use analogRead for analog sensors [OK]
Hint: Use analogRead() for analog sensor pins like A0 [OK]
Common Mistakes:
  • Using digitalRead() for analog sensors
  • Using analogWrite() which is for output
  • Using digitalWrite() which is for digital output
3. What will be printed if the IR sensor value is 350 and the threshold is 300 in this code?
int sensorValue = 350;
int threshold = 300;
if(sensorValue > threshold) {
  Serial.println("No obstacle");
} else {
  Serial.println("Obstacle detected");
}
medium
A. Error
B. No obstacle
C. 350
D. Obstacle detected

Solution

  1. Step 1: Compare sensorValue with threshold

    350 is greater than 300, so condition sensorValue > threshold is true.
  2. Step 2: Determine printed output

    Since condition is true, code prints "No obstacle".
  3. Final Answer:

    No obstacle -> Option B
  4. Quick Check:

    350 > 300 = true, prints "No obstacle" [OK]
Hint: Check if sensor value is above threshold for output [OK]
Common Mistakes:
  • Confusing greater than with less than
  • Expecting sensor value to print
  • Thinking else block runs when condition is true
4. Identify the error in this Arduino code for obstacle detection:
int sensorPin = A0;
int threshold = 400;
void setup() {
  Serial.begin(9600);
}
void loop() {
  int sensorValue = digitalRead(sensorPin);
  if(sensorValue < threshold) {
    Serial.println("Obstacle detected");
  }
  delay(100);
}
medium
A. Using digitalRead instead of analogRead for analog sensor
B. Missing Serial.begin in setup()
C. Incorrect delay value
D. Threshold comparison operator is wrong

Solution

  1. Step 1: Check sensor reading function

    IR sensor outputs analog values, so analogRead() must be used, not digitalRead().
  2. Step 2: Verify other code parts

    Serial.begin is present, delay is reasonable, comparison operator is correct.
  3. Final Answer:

    Using digitalRead instead of analogRead for analog sensor -> Option A
  4. Quick Check:

    Analog sensor needs analogRead() [OK]
Hint: Use analogRead() for analog sensors, not digitalRead() [OK]
Common Mistakes:
  • Using digitalRead for analog sensor pins
  • Forgetting Serial.begin() (not the case here)
  • Misusing delay() values
5. You want to detect obstacles only when the IR sensor value is between 200 and 400. Which code snippet correctly implements this?
hard
A. if(sensorValue < 200 && sensorValue > 400) { Serial.println("Obstacle detected"); }
B. if(sensorValue >= 200 || sensorValue <= 400) { Serial.println("Obstacle detected"); }
C. if(sensorValue > 200 && sensorValue < 400) { Serial.println("Obstacle detected"); }
D. if(sensorValue == 200 && sensorValue == 400) { Serial.println("Obstacle detected"); }

Solution

  1. Step 1: Understand range condition

    To detect values between 200 and 400, sensorValue must be greater than 200 AND less than 400.
  2. Step 2: Analyze each option

    if(sensorValue < 200 && sensorValue > 400) { Serial.println("Obstacle detected"); }: impossible condition (< 200 && > 400); if(sensorValue >= 200 || sensorValue <= 400) { Serial.println("Obstacle detected"); } uses || which allows all values; if(sensorValue > 200 && sensorValue < 400) { Serial.println("Obstacle detected"); }: > 200 && < 400 correct; if(sensorValue == 200 && sensorValue == 400) { Serial.println("Obstacle detected"); } checks equality to both values simultaneously, impossible.
  3. Final Answer:

    if(sensorValue > 200 && sensorValue < 400) { Serial.println("Obstacle detected"); } -> Option C
  4. Quick Check:

    Use AND (&&) for range checks [OK]
Hint: Use && to check sensor value within a range [OK]
Common Mistakes:
  • Using || instead of && for range
  • Checking impossible conditions
  • Using equality instead of range
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