ESP32 S3 UNO - Ultrasonic Sensor - Servo Motor
This ESP32 S3 Uno ultrasonic sensor servo motor tutorial shows you how to move a servo by distance on the ESP32 S3 Uno form board. An HC-SR04 sensor measures how far an object is. When the object comes close, the servo turns to 90 degrees. When it moves away, the servo goes back to 0 degrees. It is a simple way to build an automatic gate or a touchless trash can lid.
In this tutorial, you will:
- Wire an HC-SR04 ultrasonic sensor and a servo motor to the ESP32 S3 Uno
- Protect the 3.3V input pin from the 5V ECHO signal
- Measure distance with pulseIn() in Arduino code
- Rotate a servo motor with the ESP32Servo library when an object is near

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Buy Note: For controlling multiple servo motors, use the PCA9685 16 Channel PWM Servo Driver Module to save MCU pins and simplify wiring.
Overview of Servo Motor and Ultrasonic Sensor
This project links one sensor to one motor. The ultrasonic sensor tells the board how far away an object is, and the servo motor reacts by turning its arm. If one of these parts is new to you, learn it alone first. The tutorials below explain the pinout, how each part works, and how to program it.
How the project works
The HC-SR04 sends a short burst of sound that people cannot hear. The sound hits an object and bounces back. The sensor keeps its ECHO pin HIGH for as long as the sound takes to travel there and back. The ESP32 S3 Uno measures this time and turns it into a distance in centimeters.
The code then compares the distance with a threshold of 50 cm. If the object is closer than 50 cm, the servo turns to 90 degrees. If it is farther, the servo returns to 0 degrees.
ESP32 S3 Uno Pinout
The image below shows the pinout diagram of the ESP32 S3 Uno form board. Use it to match the Uno header pins (D6, D7, D9, 5V) to their GPIO numbers when you build the circuit.

Wiring Diagram
The sensor's TRIG pin goes to D7 and its ECHO pin goes to D6. The servo signal wire goes to D9. Both the sensor and the servo take power from the 5V pin.

This image is created using Fritzing. Click to enlarge image
| Component Pin | ESP32 S3 Uno Pin |
|---|---|
| Ultrasonic Sensor VCC | 5V |
| Ultrasonic Sensor TRIG | D7 (GPIO14) |
| Ultrasonic Sensor ECHO (through level converter) | D6 (GPIO3) |
| Ultrasonic Sensor GND | GND |
| Servo VCC (red) | 5V |
| Servo GND (brown or black) | GND |
| Servo Signal (orange or yellow) | D9 (GPIO46) |
WARNING
The HC-SR04 runs on 5V, so its ECHO pin also gives a 5V signal. The ESP32 S3 Uno pins are NOT 5V tolerant. Put a 5V to 3.3V level converter on the ECHO wire, or use a voltage divider (for example 1 kΩ from ECHO to D6, and 2 kΩ from D6 to GND). You can also use a 3.3V version of the sensor. The TRIG wire needs no change, because a 3.3V pulse is enough to trigger the sensor.
WARNING
D6 (GPIO3) and D9 (GPIO46) are boot strapping pins on the ESP32 S3 Uno. The board reads their level at power-up to pick its boot mode. The ECHO signal and the servo signal are usually fine here. But if the board does not boot or cannot upload code, unplug the ECHO wire from D6 and the servo signal wire from D9, upload the code, and then plug them back in.
※ NOTE THAT:
The servo runs from 5V, but the ESP32 S3 Uno sends it only a 3.3V control signal. This is fine, because the signal goes OUT of the board. Most hobby servos read a 3.3V signal without problems.
ESP32 S3 Uno Code - Ultrasonic Sensor Controls Servo Motor
The sketch sends a 10 microsecond pulse to the TRIG pin, then measures how long the ECHO pin stays HIGH. It turns that time into centimeters and moves the servo to 90 or 0 degrees based on the 50 cm threshold. It also prints the distance to the Serial Monitor every half second.
The ESP32 S3 Uno cannot use the standard Servo.h library. The code uses ESP32Servo.h instead. It has the same attach() and write() functions, so the rest of the logic does not change.
Detailed Instructions
- New to the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
- Wire it up as shown in the diagram, with the level converter on the ECHO wire.
- Connect the board to your computer with a USB Type-C cable.
- Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
- Install the ESP32Servo library. Open the Library Manager, search for ESP32Servo, and install the one by Kevin Harrington.
- Copy the code above and paste it into the Arduino IDE.
- Upload the code by clicking the Upload button.
- Wave your hand in front of the sensor, closer than 50 cm and then farther away.
- Watch the servo. It turns to 90 degrees when your hand is near and goes back to 0 degrees when your hand is gone.
- Tip: Open the Serial Monitor at 9600 baud to see the measured distance and check the threshold.
※ NOTE THAT:
This code is made for learning. Ultrasonic readings can be noisy, and one bad reading can make the servo jump. To get stable readings, filter the noise as shown in how to filter noise for ultrasonic sensor.
Video Tutorial
Watch the video below to see this ESP32 S3 Uno project step by step.
FAQ
Why does the code use 14, 3 and 46 instead of 7, 6 and 9?
The board has Uno header labels, but the code needs GPIO numbers. Header D7 is GPIO14, D6 is GPIO3, and D9 is GPIO46. So TRIG_PIN is 14, ECHO_PIN is 3, and SERVO_PIN is 46.
Can I connect the HC-SR04 ECHO pin straight to the ESP32 S3 Uno?
It is not safe. The ESP32 S3 Uno pins work at 3.3V and are not 5V tolerant, but a 5V HC-SR04 gives 5V on ECHO. Use a level converter or a 1 kΩ / 2 kΩ voltage divider on the ECHO wire. A 3.3V sensor such as the HC-SR04P also solves this.
Why does the code use ESP32Servo.h instead of Servo.h?
The standard Servo library does not support the ESP32 S3 Uno. The ESP32Servo library by Kevin Harrington uses the PWM hardware of the board but keeps the same functions. Your UNO R4 code works after you change only the include line and the pin numbers.
Can the ESP32 S3 Uno power the servo and the sensor at the same time?
A small servo like the SG90 and an HC-SR04 can both run from the 5V pin when the board is powered by USB. A bigger servo draws more current and can make the board reset or give wrong distance readings. In that case, power the servo from an external 5V supply and connect its GND to the board GND.
How do I change the distance or the angle?
Change DISTANCE_THRESHOLD to set when the servo moves, for example 20 for 20 cm. Change the values in servo.write(90) and servo.write(0) to set the two angles, from 0 to 180 degrees.
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 to use them and write the GPIO number in code. For a second ultrasonic sensor, use GPIO35 to GPIO42 for TRIG and ECHO, not GPIO47/GPIO48, because ECHO is an input. For extra servos, GPIO15, GPIO16 or GPIO35 to GPIO42 work well. Be careful with two things. First, GPIO47/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. Second, 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.
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| No COM port shows up in Arduino IDE | USB driver missing or board not in upload mode | Install the CP210x or CH340 driver. Or hold BOOT, press RESET, then release BOOT and upload again |
| Upload fails or board does not boot | The wires on D6 (GPIO3) or D9 (GPIO46) affect the boot strapping pins | Unplug the ECHO wire from D6 and the servo signal from D9 during upload, then plug them back in |
| Compile error "Servo.h - No such file" | The code uses the AVR Servo library | Install the ESP32Servo library and use #include |
| Distance is always 0 cm | No ECHO signal, wrong wiring or sensor not powered | Check that VCC goes to 5V, TRIG to D7 and ECHO to D6 through the level converter |
| Distance jumps to strange values | Noise or soft objects that absorb sound | Point the sensor at a flat, hard object and add the noise filter code |
| Servo does not move at all | Wrong wiring or no power to the servo | Check that red goes to 5V, brown to GND and the signal wire to D9 |
| Servo jitters or the board resets | The servo draws too much current from USB | Power the servo from an external 5V supply and join the GNDs |
| Serial Monitor shows strange characters | Wrong baud rate | Set the Serial Monitor to 9600 baud |