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:

ESP32 C3 Super Mini - Ultrasonic Sensor - Servo Motor

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)
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 .

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.
The wiring diagram between ESP32 C3 Super Mini Ultrasonic Sensor Servo Motor

This image is created using Fritzing. Click to enlarge image

The diagram below shows the same connections without a breadboard:

The wiring diagram between ESP32 C3 Super Mini Ultrasonic Sensor Servo Motor  (No 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.
/* * This ESP32 C3 Super Mini code was developed by newbiely.com * * This ESP32 C3 Super Mini code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-c3/esp32-c3-super-mini-ultrasonic-sensor-servo-motor */ #include <ESP32Servo.h> #define TRIG_PIN 7 // The ESP32 C3 SuperMini pin connected to Ultrasonic Sensor's TRIG pin #define ECHO_PIN 6 // The ESP32 C3 SuperMini pin connected to Ultrasonic Sensor's ECHO pin #define SERVO_PIN 10 // The ESP32 C3 SuperMini pin connected to Servo Motor's pin #define DISTANCE_THRESHOLD 50 // centimeters Servo servo; // create servo object to control a servo // variables will change: float duration_us, distance_cm; void setup() { Serial.begin(115200); // initialize serial port pinMode(TRIG_PIN, OUTPUT); // set ESP32 pin to output mode pinMode(ECHO_PIN, INPUT); // set ESP32 pin to input mode servo.attach(SERVO_PIN); // attaches the ESP32 C3 SuperMini pin to the servo object servo.write(0); } void loop() { // generate 10-microsecond pulse to TRIG pin digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); // measure duration of pulse from ECHO pin duration_us = pulseIn(ECHO_PIN, HIGH); // calculate the distance distance_cm = 0.017 * duration_us; if (distance_cm < DISTANCE_THRESHOLD) servo.write(90); // rotate servo motor to 90 degree else servo.write(0); // rotate servo motor to 0 degree // print the value to Serial Monitor Serial.print("distance: "); Serial.print(distance_cm); Serial.println(" cm"); delay(500); }

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.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32C3 Dev Module
Library Manager
Type:
All
Topic:
All
ESP32Servo by Kevin Harrington,John K. Bennett
This library provides advanced PWM control for ESP32 boards using LEDC hardware, with enhanced MCPWM support on ESP32S3.
ESP32S3: 20 PWM channels (8 LEDC + 12 MCPWM) with intelligent allocation
All ESP32 variants: LEDC-based PWM with variable-frequency support
Supports variable-frequency PWM and fixed-frequency servo control
Automatic hardware fallback ensures maximum channel availability
More info
3.2.0
INSTALL
Newbiely.ino
···
1 void setup() {
Output
Serial Monitor
Ln 1, Col 1
ESP32C3 Dev Module on COM15
1
  • 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.
Arduino IDE Upload Code
  • 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:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32C3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32C3 Dev Module' on 'COM15')
New Line
9600 baud
distance: 84.32 cm distance: 61.75 cm distance: 38.24 cm distance: 21.59 cm distance: 56.10 cm
Ln 11, Col 1
ESP32C3 Dev Module on COM15
2
  • 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.

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