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.
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
4) Pinout and electrical interface
Most IR obstacle avoidance modules are 3-pin with a single digital output:
| Pin | Typical Label | Description |
|---|---|---|
| 1 | VCC | Supply voltage (typically 3.3V–5V) |
| 2 | GND | Ground |
| 3 | OUT / DO | Digital output (HIGH/LOW depending on detection) |
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
- Power the module and connect OUT to your Arduino input.
- Open Serial Monitor and print the digital state continuously.
- Place the target obstacle at your desired detection distance.
- Turn the trimpot slowly until the output switches cleanly.
- Verify it does not chatter when the robot vibrates or turns.
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)
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
}
}
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