IR Obstacle Avoidance Sensor Module Tutorial: Reflective IR Detection, Threshold Tuning, and Arduino Code

Beginner Tutorial Views: 5117

This tutorial is a comprehensive, practical guide to the IR Infrared Obstacle Avoidance Sensor Module (Leobot Product #266). You will learn how the module detects nearby objects using reflected IR light, how the onboard <strong>LM393 comparator</strong> produces a clean digital output, how to tune the sensitivity for a stable detection window, and how to integrate one or many sensors into Arduino-based robot cars and obstacle detection systems.

IR Obstacle Avoidance Sensor Module Tutorial: Reflective IR Detection, Threshold Tuning, and Arduino Code

This tutorial is a comprehensive, practical guide to the IR Infrared Obstacle Avoidance Sensor Module (Leobot Product #266). You will learn how the module detects nearby objects using reflected IR light, how the onboard LM393 comparator produces a clean digital output, how to tune the sensitivity for a stable detection window, and how to integrate one or many sensors into Arduino-based robot cars and obstacle detection systems.

Tutorial Beginner ? Intermediate Infrared Obstacle Avoidance LM393 Robotics Arduino Sensors
What this module is: A reflective IR proximity detector. An IR LED emits infrared light and a receiver senses how much IR is reflected back from an object. The onboard comparator converts that into a stable HIGH/LOW output. Typical detection is roughly 2–30cm with a detection angle around 35° (both depend heavily on surface and ambient light).
Reality check: This is not a true distance sensor. It is a threshold detector. It tells you “object present / not present” within a tunable range, but it does not measure precise centimeters.

1) What the module does (and does not do)

This IR obstacle module is designed to detect whether an object is present in front of it within an adjustable range. It is widely used for robot cars, collision avoidance, and simple “near object” triggers.

Use it when

  • You want a low-cost “something is in front of me” sensor
  • You can control the environment (indoor lighting, consistent surfaces)
  • You need multiple sensors around a robot (front/left/right) with simple logic

Do not use it when

  • You need accurate distance measurement (use ultrasonic ToF/LiDAR instead)
  • You need reliable outdoor sensing in sunlight without additional shielding
  • You need consistent results on black matte surfaces or very angled/absorbent targets

2) How reflective IR obstacle detection works

The module contains an IR emitter and an IR receiver aimed in the same direction. When an object is in front of the sensor, IR light reflects back into the receiver. The receiver signal is then compared against an adjustable threshold, producing a digital output.

  • White / glossy surfaces typically reflect strongly ? easier detection
  • Black / matte surfaces absorb IR ? detection range drops
  • Angled surfaces can reflect IR away from the receiver ? unstable detection

3) Key specs and practical expectations

  • Detection distance: ~2 to 30 cm (tunable; strongly surface-dependent)
  • Detection angle: ~35° (field of view)
  • Comparator chip: LM393
  • Mounting: 3mm screw holes for easy mounting
  • Interface: single digital signal pin to your microcontroller
Practical expectation: Design your robot so you can tune the threshold under the actual lighting and on the actual floor/walls. Treat the “30cm” end of the range as optimistic for reflective targets and controlled lighting.

4) Pinout and electrical interface

Most IR obstacle avoidance modules are 3-pin with a single digital output:

Pin Typical Label Description
1VCCSupply voltage (typically 3.3V–5V)
2GNDGround
3OUT / DODigital output (HIGH/LOW depending on detection)
Output polarity varies by board: Some boards output LOW when obstacle detected, others output HIGH when detected. Always verify once in code by printing the pin state while moving an object in/out.

5) LM393 comparator and sensitivity adjustment

The onboard trimpot sets the comparator threshold: effectively your detection distance/sensitivity. Turning the trimpot changes when the output flips.

How to tune properly

  1. Power the module and connect OUT to your Arduino input.
  2. Open Serial Monitor and print the digital state continuously.
  3. Place the target obstacle at your desired detection distance.
  4. Turn the trimpot slowly until the output switches cleanly.
  5. Verify it does not chatter when the robot vibrates or turns.
Calibration tip: Tune for stability first, then distance. A slightly shorter detection distance that never false-triggers is usually better for real robots than a “max range” setting that chatters.

6) Wiring to Arduino


IR Module VCC  ? Arduino 5V  (or 3.3V if your board is 3.3V)
IR Module GND  ? Arduino GND
IR Module OUT  ? Arduino D2  (any digital input pin)
    
Power integrity: If you have multiple sensors and motors, don’t power everything from a weak USB port. Motors cause dips and noise. Use a proper supply and keep grounds common.

7) Arduino Example 1: Basic obstacle detection

This sketch prints the raw digital output and a human-readable “DETECTED / CLEAR” message. It also auto-detects polarity during setup by letting you observe the printed values.


/*
  IR Obstacle Avoidance Sensor - Basic Read
  Product #266
*/

const int IR_PIN = 2;

void setup() {
  pinMode(IR_PIN, INPUT);
  Serial.begin(9600);
  Serial.println("IR obstacle sensor test: move an object in/out and observe pin state.");
}

void loop() {
  int v = digitalRead(IR_PIN);

  Serial.print("IR_PIN=");
  Serial.print(v);
  Serial.print("  |  ");
  Serial.println(v == LOW ? "STATE=LOW" : "STATE=HIGH");

  delay(200);
}
    

8) Arduino Example 2: Stable detection (debounce + hold)

In real robots, vibration and shiny edges can cause flicker. This example adds:

  • A small debounce window
  • A “hold time” so once detected, it stays detected briefly

/*
  IR Obstacle Avoidance Sensor - Stable Detection
  - Assumes ACTIVE-LOW when obstacle detected (common on LM393 boards)
  - If your board is opposite, flip the condition.
*/

const int IR_PIN = 2;

const unsigned long DEBOUNCE_MS = 30;
const unsigned long HOLD_MS     = 200;

int lastRaw = HIGH;
int stableRaw = HIGH;
unsigned long lastChangeMs = 0;

unsigned long holdUntilMs = 0;

bool isObstacleActiveLow(int raw) { return raw == LOW; }

void setup() {
  pinMode(IR_PIN, INPUT);
  Serial.begin(9600);
}

void loop() {
  unsigned long now = millis();
  int raw = digitalRead(IR_PIN);

  // debounce raw -> stableRaw
  if (raw != lastRaw) {
    lastRaw = raw;
    lastChangeMs = now;
  }
  if (now - lastChangeMs >= DEBOUNCE_MS) {
    stableRaw = lastRaw;
  }

  // hold logic
  bool detected = isObstacleActiveLow(stableRaw);
  if (detected) {
    holdUntilMs = now + HOLD_MS;
  }
  bool detectedHeld = (now <= holdUntilMs);

  Serial.print("raw=");
  Serial.print(raw);
  Serial.print(" stable=");
  Serial.print(stableRaw);
  Serial.print(" detected=");
  Serial.print(detected ? "YES" : "NO");
  Serial.print(" held=");
  Serial.println(detectedHeld ? "YES" : "NO");

  delay(50);
}
    

9) Arduino Example 3: Multiple sensors for robot cars

The product description emphasizes that you can add many sensors (one input pin each). This example reads three sensors: left, center, and right. Use it for front bumper sensing or multi-direction avoidance.


/*
  3x IR Obstacle Sensors (L/C/R) - Simple Readout
  Adjust pin numbers to suit your wiring.
  Assumes ACTIVE-LOW (LOW = obstacle).
*/

const int IR_L = 2;
const int IR_C = 3;
const int IR_R = 4;

bool activeLowDetected(int pin) {
  return digitalRead(pin) == LOW;
}

void setup() {
  pinMode(IR_L, INPUT);
  pinMode(IR_C, INPUT);
  pinMode(IR_R, INPUT);
  Serial.begin(9600);
}

void loop() {
  bool L = activeLowDetected(IR_L);
  bool C = activeLowDetected(IR_C);
  bool R = activeLowDetected(IR_R);

  Serial.print("L=");
  Serial.print(L ? "1" : "0");
  Serial.print(" C=");
  Serial.print(C ? "1" : "0");
  Serial.print(" R=");
  Serial.println(R ? "1" : "0");

  delay(100);
}
    

10) Simple obstacle-avoidance decision logic

Below is a typical “policy” you can apply once you have left/center/right detection. It’s not motor code; it’s decision logic you can connect to your motor driver layer.


/*
  Decision logic sketch (pseudo-control)
  Inputs: L, C, R booleans
*/

if (C) {
  // obstacle ahead
  if (!L && R) {
    // turn left
  } else if (L && !R) {
    // turn right
  } else {
    // reverse + random turn
  }
} else {
  // path clear
  if (L && !R) {
    // drift slightly right
  } else if (!L && R) {
    // drift slightly left
  } else {
    // go forward
  }
}
    
Best practice: Even if you only need “avoid obstacle,” keeping left/center/right sensing gives you dramatically better navigation than a single front sensor.

11) Mounting, angle, and real-world tuning

  • Mount the module rigidly using the 3mm screw holes.
  • Keep the sensor level; changes in tilt change reflection geometry.
  • For robot cars, position sensors low enough to “see” obstacles but high enough to avoid floor reflections dominating.
  • Set the threshold using the actual obstacles and floor material you will run on.

12) Interference, sunlight, and cross-talk

  • Sunlight contains IR and can saturate the receiver; add shielding or operate indoors for best reliability.
  • Very shiny surfaces can cause longer-range false detections; tune threshold down and add debounce/hold.
  • Multiple sensors close together can sometimes “see” each other’s IR; physically separate them or angle them slightly outward.
  • Motor noise can cause false triggers if your 5V rail is unstable; use a proper power supply and good grounding.

13) Troubleshooting

Always detects obstacle

  • Threshold set too sensitive (turn trimpot to reduce sensitivity).
  • Sensor aimed at a very reflective surface too close.
  • Ambient IR (sunlight) saturating the receiver.

Never detects obstacle

  • Threshold set too strict (increase sensitivity).
  • Target is black/matte or angled (reduce distance, change angle).
  • Incorrect wiring or wrong assumption about output polarity.

Chattering / flickering output

  • Robot vibration or uneven surfaces (add debounce + hold in code).
  • Power rail noise (separate motor power, add bulk capacitance).
  • Too-close-to-threshold tuning (re-tune for a clean switch point).

14) Quick checklist


IR Obstacle Avoidance Sensor Module (#266) Checklist
---------------------------------------------------
? Wire VCC, GND, and OUT (single digital pin)
? Verify output polarity (LOW/HIGH when obstacle is present)
? Tune threshold under real lighting and using real obstacles
? Use rigid mounting; keep angle consistent
? Add debounce/hold for vibration and glossy surfaces
? Avoid direct sunlight or add shielding for outdoor use
? Use stable power when motors are present; share common ground
? Consider left/center/right sensors for robust robot navigation
    

Products that this may apply to