ESP32 S3 UNO - Obstacle Avoidance Sensor

This ESP32 S3 Uno obstacle avoidance sensor tutorial shows you how to connect an infrared obstacle sensor to the ESP32 S3 Uno form board and read it with Arduino code. The IR obstacle detector tells you when something is in front of it, and the result appears on the Serial Monitor.

In this tutorial, you will:

  1. Learn how an IR obstacle avoidance sensor works
  2. Wire the infrared obstacle sensor to the ESP32 S3 Uno
  3. Read the sensor output with digitalRead()
  4. Detect the moment an obstacle appears and the moment it goes away
ESP32 S3 Uno obstacle avoidance sensor

Hardware Preparation

1×ESP32 S3 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 .

Overview of IR Obstacle Avoidance Sensor

An IR obstacle avoidance sensor is a small, cheap module that tells you if an object is close to it. It is a common part in robot cars, counters and touchless switches.

The module uses infrared light to see objects. It can sense things from about 2cm to 30cm away. A small potentiometer on the board lets you set how far it looks.

Pinout

The sensor has only three pins, so the wiring is very simple.

GND pin

Connect it to GND (0V).

VCC pin

Connect it to the power supply. The module works with 3.3V or 5V. With the ESP32 S3 Uno, use 3.3V.

OUT pin

This is the digital output. It is LOW when an obstacle is in front of the sensor, and HIGH when there is none. Connect it to a digital input pin of the ESP32 S3 Uno.

IR Obstacle Avoidance Sensor Pinout
image source: diyables.io

How It Works

Knowing how the sensor decides "obstacle or no obstacle" helps you read its output the right way in code.

The module has two parts: an IR transmitter and an IR receiver. The transmitter sends out infrared light. When an object is in front of the sensor, the light bounces back and the receiver picks it up. The module then shows the result on the OUT pin:

  1. Obstacle in front of the sensor: OUT is LOW.
  2. No obstacle in front of the sensor: OUT is HIGH.

Note that the logic is "active LOW". A LOW reading means something was found.

※ NOTE THAT:

The sensor can get bent during shipping, and then it may not work well. If it acts strangely, gently bend the IR transmitter and receiver so they point straight ahead and stand parallel to each other.

ESP32 S3 Uno Pinout

The image below shows the pinout of the ESP32 S3 Uno form board. Use it to find the Uno header pins (D8, GND, 3.3V…) and their GPIO numbers while you wire the sensor.

ESP32 S3 Uno pinout diagram

Wiring Diagram

The sensor needs just three wires: power, ground and the output signal to pin D8.

The wiring diagram between ESP32 S3 Uno IR Obstacle Avoidance Sensor

This image is created using Fritzing. Click to enlarge image

Obstacle Sensor Pin ESP32 S3 Uno Pin
VCC 3.3V
GND GND
OUT D8 (GPIO21)

WARNING

ESP32 S3 Uno pins work with 3.3V logic and are NOT 5V tolerant. If you power the sensor from 5V, its OUT pin can send 5V into GPIO21 and damage it. Power the sensor from 3.3V, or put a voltage divider on the OUT line if you must use 5V.

How To Program For IR Obstacle Avoidance Sensor

Reading the sensor takes only two steps, because the OUT pin is a plain digital signal.

  1. Set the ESP32 S3 Uno pin as a digital input with pinMode(). Here we use D8, which is GPIO21 in code.
pinMode(21, INPUT_PULLUP);
  1. Read the state of that pin with digitalRead().
int state = digitalRead(21);

ESP32 S3 Uno Code

You will usually use the obstacle sensor in one of two ways:

  1. Act based on the current state: is an obstacle there right now or not?
  2. Act on a change: the moment an obstacle shows up, or the moment it goes away.

The two sketches below cover both cases.

ESP32 S3 Uno code for checking if the obstacle is present

This sketch reads the OUT pin every 100ms and prints whether an obstacle is there. Because the sensor is active LOW, a LOW reading prints "The obstacle is present".

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-obstacle-avoidance-sensor */ #define SENSOR_PIN 21 // The ESP32 S3 Uno pin GPIO21 (D8) connected to OUT pin of IR obstacle avoidance sensor void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the ESP32 S3 Uno pin as an input pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of 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

  1. New to the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
  2. Wire it up as shown in the diagram.
  3. Connect the board to your computer with a USB Type-C cable.
  4. Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
  5. Copy the code above and paste it into the Arduino IDE.
  6. Upload the code by clicking the Upload button.
  7. Test it by holding an object in front of the sensor for a few seconds, then moving it away.
  8. Check the output on the Serial Monitor (baud rate 9600).
  9. Tip: If the sensor never sees your object, turn the potentiometer on the module slowly until its LED turns on at the distance you want.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 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 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
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

ESP32 S3 Uno code for detecting obstacle

This sketch remembers the last reading and compares it with the new one. It prints a message only when the state changes: HIGH to LOW means an obstacle was detected, and LOW to HIGH means it was cleared.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-obstacle-avoidance-sensor */ #define SENSOR_PIN 21 // The ESP32 S3 Uno pin GPIO21 (D8) connected to OUT pin of IR obstacle avoidance sensor int prev_obstacle_state = HIGH; // the previous state from the input pin int obstacle_state; // the current reading from the input pin void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the ESP32 S3 Uno pin as an input pinMode(SENSOR_PIN, INPUT); } void loop() { // read the state of the input pin: 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); // save the last state prev_obstacle_state = obstacle_state; }

Detailed Instructions

  1. Copy the code above and open it in the Arduino IDE.
  2. Upload the code by clicking the Upload button.
  3. Test it by putting an object in front of the sensor for a moment, then taking it away.
  4. Check the output on the Serial Monitor.
  5. Tip: Each event is printed only once, so this sketch is a good base for counters and alarms.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
The obstacle is detected The obstacle is cleared
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Video Tutorial

Watch the video below to see this ESP32 S3 Uno project step by step.

Function References

FAQ

Can I power the IR obstacle sensor from 5V on the ESP32 S3 Uno?

The module itself works at 5V, but its OUT pin then gives a 5V signal. ESP32 S3 Uno pins are 3.3V only and are not 5V tolerant. Power the sensor from 3.3V, or use a voltage divider on the OUT line.

Why does the sensor give LOW when an obstacle is present?

The module is designed as "active LOW". Its output stays HIGH when nothing is in front of it and goes LOW when the IR light bounces back. Your code just needs to check for LOW to know an object is there.

Why do I write 21 in the code and not 8?

On the ESP32 S3 Uno, the Uno header label D8 is connected to GPIO21 of the chip. In code you always use the GPIO number. The wiring stays on the D8 header, like on a classic Uno board.

How do I change the detection distance?

Turn the small potentiometer on the module. Turning one way makes the range longer, the other way makes it shorter. The range is about 2cm to 30cm, and dark or shiny objects may be harder to detect.

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

The ESP32 S3 Uno has two extra rows of holes with more GPIOs: GPIO15 and GPIO16 (I/O, PWM, analog), GPIO45 and GPIO35 to GPIO42 (I/O, PWM), and GPIO47/GPIO48 (output only, PWM). Solder pin headers and write the GPIO number in code. For more obstacle sensors, GPIO35 to GPIO42, GPIO15 or GPIO16 are good choices, but not GPIO47/GPIO48 because they cannot read inputs. Be careful with two things. GPIO47 and GPIO48 may run at 1.8V instead of 3.3V on some modules (especially ones with "V" in the name, like R8V or R16V), so 3.3V devices can be damaged or act strangely. GPIO0, GPIO3 (D6), GPIO45 and GPIO46 (D9) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code. Use the other extra pins first.

Can I use several obstacle sensors at the same time?

Yes. Connect each sensor's OUT pin to its own digital pin, for example D7 (GPIO14) and D8 (GPIO21), and read each one with digitalRead(). All sensors can share the same 3.3V and GND.

Troubleshooting

Problem Possible Cause Solution
No COM port in Arduino IDE USB driver missing or board not in upload mode Install the CP210x or CH340 driver, use a data USB cable, or hold BOOT while pressing RESET
Serial Monitor shows nothing or strange characters Wrong baud rate Set the Serial Monitor to 9600 baud
Always prints "NOT present" Range too short, or sensor not powered Check VCC and GND wires, then turn the potentiometer to increase the range
Always prints "present" Range too long, or a nearby surface reflects IR Turn the potentiometer to reduce the range and point the sensor away from the table
Readings are wrong or random OUT wire on the wrong pin, or code uses 8 instead of 21 Wire OUT to D8 and use GPIO 21 in the code
Sensor works badly outdoors Sunlight contains a lot of infrared light Use the sensor indoors or shade it from direct sunlight
Detection is weak or one-sided IR LED and receiver bent during shipping Gently bend them so they point straight ahead and are parallel
Board resets or GPIO gets hot Sensor powered from 5V sends 5V into GPIO21 Power the sensor from 3.3V or add a voltage divider

※ 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!