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 Dev 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
#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 the sensor value
if (sensorValue == HIGH) { // If no obstacle is detected
Serial.println("No Obstacle");
} else { // If an obstacle is detected
Serial.println("Obstacle Detected");
}
delay(500); // Wait for half a second before next reading
}
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.
Testing and Troubleshooting
- Ensure that the sensor is powered correctly (VCC to 5V 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.