ESP32 S3 - Ultrasonic Sensor - Piezo Buzzer

This tutorial shows you how to use the ESP32 S3 with an ultrasonic sensor and a piezo buzzer to build a proximity-triggered audio alert system. You will implement two practical applications — one that produces a continuous tone when an object is detected nearby, and one that plays a musical melody when the object enters the detection zone.

What you'll build:

  1. An ESP32 S3 circuit with an ultrasonic sensor and piezo buzzer wired to the board
  2. Application 1: Buzzer sounds automatically when an object is close to the sensor and stops when the object moves away
  3. Application 2: Buzzer plays a musical melody each time an object enters the detection range
  4. A foundation for real-world proximity alert, parking sensor, and security projects

Overview of Piezo Buzzer and Ultrasonic Sensor

The ultrasonic sensor measures distance by emitting a sound pulse and timing its echo, while the piezo buzzer converts electrical signals into audible tones that alert the user when an object enters the detection zone. Together on the ESP32 S3 they form a complete sense-and-respond loop — the sensor feeds distance data to the board, the board evaluates it against a threshold, and the buzzer delivers immediate audio feedback. This pattern is the foundation of proximity alarms, parking sensors, and obstacle-avoidance robots.

Key Specifications

For detailed background on each component, refer to these dedicated ESP32 S3 tutorials:

Please note that this tutorial uses a 3–5V buzzer, but you can adapt it for a 12V buzzer. Learn more in the ESP32 S3 - Buzzer tutorial.

Wiring Diagram

Connect the ultrasonic sensor and piezo buzzer to your ESP32 S3 by following the diagram below. Verify every connection before powering the board to avoid accidental short circuits.

The wiring diagram between ESP32 S3 Ultrasonic Sensor Piezo Buzzer

This image is created using Fritzing. Click to enlarge image

Jumper wires are good for a quick test, but not for a final build. A Screw Terminal Block for ESP32 S3 solves this. Plug the ESP32 S3 into it and screw the wires in. The diagram below shows the same wiring with the screw terminal board.

The wiring diagram between ESP32 S3 Ultrasonic Sensor Piezo Buzzer Screw Terminal

This image is created using Fritzing. Click to enlarge image

Another choice is a GPIO Breakout Board for ESP32 S3. Each GPIO has a signal pin with 3.3V and GND pins next to it, as shown below:

The wiring diagram between ESP32 S3 Ultrasonic Sensor Piezo Buzzer GPIO Breakout Board

This image is created using Fritzing. Click to enlarge image

Safety Notes

The GPIO pins on the ESP32 S3 operate at 3.3V logic, but the HC-SR04 ultrasonic sensor's VCC pin requires 5V for reliable operation — connect VCC to the board's 5V pin, not 3.3V. The TRIG and ECHO lines operate at 3.3V logic on most modules, so they connect directly to the ESP32 S3 GPIO pins without a level shifter. A bare piezo buzzer draws minimal current from a GPIO pin and is safe at 3.3V, but never connect a high-voltage buzzer directly without a relay or driver circuit.

Component Pin ESP32 S3 Pin
Ultrasonic Sensor VCC 5V
Ultrasonic Sensor GND GND
Ultrasonic Sensor TRIG GPIO8
Ultrasonic Sensor ECHO GPIO9
Piezo Buzzer Positive (+) GPIO42
Piezo Buzzer Negative (-) GND

ESP32 S3 Code - Simple Sound

The following sketch reads the ultrasonic sensor on every loop iteration, calculates the distance to the nearest object, and drives the piezo buzzer based on whether that distance is below a set threshold. When the object is close the buzzer sounds continuously; when the object moves away the buzzer stops immediately. This direct coupling between sensor reading and buzzer output is the simplest form of proximity-triggered audio you can implement on the ESP32 S3.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-ultrasonic-sensor-piezo-buzzer */ /* * This ESP32-S3 code is created by esp32io.com * * This ESP32-S3 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-s3-ultrasonic-sensor-piezo-buzzer */ #define TRIG_PIN 8 // ESP32-S3 pin GPIO8 connected to Ultrasonic Sensor's TRIG pin #define ECHO_PIN 9 // ESP32-S3 pin GPIO9 connected to Ultrasonic Sensor's ECHO pin #define BUZZER_PIN 42 // ESP32-S3 pin GPIO42 connected to Piezo Buzzer's pin #define DISTANCE_THRESHOLD 50 // centimeters // variables will change: float duration_us, distance_cm; void setup() { Serial.begin (9600); // initialize serial port pinMode(TRIG_PIN, OUTPUT); // set ESP32-S3 pin to output mode pinMode(ECHO_PIN, INPUT); // set ESP32-S3 pin to input mode pinMode(BUZZER_PIN, OUTPUT); // set ESP32-S3 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

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Wire the components: Follow the wiring diagram shown above.
  3. Connect ESP32 S3: Use a USB Type-C cable to connect to your computer.
  4. Setup Arduino IDE: If this is the first time you use ESP32 S3, see how to setup environment for ESP32 S3 on Arduino IDE.
  5. Select your board: Choose the ESP32 S3 board and its COM port in Arduino IDE.
  6. Upload the code: Copy the code above and upload it to your ESP32 S3.
How to upload ESP32 S3 code on Arduino IDE
  1. Move your hand in front of the ultrasonic sensor.
  2. Listen to the buzzer: The piezo buzzer will sound when your hand is close and stop when you pull it away.
  3. Pro Tip: If the sensor gives erratic readings, make sure the TRIG and ECHO wires are not running parallel to power wires — keep signal lines separated to reduce noise.

Serial Monitor Output

Open the Arduino IDE Serial Monitor at 9600 baud to observe the distance readings and buzzer state in real time.

∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
[2026-06-16 09:12:01] Distance: 8 cm — Object detected — Buzzer ON [2026-06-16 09:12:02] Distance: 6 cm — Object detected — Buzzer ON [2026-06-16 09:12:03] Distance: 35 cm — No object — Buzzer OFF [2026-06-16 09:12:04] Distance: 42 cm — No object — Buzzer OFF
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Line-by-line Code Explanation

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

ESP32 S3 Code - Melody

The following sketch plays a full musical melody through the piezo buzzer each time an object enters the detection range of the ultrasonic sensor. It uses a companion pitches.h header file that maps note names such as NOTE_C4 and NOTE_G3 to their frequencies in Hz, keeping the melody array easy to read and modify for any tune you choose.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-ultrasonic-sensor-piezo-buzzer */ /* * This ESP32-S3 code is created by esp32io.com * * This ESP32-S3 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-s3-ultrasonic-sensor-piezo-buzzer */ #include "pitches.h" #define TRIG_PIN 8 // ESP32-S3 pin GPIO8 connected to Ultrasonic Sensor's TRIG pin #define ECHO_PIN 9 // ESP32-S3 pin GPIO9 connected to Ultrasonic Sensor's ECHO pin #define BUZZER_PIN 42 // ESP32-S3 pin GPIO42 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-S3 pin to output mode pinMode(ECHO_PIN, INPUT); // set ESP32-S3 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

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Upload the melody sketch: Copy the code above and paste it to Arduino IDE.
  3. Create the pitches.h file in 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 file's name pitches.h and click OK button
    Arduino IDE 2 adds file pitches.h
    • Copy the below code and paste it to the created 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
    1. Compile and upload: Click the Upload button in Arduino IDE.
    How to upload ESP32 S3 code on Arduino IDE
    1. Move your hand in front of the ultrasonic sensor to enter the detection zone.
    2. Listen to the melody: The piezo buzzer plays the full tune from start to finish each time an object is detected.
    3. Pro Tip: The delay() function inside the melody loop blocks other code during playback. To avoid blocking, replace it with the ezBuzzer library, which is designed to play melodies without blocking other code.

    Serial Monitor Output

    Open the Arduino IDE Serial Monitor at 9600 baud to watch melody playback triggered by proximity detection.

    ∞
    Newbiely | Arduino IDE 2.3.8
    ──
    ☐
    ✕
    File
    Edit
    Sketch
    Tools
    Help
    ESP32S3 Dev Module
    Newbiely.ino
    ···
    8 Serial.println("Hello World!");
    Output
    Serial Monitor
    Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
    New Line
    9600 baud
    [2026-06-16 09:18:10] Distance: 9 cm — Object detected — Starting melody [2026-06-16 09:18:10] Note: NOTE_C4 | Duration: 250ms [2026-06-16 09:18:11] Note: NOTE_G3 | Duration: 125ms [2026-06-16 09:18:14] Melody complete
    Ln 11, Col 1
    ESP32S3 Dev Module on COM15
    2

    Line-by-line Code Explanation

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

    ※ NOTE THAT:

    The above code uses the delay() function. This blocks other code during melody playback. 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 Ideas

The ESP32 S3 ultrasonic sensor and piezo buzzer combination is a versatile building block for dozens of proximity-aware projects. Here are some ideas to spark your imagination:

  1. Parking sensor: Build a car parking aid that beeps faster as a vehicle gets closer to a wall or obstacle.
  2. Intruder alarm: Create a security alert that sounds a loud tone whenever someone enters a monitored zone.
  3. Blind-assist cane: Attach the sensor to a walking cane and alert the user to obstacles with buzzer patterns.
  4. Liquid level alert: Mount the sensor above a tank and trigger the buzzer when the liquid level drops below a threshold.
  5. Proximity doorbell: Play a chime melody automatically when a visitor approaches your door.
  6. Obstacle-avoidance robot: Use the buzzer as an audible warning while the robot navigates around objects.

Video Section

Watch the step-by-step video walkthrough for this ESP32 S3 project below.

Challenge Yourself

Now that you have a working ESP32 S3 ultrasonic sensor and piezo buzzer circuit, push your skills further with these challenges ranked by difficulty:

  1. Beginner: Change the distance threshold so the buzzer activates only when an object is within 5 cm instead of the default value.
  2. Beginner: Make the buzzer beep three short times instead of sounding continuously when an object is detected.
  3. Intermediate: Vary the buzzer tone frequency based on distance — a higher pitch for closer objects and a lower pitch for farther ones.
  4. Intermediate: Use the ezBuzzer library to play a melody in the background without blocking the distance measurement loop.
  5. Advanced: Add an OLED display that shows the live distance reading alongside the buzzer state.
  6. Advanced: Implement a multi-zone alarm with three distance thresholds that trigger different buzzer patterns for each zone.

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