ESP8266 - Obstacle Sensor

This tutorial instructs you how to use ESP8266 with the infrared obstacle avoidance sensor. In detail, we will learn:

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×IR Obstacle Avoidance Sensor
1×Jumper Wires
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) ESP8266 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of IR Obstacle Avoidance Sensor

The infrared obstacle sensor is designed to detect the presence of any obstacle in its path by using an infrared signal. It has a detection range of 2cm to 30cm, which can be adjusted with a built-in potentiometer.

The Infrared Obstacle Avoidance Sensor Pinout

The IR obstacle avoidance sensor has three pins:

  • GND pin: must be connected to GND (0V)
  • VCC pin: must be connected to VCC (5V or 3.3v)
  • OUT pin: is an output pin, LOW when an obstacle is present, HIGH when there is none. This pin must be connected to an ESP8266 input pin.
IR Obstacle Avoidance Sensor pinout

How It Works

The infrared obstacle sensor module has a built-in infrared transmitter and receiver. The transmitter emits an infrared signal. The receiver looks for the reflected signal to detect if an object is present or not. The OUT pin indicates the presence of an obstacle:

  • If something is blocking the sensor, the OUT pin will be LOW
  • If nothing is blocking the sensor, the OUT pin will be HIGH

※ NOTE THAT:

During shipment, there is a possibility that this sensor may become deformed, which can result in malfunctioning. If you find that the sensor is not working properly, try adjusting the IR transmitter and receiver to align them parallel to each other.

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and IR Obstacle Avoidance Sensor

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

How To Program For IR Obstacle Avoidance Sensor

  • Initializes the ESP8266 pin to the digital input mode by utilizing the pinMode() function. For example, pin D7:
pinMode(D7, INPUT_PULLUP);
  • Read the status of the ESP8266 pin through the digitalRead() function.
int state = digitalRead(D7);

ESP8266 Code

There are two approaches to programming for an obstacle avoidance application:

  • Taking action when the obstacle is present or absent
  • Taking action when the obstacle is detected or cleared

ESP8266 code for checking if the obstacle is present

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-obstacle-sensor */ #define SENSOR_PIN D7 // The ESP8266 pin D7 connected to OUT pin of IR obstacle avoidance sensor void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); // initialize the ESP8266 NodeMCU's pin as an input 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); }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Copy the code and open it in Arduino IDE.
  • Click the Upload button to send the code to ESP8266.
  • Place an obstacle in front of the sensor and then take it away.
  • Check the result on Serial Monitor.
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  

ESP8266 code for detecting obstacle

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-obstacle-sensor */ #define SENSOR_PIN D7 // The ESP8266 pin D7 connected to OUT pin of IR obstacle avoidance sensor int prev_state = HIGH; // The previous state from the input pin int obstacle_state; // The current reading from the input pin void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); // initialize the ESP8266's pin as an input pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the the input pin: obstacle_state = digitalRead(SENSOR_PIN); if (prev_state == HIGH && obstacle_state == LOW) Serial.println("The obstacle is detected"); else if (prev_state == LOW && obstacle_state == HIGH) Serial.println("The obstacle is cleared"); delay(50); // save the the last state prev_state = obstacle_state; }

Detailed Instructions

  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Copy the code above and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the ESP8266.
  • Place an obstacle in front of the sensor for a while, then remove it.
  • Check the results in the Serial Monitor.
COM6
Send
The obstacle is detected The obstacle is cleared
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

Function References

Learn More