Index
Introduction
The IR Infrared Obstacle Avoidance Sensor detects obstacles by emitting infrared light and measuring the reflected signal.
It provides a digital output (HIGH or LOW) depending on whether an object is detected.
The ESP32 can read the sensor’s output and take appropriate action, such as stopping or turning a motor.
In this tutorial, we’ll interface the IR sensor with the ESP32 to detect obstacles.
Required Components
- ESP32 Board
- IR Sensor
- Jumper wires
- Breadboard (optional)
Pinout

Circuit Diagram / Wiring
- IR SENSOR VCC → VIN, 3.3V (ESP32)
- IR SENSOR GND → GND (ESP32)
- IR SENSOR OUT → GPIO 15 (ESP32)

Code / Programming
/*
Filename: ol_ir_obstacle_sensor.ino
Description: Reads IR obstacle sensor and prints detection status via Serial Monitor
Author: www.oceanlabz.in
Modification: 1/4/2025
*/
#define IR_SENSOR_PIN 15 // Define IR sensor pin
void setup() {
Serial.begin(115200); // Start serial communication
pinMode(IR_SENSOR_PIN, INPUT); // Set IR sensor pin as input
}
void loop() {
int sensorValue = digitalRead(IR_SENSOR_PIN); // Read sensor value
if (sensorValue == HIGH) {
Serial.println("No Obstacle"); // No obstacle detected
} else {
Serial.println("Obstacle Detected"); // Obstacle detected
}
delay(500); // Wait for 500 milliseconds
}
Explanation
- The
IR_SENSOR_PIN
reads a digital HIGH or LOW value depending on obstacle detection. - If the value is HIGH, no obstacle is detected; if LOW, an obstacle is present.
- The sensor value is printed to the Serial Monitor for feedback.
Troubleshooting
- Ensure that the sensor is powered correctly (VCC to 3.3V, VIN and GND to GND) and that the output pin is connected to the ESP32.
- If the sensor continuously reads “Obstacle Detected,” check if the sensor is too close to a surface or incorrectly calibrated.
- If no readings are displayed, ensure the sensor’s wiring is secure and the ESP32’s GPIO is set to INPUT.
Project 1: ESP32 IR Sensor with Stable Signal Detection (Debounce Filter)
Introduction
This project uses an IR sensor with ESP32 to detect objects or motion with improved accuracy.
It implements a software debounce technique, ensuring signal changes are only accepted if stable for 500ms.
This helps filter out false triggers or noise from quick or shaky signals.
The stable HIGH or LOW state is printed to the Serial Monitor when a valid change occurs.
Required Components
- ESP32 Board
- IR Sensor
- Jumper wires
- Breadboard (optional)
- Wi-Fi Network (2.4 GHz)
Code / Programming
/*
Filename: ol_ir_time_debounce.ino
Description: Reads IR sensor input and only considers it HIGH or LOW if stable for 500ms
Author: www.oceanlabz.in
Modification: 1/4/2025
*/
#define SENSOR_PIN 15 // GPIO pin connected to IR sensor
#define STABLE_TIME 500 // Duration in ms for a stable reading
unsigned long lastChangeTime = 0;
bool lastState = LOW;
bool stableState = LOW;
void setup() {
Serial.begin(115200);
pinMode(SENSOR_PIN, INPUT);
Serial.println("Starting IR sensor stable detection...");
}
void loop() {
bool currentState = digitalRead(SENSOR_PIN);
unsigned long currentTime = millis();
// Detect if the signal has changed
if (currentState != lastState) {
lastChangeTime = currentTime; // Reset the timer
lastState = currentState; // Update last state
}
// Check if the signal has remained unchanged for at least STABLE_TIME
if ((currentTime - lastChangeTime) >= STABLE_TIME) {
if (stableState != currentState) {
stableState = currentState;
Serial.print("Stable state changed to: ");
Serial.println(stableState ? "HIGH" : "LOW");
}
}
delay(10); // Optional: reduce CPU usage and avoid rapid looping
}
Explanation
- The IR sensor detects objects by outputting HIGH or LOW signals to the ESP32.
- A 500ms debounce ensures only stable signal changes are considered valid.
- The ESP32 prints the stable state to the Serial Monitor when a confirmed change occurs.
Troubleshooting
- No output? Check sensor wiring and ensure it’s connected to the correct GPIO pin.
- Too sensitive or delayed? Adjust the
STABLE_TIME
value for faster or slower response. - Unstable readings? Make sure the sensor has a clear view and isn’t affected by external IR sources.