Bird
0
0
Arduinoprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - IR sensor for obstacle detection
Start
Read IR sensor value
Is value < threshold?
NoNo 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.