Bird
0
0
Arduinoprogramming~20 mins

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

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