ESP32 UNO - Obstacle Avoidance Sensor

Need your ESP32 Uno to notice a hand, a box or a wall a few centimeters away? An IR obstacle avoidance sensor does exactly that, and the ESP32 Uno form board reads it with one digital pin. Hook up the infrared obstacle detector, upload a short Arduino IDE sketch, and watch "present" or "detected" messages appear as objects come and go.

By the end, you can:

  1. Explain how an infrared obstacle sensor turns reflected light into a HIGH or LOW signal
  2. Wire the IR obstacle detector to the ESP32 Uno with three jumper wires
  3. Read the module state with digitalRead() in Arduino code
  4. Print a message only at the moment an object arrives or leaves
ESP32 Uno IR obstacle avoidance sensor

Components You Need

1×ESP32 Uno-form Board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×IR Obstacle Avoidance Sensor
1×Alternatively, TCRT5000 Obstacle Avoidance Sensor
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
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.
Additionally, some of these links are for products from our own brand, DIYables .

Meet the IR Obstacle Avoidance Sensor

After this part, you know what the module senses and how far it reaches.

Property Value
Sensing method Reflected infrared light
Detection range About 2cm to 30cm
Range adjustment Onboard potentiometer
Supply voltage 3.3V or 5V
Output type Digital, active LOW

Robot cars, people counters and no-touch switches use this module a lot, since it is cheap and needs no library.

Pinout

This part tells you where each of the three pins goes.

Pin Role Connect To
GND Ground (0V) GND of the ESP32 Uno
VCC Power input 3.3V of the ESP32 Uno
OUT Digital result, LOW = object, HIGH = clear A digital input pin of the ESP32 Uno
IR Obstacle Avoidance Sensor Pinout
image source: diyables.io

How It Works

Here you see why the OUT pin drops to LOW when something is near.

The board carries two parts side by side: an IR emitter LED and an IR receiver. The emitter shines invisible light forward all the time. If an object sits in front, part of that light reflects back and hits the receiver. A small comparator on the module then sets the OUT pin:

Situation OUT Pin Level
Object in front of the module LOW
Nothing in front of the module HIGH

Good to know: the turn-pot sets the comparator level, so it decides how much reflected light counts as "object". Light, shiny surfaces reflect far better than black ones.

※ NOTE THAT:

The emitter and receiver legs can get bent in the box during delivery. When that happens, the module may miss objects or see them all the time. Bend both parts gently until they face straight forward and stand parallel.

ESP32 Uno Pinout

Every Uno header label on the ESP32 Uno form board belongs to one GPIO number, and that number is what goes into your sketch. Find D7 on the diagram below before you plug in the OUT wire.

ESP32 Uno pinout diagram

How to Connect

Once these three wires are in place, the module gets power and sends its result to D7.

The wiring diagram between ESP32 Uno IR Obstacle Avoidance Sensor

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

The wiring diagram may not show the correct pin. On the ESP32 Uno, the OUT wire goes to D7 (GPIO14), not D8 as the picture may show. We will update the diagram soon. Until then, follow the wiring table and the code on this page, not the picture.

Obstacle Sensor Pin ESP32 Uno Pin
VCC 3.3V
GND GND
OUT D7 (GPIO14)

Good to know: ESP32 Uno pins use 3.3V logic and are NOT 5V tolerant, so feed the module from 3.3V. Its OUT pin then never goes above 3.3V.

WARNING

If you power the module from 5V, its OUT pin sends 5V into GPIO14 and can damage it. Stay with 3.3V, or put a voltage divider (for example 10kΩ and 20kΩ) on the OUT line.

Reading the Sensor in Code

This part gives you the two lines that every obstacle sketch needs.

Step 1 — Make the pin an input

Use pinMode(). The wire is on D7, and D7 is GPIO14, so write 14.

pinMode(14, INPUT_PULLUP);

Step 2 — Read the level

Use digitalRead(). A LOW result means an object is there.

int state = digitalRead(14);

ESP32 Uno Code

Pick the sketch that matches your goal:

Goal Sketch to Use
React to the current state (object there or not) Check if the obstacle is present
React only to a change (object arrives or leaves) Detect the obstacle

ESP32 Uno code to check if an obstacle is present

This sketch reads GPIO14 every 100ms and prints the state each time.

/* * This ESP32 Uno code was developed by newbiely.com * * This ESP32 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-uno/esp32-uno-obstacle-avoidance-sensor */ #define SENSOR_PIN 14 // D7 (GPIO14) reads the OUT pin of the IR obstacle avoidance sensor void setup() { // start the Serial Monitor link at 9600 baud Serial.begin(9600); // make the ESP32 Uno sensor pin a digital input pinMode(SENSOR_PIN, INPUT); } void loop() { // get the current level of the sensor output int state = digitalRead(SENSOR_PIN); if (state == LOW) Serial.println("The obstacle is present"); else Serial.println("The obstacle is NOT present"); delay(100); }

Upload and Test

  1. First time with this board? Set it up with ESP32 Uno - Getting Started before you continue.
  2. Build the circuit by following the wiring table above.
  3. Plug in the USB Type-C cable between the ESP32 Uno and your computer.
  4. Select the board in Arduino IDE: pick ESP32 Dev Module and the COM port of the board (CH340).
  5. Paste the sketch into a new Arduino IDE window.
  6. Press Upload.
  7. Open the Serial Monitor and set it to 9600 baud.
  8. Move your hand toward the sensor, keep it there for two seconds, then pull it back.
  9. Pro tip: watch the small status LED on the module while you turn the pot. Stop turning when it lights up at the exact distance you want, for example 10cm from a sheet of paper.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
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 present The obstacle is present The obstacle is NOT present The obstacle is NOT present The obstacle is NOT present
Ln 11, Col 1
ESP32 Dev Module on COM15
2

ESP32 Uno code to detect an obstacle

This sketch stores the previous level and prints a line only when the level changes.

/* * This ESP32 Uno code was developed by newbiely.com * * This ESP32 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-uno/esp32-uno-obstacle-avoidance-sensor */ #define SENSOR_PIN 14 // D7 (GPIO14) reads the OUT pin of the IR obstacle avoidance sensor int prev_obstacle_state = HIGH; // level seen in the last loop pass int obstacle_state; // level seen in this loop pass void setup() { // start the Serial Monitor link at 9600 baud Serial.begin(9600); // make the ESP32 Uno sensor pin a digital input pinMode(SENSOR_PIN, INPUT); } void loop() { // get the current level of the sensor output obstacle_state = digitalRead(SENSOR_PIN); if (prev_obstacle_state == HIGH && obstacle_state == LOW) Serial.println("The obstacle is detected"); else if (prev_obstacle_state == LOW && obstacle_state == HIGH) Serial.println("The obstacle is cleared"); delay(50); // keep this level for the next comparison prev_obstacle_state = obstacle_state; }

Upload and Test

  1. Paste the sketch into the Arduino IDE.
  2. Press Upload and wait for "Done uploading".
  3. Open the Serial Monitor at 9600 baud.
  4. Wave a card past the module three times.
  5. Pro tip: add a counter variable that grows by one on each "detected" line, and you have a simple visitor or parts counter.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
The obstacle is detected The obstacle is cleared The obstacle is detected The obstacle is cleared The obstacle is detected The obstacle is cleared
Ln 11, Col 1
ESP32 Dev Module on COM15
2

Video Tutorial

See the ESP32 Uno build in action in the video below.

Code Explanation

This part shows what each piece of the two sketches is for.

Code Part What It Does
#define SENSOR_PIN 14 Names GPIO14, which is the D7 header pin
pinMode(SENSOR_PIN, INPUT) Sets GPIO14 to read a signal
digitalRead(SENSOR_PIN) Returns LOW when an object reflects the IR light
prev_obstacle_state Holds the level from the last loop pass
HIGH to LOW check Prints "detected" once when an object arrives
LOW to HIGH check Prints "cleared" once when the object leaves

The first sketch reports the state all the time, while the second one reports only events, so it never floods the Serial Monitor.

ESP32 Uno vs UNO R4 for This Project

This table lists what changes when you move this obstacle project from the UNO R4 to the ESP32 Uno.

Item Arduino UNO R4 ESP32 Uno
Logic voltage 5V 3.3V
Sensor power 5V or 3.3V 3.3V (keeps OUT safe)
OUT pin D8 (pin 8 in code) D7 (GPIO14, 14 in code)
Arduino IDE board Arduino UNO R4 WiFi ESP32 Dev Module
WiFi and Bluetooth WiFi only (R4 WiFi) WiFi plus Bluetooth Classic and BLE

The wiring is the same, but the OUT wire goes to D7 instead of D8. D8 is GPIO12, a boot strapping pin, and the sensor holds it HIGH, so D7 (GPIO14) is the safe choice. The code uses 14.

Go Wireless: Get an Alert on Your Phone

In this part, you get an idea to see obstacle events without a USB cable.

The ESP32 Uno has WiFi and Bluetooth built in. Instead of the Serial Monitor, you can send "detected" and "cleared" messages to a web page or a phone app. That way, a sensor on a door or a shelf can tell you when something passes, from anywhere in the room or house. You can also show the live HIGH/LOW level of D7 as a digital pin state.

Function References

Troubleshooting

Problem Possible Cause Solution
No COM port shows up CH340 driver missing or charge-only USB cable Install the CH340 driver and use a USB cable that carries data
Upload fails with "Failed to connect" or "Waiting for download" Board not entering download mode Hold the BOOT button while the upload starts, then release it
Module LED stays off and output never changes No power, or VCC and GND swapped Check that VCC goes to 3.3V and GND goes to GND
Prints "present" with nothing in front Range set too far, or the table surface reflects IR Turn the pot to shorten the range and tilt the sensor up a little
Black objects are missed Dark surfaces absorb infrared light Bring the object closer or raise the range with the pot
Random readings near a window Strong sunlight adds infrared noise Shade the sensor or use it away from direct sun
Garbled text in Serial Monitor Baud rate does not match the sketch Select 9600 baud in the Serial Monitor

FAQ

Does the IR obstacle sensor work at 3.3V with the ESP32 Uno?

Yes. The module runs fine at 3.3V, and this is the best choice for the ESP32 Uno. The OUT signal then stays at 3.3V, which the board pins can take safely. At 3.3V the maximum range can be a bit shorter than at 5V.

What does a LOW reading from the module mean?

A LOW level tells you that something blocks the sensor. The module is active LOW: its output rests at HIGH and drops to LOW when reflected light reaches the receiver.

How far can the obstacle sensor see?

About 2cm to 30cm, set with the onboard potentiometer. The real distance depends on the object color and surface. White paper is easy to see, and black cloth is hard.

Why is the pin number 14 in the code instead of 7?

On the ESP32 Uno, the label D7 on the header is connected to GPIO14 of the chip. The Arduino code always uses the GPIO number, so the sketch says 14 while the wire still goes to D7.

Can I use INPUT_PULLUP with this sensor on the ESP32 Uno?

Yes, on D7 (GPIO14) it works, since GPIO14 has an internal pull-up. Most modules already have their own pull-up on OUT, so plain INPUT is fine too. Do not move the sensor to A2 to A5 (GPIO35, 34, 36, 39) and expect INPUT_PULLUP to work, because those pins have no internal pull-up.

Can I use the analog pins A0 to A5 for this sensor?

Yes, the OUT pin is digital, so any input pin works with digitalRead(). Remember to write the GPIO number, for example A1 is 4 in code. A2 to A5 are input only, which is fine for a sensor.

Do I need a special driver for the ESP32 Uno?

The ESP32 Uno uses a CH340 USB-to-serial chip. Windows often installs it by itself. If no COM port appears, install the CH340 driver once and restart the Arduino IDE.

How can I connect two obstacle sensors?

Wire every OUT pin to a separate input, such as D6 (GPIO27) for the second module and D7 (GPIO14) for the first, then call digitalRead() on both. Both modules can share the 3.3V and GND pins.

What if my project needs more pins than the Uno headers give?

The ESP32 Uno has extra holes near the power header: GPIO32 and GPIO33 (I/O, PWM, analog on ADC1, so they also work while WiFi is on) and GPIO15 (I/O, PWM, analog on ADC2). Add pin headers with a soldering iron, then use the GPIO number in your sketch. For extra obstacle sensors, GPIO32 and GPIO33 are the best picks. Two warnings: GPIO15 and GPIO0 (the IO0 pin at the unused Uno NC position) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code, so use GPIO32/33 first. Never use the six holes under the digital header (SD0 to SD3, CMD, CLK = GPIO6 to GPIO11), because they are wired to the flash chip and using them crashes the board.

Can I see obstacle alerts on my phone with the ESP32 Uno?

Yes. The ESP32 Uno has WiFi and Bluetooth, so you can send each "detected" event to a web page or a Bluetooth app. See the Go Wireless section above for ready-made tutorials.

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!