Bird
Raised Fist0
Arduinoprogramming~10 mins

IR sensor for obstacle detection in Arduino - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - IR sensor for obstacle detection
Start
↓
Read IR sensor value
↓
Is value < threshold?
No→No obstacle detected
Yes↓
Obstacle detected
↓
Take action (e.g., LED ON)
↓
Repeat reading
The IR sensor reads distance values continuously. If the value is below a set threshold, it means an obstacle is close, triggering an action.
Execution Sample
Arduino
const int irPin = A0;
const int ledPin = 13;
int sensorValue;

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  sensorValue = analogRead(irPin);
  if(sensorValue < 300) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
  delay(100);
}
This code reads the IR sensor value and turns on an LED if an obstacle is detected (sensor value below 300).
Execution Table
StepActionsensorValueCondition (sensorValue < 300)LED State
1Read sensor value250TrueON
2Read sensor value320FalseOFF
3Read sensor value280TrueON
4Read sensor value350FalseOFF
5Read sensor value290TrueON
ExitLoop continues indefinitely---
💡 The loop runs forever, continuously checking sensor values and updating LED state.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
sensorValueundefined250320280350290last read value
LED StateOFFONOFFONOFFONdepends on last sensorValue
Key Moments - 3 Insights
Why does the LED turn ON when sensorValue is less than 300?
Because the code checks if sensorValue < 300 to detect an obstacle close by. When true, it turns the LED ON as shown in execution_table rows 1, 3, and 5.
What happens if the sensorValue is exactly 300?
The condition sensorValue < 300 is false if sensorValue is 300, so the LED stays OFF. This is shown in execution_table row 2 where 320 is false, similarly 300 would be false.
Why is there a delay(100) in the loop?
The delay slows down the loop so readings happen every 100 milliseconds, preventing too fast changes and giving time to see LED changes clearly.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the LED state when sensorValue is 320?
AON
BBlinking
COFF
DNo change
💡 Hint
Check execution_table row 2 where sensorValue is 320 and LED state is OFF.
At which step does the sensorValue first cause the LED to turn ON?
AStep 2
BStep 1
CStep 4
DStep 5
💡 Hint
Look at execution_table row 1 where sensorValue is 250 and LED turns ON.
If the threshold changed from 300 to 280, what would happen at step 3 where sensorValue is 280?
ALED would be OFF
BLED would be ON
CLED would blink
DNo change
💡 Hint
Since condition is sensorValue < threshold, if threshold is 280, 280 < 280 is false, so LED OFF.
Concept Snapshot
IR sensor reads analog values to detect obstacles.
If value is below a threshold, obstacle is near.
Use if condition to check sensorValue < threshold.
Turn LED ON when obstacle detected, OFF otherwise.
Loop reads sensor repeatedly with delay for stability.
Full Transcript
This Arduino program uses an IR sensor connected to analog pin A0 to detect obstacles. The sensor value is read continuously inside the loop. If the sensor value is less than 300, it means an obstacle is close, so the program turns on an LED connected to pin 13. Otherwise, the LED is turned off. The loop repeats every 100 milliseconds to update the LED state based on the latest sensor reading. This simple logic helps detect obstacles using the IR sensor and gives visual feedback with the LED.

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