How to Use Obstacle Avoidance Sensor with ESP8266 Node MCU

In this tutorial, I am going to show you how to use KY-032 obstacle avoidance sensors. These kinds of devices are distance-adjustable, infrared proximity sensors designed, mainly, for wheeled robots.

Parts Required

  • ESP8266 Node MCU;
  • Micro-USB to USB cable;
  • KY-032 obstacle avoidance sensor;
  • 1 LED;
  • 1 220Ω resistor;
  • Male-to-male jumper wires;
  • Male-to-female jumper wires;
  • 1 Breadboard;

Note: Since only 2 digital pins are required to follow this tutorial, an Arduino Nano, ESP32 NodeMCU board or any similar board can be used instead. Just be sure to change the pins in the code if necessary.

The KY-032 Obstacle Avoidance Sensor

Just as line tracking sensors, the working principle of these kinds of sensors is based on the use of infrared (IR) light to detect obstacles [1]. As seen in Figure 1, the KY-032 sensor module’s main components are:

  • IR LED – emits infrared light;
  • IR Receiver – receives infrared light reflected from obstacles;
  • 555 timer – commonly the NE555 timer, produces a square wave used to switch the IR LED on and off;
  • Frequency potentiometer – adjusts the frequency of the emitting IR pulse;
  • Distance potentiometer – adjusts the detection distance. This distance can be set to detect obstacles in a range between 2cm and 40 cm;
  • Enable jumper – makes the module permanently enabled so it’s always detecting obstacles.
Figure 1 – KY-032 obstacle avoidance sensor

The emitting LED sends infrared light pulses at a certain frequency (38kHz). When the light hits an obstacle, it is reflected back to the IR receiver.

One can adjust the detection distance by turning the left potentiometer. For maximum distance, the potentiometer must be in the middle position. Regarding the right potentiometer, it controls the frequency of the emitting IR pulse. Turn it clockwise all the way to set the emitter to the right frequency required to work with the receiver.

It is possible to programmatically enable and disable the sensor. To do so, one must remove the jumper and use the Enable (EN) pin of the sensor to control its operation. There are other versions of obstacle avoidance sensors that do not have the Enable pin, and thus do not have the enable jumper as well. Also, these sensors only have one potentiometer for distance detection adjustment, as seen in Figure 2.

Figure 2 – 3 pin obstacle avoidance sensor


From Figure 1, the KY-032 module has 4 pins:

  • VCC – Power supply (5V);
  • OUT – Sensor output;
  • GND – Ground (0V);
  • EN – Enable pin.

We can start by connecting the VCC and GND pins of the sensor to the Vin and GND pins of our ESP8266, respectively. Then, we can connect the OUT pin of the sensor to pin D2 of the ESP8266. To visually perceive the detection of an obstacle, one LED is added. Connect the anode of the LED to pin D3 of the ESP8266, the cathode of the LED to the 220 Ω resistor and its remaining terminal to ground. In the end, your circuit should be set as shown in Figure 3.

Figure 3 – Circuit schematics


Finally, it is time for some code. We can start by defining the desired port numbers and the system’s inputs (KY-032 sensor) and outputs (LED). Optionally, we can also initialize the serial communication with the PC, in order to check the output of the sensor over time. Then, according to what was previously mentioned, the LED is set on if an obstacle is detected and off otherwise.

#define sensorPin 4
#define ledPin 0

void setup(){
  // Define sensor as input and the LED as output
  pinMode(sensorPin, INPUT);
  pinMode(ledPin, OUTPUT);
  // Initialize serial communications with the PC

void loop(){
  int obstacle = digitalRead(sensorPin);
  // If an obstacle is detected
  if(obstacle == HIGH){
    digitalWrite(ledPin, HIGH);
    Serial.println("Obstacle detected");
  // If no obstacle is detected
  else {
    digitalWrite(ledPin, LOW);
    Serial.println("No obstacles detected");

That’s it! If you enjoyed this tutorial, you can visit our YouTube channel and watch our many tutorials. Thanks for following us and be sure to rate, comment and share our content.



Towards the Future !!! 😉

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
100% Free SEO Tools - Tool Kits PRO