ESP32 C3 Super Mini - Ultrasonic Sensor

This tutorial shows you how to use the ESP32 C3 Super Mini with the HC-SR04 ultrasonic sensor to measure distance accurately. Perfect for beginners looking to add distance sensing capabilities to their Arduino projects!

In this tutorial, you'll learn:

ESP32 C3 Super Mini - Ultrasonic Sensor

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

Overview of Ultrasonic Sensor

The HC-SR04 is a distance sensor that measures the distance to objects using ultrasonic sound waves.

Key specifications and features:

  • Operating Voltage: 5V DC
  • Measuring Range: 2cm to 400cm (0.8 inches to 157 inches)
  • Accuracy: Up to 3mm
  • Measuring Angle: 15 degrees
  • Trigger Input: 10µS TTL pulse
  • Non-contact measurement: Safe for detecting objects without physical touch
  • Perfect for beginners: Easy to connect and program with simple Arduino code
  • Affordable and reliable: Great for learning distance sensing basics

Ultrasonic Sensor Pinout

The HC-SR04 ultrasonic sensor has four pins for connection:

  • VCC: Connect to 5V power supply
  • GND: Connect to ground (0V)
  • TRIG: Receives trigger pulse from ESP32 C3 Super Mini to start measurement
  • ECHO: Sends pulse back to ESP32 with duration proportional to measured distance
Ultrasonic Sensor Pinout
image source: diyables.io

How Ultrasonic Sensor Works

See How Ultrasonic Sensor Work

Wiring Diagram between Ultrasonic Sensor and ESP32 C3 Super Mini

Here's how to connect the HC-SR04 ultrasonic sensor to your ESP32 C3 Super Mini board:

  • Note: The HC-SR04 requires 5V power, which can be supplied via USB or an external power source through the Vin pin.
The wiring diagram between ESP32 C3 Super Mini ultrasonic sensor

This image is created using Fritzing. Click to enlarge image

Ultrasonic Sensor Pin ESP32 C3 Super Mini Pin
VCC 5V
GND GND
TRIG D1
ECHO D10

How To Program Ultrasonic Sensor

Programming the HC-SR04 ultrasonic sensor with ESP32 C3 Super Mini involves three simple steps:

Step 1: Send trigger pulse

  • Generate a 10-microsecond pulse on the TRIG pin using digitalWrite() and delayMicroseconds() functions
  • Example with pin D1:
digitalWrite(D1, HIGH); delayMicroseconds(10); digitalWrite(D1, LOW);

Step 2: Measure echo pulse duration

  • Read the pulse duration from the ECHO pin using pulseIn() function
  • The pulse duration represents the time for sound to travel to object and back
  • Example with pin D10:
duration_us = pulseIn(D10, HIGH);

Step 3: Calculate distance

  • Convert pulse duration to distance in centimeters
  • Formula: distance = 0.017 × duration (sound travels at ~340 m/s, divided by 2 for round trip)
distance_cm = 0.017 * duration_us;

ESP32 C3 Super Mini Code

The following code demonstrates how to read distance measurements from the HC-SR04 ultrasonic sensor and display them on the Serial Monitor:

What this code does:

  • Initializes the TRIG and ECHO pins on the ESP32 C3 Super Mini
  • Sends ultrasonic pulses every 500 milliseconds
  • Measures the time for echo to return
  • Calculates distance in centimeters
  • Displays distance readings on Serial Monitor
/* * 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 */ #define TRIG_PIN D1 // The ESP32 C3 SuperMini pin connected to Ultrasonic Sensor's TRIG pin #define ECHO_PIN D10 // The ESP32 C3 SuperMini pin connected to Ultrasonic Sensor's ECHO pin float duration_us, distance_cm; void setup() { // begin serial port Serial.begin (9600); // Configure the trigger pin to output mode pinMode(TRIG_PIN, OUTPUT); // Configure the echo pin to input mode pinMode(ECHO_PIN, INPUT); } void loop() { // Produce a 10-microsecond pulse to the TRIG pin. digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); // Measure the pulse duration from the ECHO pin duration_us = pulseIn(ECHO_PIN, HIGH); // calculate the distance distance_cm = 0.017 * duration_us; // print the value to Serial Monitor Serial.print("distance: "); Serial.print(distance_cm); Serial.println(" cm"); delay(500); }

Detailed Instructions

  • New to ESP32 C3 Mini? Complete our Getting Started with ESP32 C3 Mini tutorial first to set up your development environment.
  • Install Arduino IDE: If this is the first time you use ESP32 C3 Super Mini, see how to setup environment for ESP32 C3 Super Mini on Arduino IDE.
  • Copy the code: Copy the above code and paste it into Arduino IDE.
  • Connect your board: Connect the ESP32 C3 Super Mini to your computer via USB cable.
  • Select board and port: Choose the correct board and COM port in Arduino IDE.
  • Upload the code: Click the Upload button to compile and upload code to your ESP32 C3 Super Mini.
How to upload ESP32 C3 Super Mini code on Arduino IDE
  • Open Serial Monitor: Click the Serial Monitor icon to view distance readings.
How to open serial monitor on Arduino IDE
  • Set baud rate: Make sure Serial Monitor is set to 9600 baud.
  • Test the sensor: Move your hand in front of the ultrasonic sensor at different distances.
  • View results: Watch the distance measurements update on Serial Monitor in real-time.
  • Pro Tip: Keep objects at least 2cm away from the sensor for accurate readings, and avoid soft surfaces like cloth that absorb sound waves.

Line-by-line Code Explanation

The above ESP32 C3 Super Mini code contains line-by-line explanation. Please read the comments in the code!

Serial Monitor Output

When you move your hand in front of the HC-SR04 ultrasonic sensor, you'll see distance readings like this:

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: 19.4 cm distance: 17.6 cm distance: 16.9 cm distance: 14.2 cm distance: 12.8 cm distance: 15.3 cm distance: 18.7 cm distance: 23.5 cm distance: 27.4 cm distance: 26.9 cm distance: 24.3 cm distance: 25.6 cm distance: 23.1 cm distance: 20.8 cm
Ln 11, Col 1
ESP32C3 Dev Module on COM15
2

How to Filter Noise from Distance Measurements of Ultrasonic Sensor

Ultrasonic sensors can sometimes produce noisy or inconsistent readings due to environmental factors.

See How to Filter Noise from Distance Measurements of Ultrasonic Sensor

Why filtering matters:

  • Environmental interference (temperature, humidity)
  • Object surface texture and angle
  • Electrical noise
  • Multiple reflections from nearby objects

Application and Project Ideas

Here are practical ways to use the ESP32 C3 Super Mini with HC-SR04 ultrasonic sensor:

  • Parking assistant: Build a distance alert system for cars or garage parking
  • Automatic trash can: Create a touchless lid opener that detects hand proximity
  • Water level monitor: Measure water level in tanks without contact
  • Obstacle avoidance robot: Enable robots to detect and avoid objects
  • Security alarm: Trigger alerts when objects enter a specific zone
  • Height measurement tool: Build a digital height measuring device
  • Smart doorbell: Detect when someone approaches your door

Video Tutorial

Watch the video below for a visual walkthrough of this project.

Challenge Yourself

Ready to take your ultrasonic sensor skills to the next level? Try these challenges:

  • Easy: Modify the code to display distance in inches instead of centimeters
  • Easy: Add an LED that lights up when an object is within 10cm
  • Medium: Create a distance-based warning system with different beep patterns using a buzzer
  • Medium: Display distance on an LCD screen instead of Serial Monitor
  • Advanced: Build a multi-sensor array to detect object position in 2D space
  • Advanced: Implement a median filter to smooth out noisy readings and improve accuracy

Learn More

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