ESP32 C3 Super Mini - Ultrasonic Sensor - Piezo Buzzer

Build a simple distance alarm with the ESP32 C3 Super Mini, an ultrasonic sensor and a piezo buzzer. The buzzer sounds when an object comes close, and it stops when the object moves away.

In this tutorial, you'll learn:

ESP32 C3 Super Mini - Ultrasonic Sensor - Piezo Buzzer

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×3-24V Active Piezo Buzzer
1×Active Piezo Buzzer Module
1×Passive Piezo Buzzer Module
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 Piezo Buzzer and Ultrasonic Sensor

The ultrasonic sensor is a distance sensor that measures how far away an object is, and the piezo buzzer is a small part that makes sound.

Key Features:

  • No touch needed: The ultrasonic sensor detects an object without touching it
  • Simple sound output: The piezo buzzer needs only one GPIO pin
  • Easy code: A few lines of code turn a distance reading into sound

Technical Specifications:

  • Ultrasonic sensor voltage: 5V DC
  • Measuring range: 2 cm to 400 cm
  • Measuring angle: 15 degrees
  • Buzzer voltage: This tutorial uses a 3-5V buzzer, and you can adapt it for a 12V buzzer

Learn more in our detailed tutorials (pinout, how it works, wiring and code):

Pinout

Each part connects to the board with only a few pins.

  • VCC (ultrasonic sensor): Connect to 5V power
  • GND (ultrasonic sensor): Connect to ground (0V)
  • TRIG (ultrasonic sensor): Receives the trigger pulse from the ESP32 C3 Super Mini
  • ECHO (ultrasonic sensor): Sends a pulse back, and its length depends on the distance
  • Positive (+) (piezo buzzer): Receives the control signal from the ESP32 C3 Super Mini
  • Negative (-) (piezo buzzer): Connect to ground (0V)

Wiring Diagram

Connect the ultrasonic sensor and the piezo buzzer as shown below.

  • Note: The ultrasonic sensor needs 5V power, so connect its VCC to the 5V pin.
  • Note: Check the polarity of the bare buzzer. The (+) pin has a longer leg or a + mark.

The wiring diagram with power supply from USB cable:

The wiring diagram between ESP32 C3 Super Mini Ultrasonic Sensor Piezo Buzzer

This image is created using Fritzing. Click to enlarge image

The wiring diagram with power supply from 5V adapter:

The wiring diagram between ESP32 C3 Super Mini Ultrasonic Sensor Piezo Buzzer  5V power adapter

This image is created using Fritzing. Click to enlarge image

Ultrasonic Sensor Pin ESP32 C3 Super Mini Pin
VCC 5V
GND GND
TRIG GPIO1
ECHO GPIO10
Piezo Buzzer Pin ESP32 C3 Super Mini Pin
Positive (+) GPIO7
Negative (-) GND

ESP32 C3 Super Mini Code - Simple Sound

This ESP32 C3 Super Mini code turns the piezo buzzer on while an object is close to the ultrasonic sensor.

What This Code Does:

  • Sends a 10-microsecond pulse to the TRIG pin on GPIO1
  • Measures the echo pulse on the ECHO pin on GPIO10
  • Calculates the distance in centimeters
  • Turns the buzzer on GPIO7 on when the distance is below 50 cm, and off when it is not
  • Prints the distance to the Serial Monitor every 500 milliseconds
/* * 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-piezo-buzzer */ #define TRIG_PIN 1 // The ESP32 C3 SuperMini pin connected to Ultrasonic Sensor's TRIG pin #define ECHO_PIN 10 // The ESP32 C3 SuperMini pin connected to Ultrasonic Sensor's ECHO pin #define BUZZER_PIN 7 // The ESP32 C3 SuperMini pin connected to Piezo Buzzer's pin #define DISTANCE_THRESHOLD 50 // centimeters // variables will change: float duration_us, distance_cm; void setup() { Serial.begin (115200); // initialize serial port pinMode(TRIG_PIN, OUTPUT); // set ESP32 C3 SuperMini pin to output mode pinMode(ECHO_PIN, INPUT); // set ESP32 C3 SuperMini pin to input mode pinMode(BUZZER_PIN, OUTPUT); // set ESP32 C3 SuperMini pin to output mode } 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) digitalWrite(BUZZER_PIN, HIGH); // turn on Piezo Buzzer else digitalWrite(BUZZER_PIN, LOW); // turn off Piezo Buzzer // print the value to Serial Monitor Serial.print("distance: "); Serial.print(distance_cm); Serial.println(" cm"); delay(500); }

Detailed Instructions

Follow these steps to run the simple sound example.

  • New to ESP32 C3 Super Mini? Complete our Getting Started with ESP32 C3 Super Mini tutorial first to set up your development environment.
  • Wire the parts: Follow the wiring diagram above.
  • Connect the board: Plug the ESP32 C3 Super Mini into your computer with a USB Cable Type-C.
  • Open the IDE: Launch the 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.
  • Copy the code: Copy the above code and paste it into the Arduino IDE.
  • Upload the code: Click the Upload button in the Arduino IDE.
  • Open the Serial Monitor: Set the baud rate to 115200 to see the distance.
  • Test it: Move your hand in front of the sensor.
  • Listen: The piezo buzzer sounds when your hand is closer than 50 cm.
  • Pro Tip: Hear nothing? Swap the two buzzer legs. A bare buzzer only works one way round.

Serial Monitor Output

The distance changes as you move your hand toward the sensor and away from it.

∞
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: 62.4 cm distance: 48.7 cm distance: 31.2 cm distance: 54.9 cm
Ln 11, Col 1
ESP32C3 Dev Module on COM15
2

Line-by-line Code Explanation

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

ESP32 C3 Super Mini Code - Melody

This ESP32 C3 Super Mini code plays a melody when an object is close to the ultrasonic sensor.

What This Code Does:

  • Measures the distance in the same way as the first example
  • Reads note names such as NOTE_E5 from a pitches.h file
  • Plays the whole melody on GPIO7 when the distance is below 50 cm
  • Stays quiet when the object is far away
/* * 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-piezo-buzzer */ #include "pitches.h" #define TRIG_PIN 1 // The ESP32 C3 SuperMini pin connected to Ultrasonic Sensor's TRIG pin #define ECHO_PIN 10 // The ESP32 C3 SuperMini pin connected to Ultrasonic Sensor's ECHO pin #define BUZZER_PIN 7 // The ESP32 C3 SuperMini pin connected to Piezo Buzzer's pin #define DISTANCE_THRESHOLD 50 // centimeters // variables will change: float duration_us, distance_cm; // notes in the melody: int melody[] = { NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_G5, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_D5, NOTE_D5, NOTE_E5, NOTE_D5, NOTE_G5 }; // note durations: 4 = quarter note, 8 = eighth note, etc, also called tempo: int noteDurations[] = { 8, 8, 4, 8, 8, 4, 8, 8, 8, 8, 2, 8, 8, 8, 8, 8, 8, 8, 16, 16, 8, 8, 8, 8, 4, 4 }; void setup() { pinMode(TRIG_PIN, OUTPUT); // set ESP32 C3 SuperMini pin to output mode pinMode(ECHO_PIN, INPUT); // set ESP32 C3 SuperMini pin to input mode } 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) buzzer(); // play a song delay(500); } void buzzer() { // iterate over the notes of the melody: int size = sizeof(noteDurations) / sizeof(int); for (int thisNote = 0; thisNote < size; thisNote++) { // to calculate the note duration, take one second divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. int noteDuration = 1000 / noteDurations[thisNote]; tone(BUZZER_PIN, melody[thisNote], noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); // stop the tone playing: noTone(BUZZER_PIN); } }

Detailed Instructions

Follow these steps to run the melody example.

  • New to ESP32 C3 Super Mini? Complete our Getting Started with ESP32 C3 Super Mini tutorial first to set up your development environment.
  • Select the board: Make sure ESP32 C3 Super Mini is selected in Tools > Board.
  • Copy the code: Copy the above code and paste it into the Arduino IDE.
  • Create the pitches.h file in the Arduino IDE by:
    • Either click on the button just below the serial monitor icon and choose New Tab, or use Ctrl+Shift+N keys.
    Arduino IDE 2 adds file
    • Give the file the name pitches.h and click the OK button.
    Arduino IDE 2 adds file pitches.h
    • Copy the code below and paste it into the new pitches.h file.
    /************************************************* * Public Constants *************************************************/ #define NOTE_B0 31 #define NOTE_C1 33 #define NOTE_CS1 35 #define NOTE_D1 37 #define NOTE_DS1 39 #define NOTE_E1 41 #define NOTE_F1 44 #define NOTE_FS1 46 #define NOTE_G1 49 #define NOTE_GS1 52 #define NOTE_A1 55 #define NOTE_AS1 58 #define NOTE_B1 62 #define NOTE_C2 65 #define NOTE_CS2 69 #define NOTE_D2 73 #define NOTE_DS2 78 #define NOTE_E2 82 #define NOTE_F2 87 #define NOTE_FS2 93 #define NOTE_G2 98 #define NOTE_GS2 104 #define NOTE_A2 110 #define NOTE_AS2 117 #define NOTE_B2 123 #define NOTE_C3 131 #define NOTE_CS3 139 #define NOTE_D3 147 #define NOTE_DS3 156 #define NOTE_E3 165 #define NOTE_F3 175 #define NOTE_FS3 185 #define NOTE_G3 196 #define NOTE_GS3 208 #define NOTE_A3 220 #define NOTE_AS3 233 #define NOTE_B3 247 #define NOTE_C4 262 #define NOTE_CS4 277 #define NOTE_D4 294 #define NOTE_DS4 311 #define NOTE_E4 330 #define NOTE_F4 349 #define NOTE_FS4 370 #define NOTE_G4 392 #define NOTE_GS4 415 #define NOTE_A4 440 #define NOTE_AS4 466 #define NOTE_B4 494 #define NOTE_C5 523 #define NOTE_CS5 554 #define NOTE_D5 587 #define NOTE_DS5 622 #define NOTE_E5 659 #define NOTE_F5 698 #define NOTE_FS5 740 #define NOTE_G5 784 #define NOTE_GS5 831 #define NOTE_A5 880 #define NOTE_AS5 932 #define NOTE_B5 988 #define NOTE_C6 1047 #define NOTE_CS6 1109 #define NOTE_D6 1175 #define NOTE_DS6 1245 #define NOTE_E6 1319 #define NOTE_F6 1397 #define NOTE_FS6 1480 #define NOTE_G6 1568 #define NOTE_GS6 1661 #define NOTE_A6 1760 #define NOTE_AS6 1865 #define NOTE_B6 1976 #define NOTE_C7 2093 #define NOTE_CS7 2217 #define NOTE_D7 2349 #define NOTE_DS7 2489 #define NOTE_E7 2637 #define NOTE_F7 2794 #define NOTE_FS7 2960 #define NOTE_G7 3136 #define NOTE_GS7 3322 #define NOTE_A7 3520 #define NOTE_AS7 3729 #define NOTE_B7 3951 #define NOTE_C8 4186 #define NOTE_CS8 4435 #define NOTE_D8 4699 #define NOTE_DS8 4978
    • Upload the code: Click the Upload button in the Arduino IDE.
    • Test it: Move your hand in front of the sensor.
    • Listen: The piezo buzzer plays the melody.
    • Serial Monitor: This sketch prints nothing. If you add your own Serial.print() lines, use Serial.begin(115200) and set the baud rate to 115200.
    • Pro Tip: A passive buzzer module plays the melody best, because it follows each note.

    Line-by-line Code Explanation

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

    ※ NOTE THAT:

    The above code using delay() function. This blocks other code during playing melody. To avoid blocking other code, use the ezBuzzer library instead. This library is designed for buzzer to beep or play melody without blocking other code.

Application and Project Ideas

Here are simple ways to use the ESP32 C3 Super Mini with an ultrasonic sensor and a piezo buzzer.

  • Parking helper: Beep when a car gets close to the garage wall
  • Doorway alert: Sound when someone walks through a door
  • Desk distance reminder: Beep when you sit too close to the screen
  • Robot warning: Sound when a small robot gets close to an obstacle
  • Welcome melody: Play a song when a person walks up to a shop counter

Challenge Yourself

Ready for more? Try these challenges with your ESP32 C3 Super Mini.

  • Easy: Change DISTANCE_THRESHOLD from 50 cm to 20 cm
  • Easy: Replace the melody with another song from your own notes
  • Medium: Beep faster as the object gets closer
  • Medium: Add an LED that lights up together with the buzzer
  • Advanced: Use the ezBuzzer library so the code keeps measuring the distance during the melody

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