ESP32 - Infrared Obstacle Avoidance Sensor

This tutorial instructs you on how to use ESP32 with the infrared obstacle avoidance sensor to detect the presence of the obstacle.

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×IR Obstacle Avoidance Sensor
n×Jumper Wires
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

Introduction to IR Obstacle Avoidance Sensor

The IR (infrared) obstacle sensor is used to if any obstacle is present in front of the sensor module by using the IR signal. The detection range is from 2cm to 30cm and is adjustable by a built-in potentiometer.

Pinout

An IR obstacle avoidance sensor has three pins:

  • GND pin: connect this pin to GND (0V)
  • VCC pin: connect this pin to VCC (5V or 3.3v)
  • OUT pin: is an output pin: LOW if an obstacle is present, HIGH if no obstacle is present. This pin needs to be connected to ESP32's input pin.
  • IR Obstacle Avoidance Sensor Pinout

How It Works

An infrared obstacle sensor module consists of an IR transmitter and an IR receiver. The IR transmitter emits the IR signal while the IR receiver searches for the reflected IR signal to determine if the object is present or not. The presence of obstacle is reflected in the OUT pin:

  • If the obstacle is present, the sensor's OUT pin is LOW
  • If the obstacle is NOT present, the sensor's OUT pin is HIGH

Wiring Diagram

wiring diagram between esp32 ir obstacle avoidance sensor

This image is created using Fritzing. Click to enlarge image

How To Program For IR Obstacle Avoidance Sensor

  • Initializes the ESP32 pin to the digital input mode by using pinMode() function. For example, pin 8
pinMode(8, INPUT_PULLUP);
  • Reads the state of the ESP32 pin by using digitalRead() function.
int state = digitalRead(8);

ESP32 Code

There are two use cases to implement an obstacle avoidance application:

  • Take actions while the obstacle is present or not present
  • Take actions when the obstacle is detected or cleared

ESP32 code for checking if the obstacle is present

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-infrared-obstacle-avoidance-sensor */ #define SENSOR_PIN 18 // ESP32 pin GIOP18 connected to OUT pin of IR obstacle avoidance sensor void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the Arduino's pin as aninput pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the the input pin: int state = digitalRead(SENSOR_PIN); if (state == LOW) Serial.println("The obstacle is present"); else Serial.println("The obstacle is NOT present"); delay(100); }

Quick Instructions

  • If this is the first time you use ESP32, see how to set up environment for ESP32 on Arduino IDE.
  • Copy the above code and paste it to Arduino IDE.
  • Compile and upload code to ESP32 board by clicking Upload button on Arduino IDE
  • Place an obstacle in front of the sensor for a while, and then withdraw it.
  • See the result on Serial Monitor. It looks like the below:
  • COM6
    Send
    The obstacle is NOT present The obstacle is NOT present The obstacle is NOT present The obstacle is NOT present The obstacle is NOT present The obstacle is present The obstacle is present The obstacle is present The obstacle is present The obstacle is NOT present The obstacle is NOT present
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

ESP32 code for detecting obstacle

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-infrared-obstacle-avoidance-sensor */ #define SENSOR_PIN 18 // ESP32 pin GIOP18 connected to OUT pin of IR obstacle avoidance sensor int lastState = HIGH; // the previous state from the input pin int currentState; // the current reading from the input pin void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the ESP32's pin as an input pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the the input pin: currentState = digitalRead(SENSOR_PIN); if (lastState == HIGH && currentState == LOW) Serial.println("The obstacle is detected"); else if (lastState == LOW && currentState == HIGH) Serial.println("The obstacle is cleared"); delay(50); // save the the last state lastState = currentState; }

Quick Instructions

  • Copy the above code and paste it to Arduino IDE.
  • Compile and upload code to ESP32 board by clicking Upload button on Arduino IDE
  • Place an obstacle in front of the sensor for a while, and then withdraw it.
  • See the result on Serial Monitor. It looks like the below:
  • COM6
    Send
    The obstacle is detected The obstacle is cleared
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ NOTICES