ESP32 C3 Super Mini - Ultrasonic Sensor - Servo Motor
Move your hand close and a servo motor turns by itself. In this beginner tutorial, the ESP32 C3 Super Mini reads an ultrasonic sensor and moves a servo motor based on the distance.
In this tutorial, you'll learn:
- How to connect an ultrasonic sensor and a servo motor to the ESP32 C3 Super Mini
- How to program the ESP32 C3 Super Mini to measure distance
- How to rotate the servo motor to 90° when an object is close to the ultrasonic sensor
- How to rotate the servo motor back to 0° when the object is far from the ultrasonic sensor
- How to check the distance in the Serial Monitor

Hardware Preparation
| 1 | × | ESP32 C3 Super Mini | |
| 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 | × | Ultrasonic Sensor | |
| 1 | × | Servo Motor | |
| 1 | × | Breadboard | |
| 1 | × | Jumper Wires | |
| 1 | × | Optionally, DC Power Jack |
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
An ultrasonic sensor is a distance sensor that uses sound waves, and a servo motor is an actuator that turns to an exact angle.
Key Features:
- Ultrasonic sensor: measures the distance to an object without touching it.
- Servo motor: moves to the angle set by your code.
- Together: the distance decides the servo motor position.
Technical Specifications:
- Ultrasonic sensor voltage: 5V
- Ultrasonic sensor range: 2 cm to 400 cm
- Servo motor voltage: 5V
- Servo motor angle: 0° to 180°
New to these parts? These tutorials explain the pinout, how they work, and how to connect and program them:
Pinout
The ultrasonic sensor has four pins, and the servo motor has three.
Ultrasonic sensor:
- VCC: connect to 5V.
- GND: connect to GND.
- TRIG: receives the trigger pulse from the board.
- ECHO: sends a pulse back to the board. Its length depends on the distance.
Servo motor:
- VCC pin: (red) connect to 5V.
- GND pin: (brown or black) connect to GND.
- Signal pin: (yellow or orange) receives the control signal from the board.
Wiring Diagram
Connect both parts to the ESP32 C3 Super Mini as shown below.
- Note: The 5V pin is fine for one small servo motor. Use an external power supply for a bigger servo motor.

This image is created using Fritzing. Click to enlarge image
The diagram below shows the same connections without a breadboard:

This image is created using Fritzing. Click to enlarge image
| Ultrasonic Sensor Pin | ESP32 C3 Super Mini Pin |
|---|---|
| VCC | 5V |
| GND | GND |
| TRIG | GPIO7 |
| ECHO | GPIO6 |
| Servo Motor Pin | ESP32 C3 Super Mini Pin |
|---|---|
| VCC (Red) | 5V |
| GND (Brown/Black) | GND |
| Signal (Yellow/Orange) | GPIO10 |
ESP32 C3 Super Mini Code - Ultrasonic Sensor Controls Servo Motor
This code moves the servo motor on GPIO10 according to the distance from the ultrasonic sensor on GPIO7 and GPIO6.
What This Code Does:
- Starts Serial: opens the Serial Monitor at 115200 baud.
- Sends a pulse: a 10-microsecond pulse on GPIO7 starts each measurement.
- Measures the distance: the pulse length on GPIO6 is converted to centimeters.
- Moves the servo motor: below 50 cm it goes to 90°, otherwise it goes to 0°.
- Prints the distance: shows the result every 500 ms.
Detailed Instructions
- New to ESP32 C3 Super Mini? Complete our Getting Started with ESP32 C3 Super Mini tutorial first to set up your development environment.
- Wire it up: Connect the parts as shown in the wiring diagram above.
- Connect the board: Plug the ESP32 C3 Super Mini into your computer with a USB Type-C cable.
- Open the IDE: Open Arduino IDE on your computer.
- Select the board: Make sure ESP32 C3 Super Mini is selected in Tools > Board.
- Select the port: Pick the COM port of your board in Tools > Port.
- Install the library: Click the Libraries icon on the left bar, search for "ESP32Servo", and install the library by Kevin Harrington and John K. Bennett.
- Search for ESP32Servo created by Kevin Harrington,John K. Bennett and click the Install button.
- Copy the code: Copy the code above and paste it into Arduino IDE.
- Upload: Click the Upload button to send the code to the ESP32 C3 Super Mini.

- Open Serial Monitor: Set the baud rate to 115200.
- Move your hand: Move your hand in front of the ultrasonic sensor.
- Watch the servo motor: See the change of the servo motor. It rotates to 90° when your hand is closer than 50 cm.
- Check the result: The Serial Monitor shows the distance. It looks like the below:
- Pro Tip: If the servo motor shakes or the board restarts, the servo motor may need more current. Try an external power supply and connect its GND to the board's GND.
Code Explanation
Check out the line-by-line explanation in the comments of the source code!
Project Ideas
You can use this ultrasonic sensor and servo motor pair in many small builds.
- Automatic Trash Can: Open the lid when a hand comes close.
- Mini Gate: Open a model gate when a car comes near.
- Touchless Door: Open a small door or box without touching it.
- Hand Sanitizer Dispenser: Press a pump when a hand is under the sensor.
- Pet Feeder: Open a food flap when your pet walks up.
Challenge Yourself
Try these small upgrades to your ESP32 C3 Super Mini ultrasonic sensor servo motor project.
- Easy: Change DISTANCE_THRESHOLD so the servo motor reacts at 20 cm.
- Easy: Change the 90° angle to 180°.
- Medium: Add an LED on GPIO3 that turns on when an object is close.
- Medium: Keep the servo motor at 90° for 3 seconds after the object leaves.
- Advanced: Use map() to set the servo motor angle from the distance, so it moves smoothly.