ESP32 S3 UNO - Ultrasonic Sensor - Piezo Buzzer

This ESP32 S3 Uno ultrasonic sensor piezo buzzer tutorial shows you how to make a buzzer sound when an object comes close. You wire an HC-SR04 distance sensor and a buzzer to the ESP32 S3 Uno form board, then write Arduino code that turns the measured distance into a beep or a short song. It is a simple proximity alarm, like a parking sensor.

In this tutorial, you will:

  1. Wire an ultrasonic sensor and a piezo buzzer to the ESP32 S3 Uno
  2. Measure distance with the HC-SR04 and pulseIn()
  3. Turn the buzzer on when an object is closer than 50 cm, and off when it moves away
  4. Play a melody with tone() when someone comes near the sensor
ESP32 S3 Uno ultrasonic sensor piezo buzzer

Hardware Preparation

1×ESP32 S3 Uno-form Board
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×5V to 3.3V Level Converter
1×Breadboard
1×Jumper Wires

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

This project joins two parts. The ultrasonic sensor tells the ESP32 S3 Uno how far away an object is, and the buzzer gives you a sound when that object is too close. If you have not used these parts before, read their own tutorials first.

The examples here use a small 3–5V buzzer that you can drive straight from a pin. If you want a loud 12V buzzer instead, you need a relay. The ESP32 S3 Uno - Buzzer tutorial shows how.

Why the HC-SR04 needs care on this board

The HC-SR04 runs on 5V, so its ECHO pin also sends back a 5V pulse. The ESP32 S3 Uno uses 3.3V logic, and its pins are NOT 5V tolerant. For this reason, put a level converter (or a simple voltage divider) on the ECHO line. The TRIG line is fine as it is, because the 3.3V pulse from the board is high enough for the sensor.

ESP32 S3 Uno Pinout

The image below shows the pinout diagram of the ESP32 S3 Uno form board. Use it to find the Uno header pins (D3, D6, D7…) and their GPIO numbers when you wire the sensor and the buzzer.

ESP32 S3 Uno pinout diagram

Wiring Diagram

You can use a bare piezo buzzer or a buzzer module. The sensor wiring is the same in both cases, and the buzzer signal always goes to D3 (GPIO17).

With a piezo buzzer

The wiring diagram between ESP32 S3 Uno ultrasonic sensor piezo buzzer

This image is created using Fritzing. Click to enlarge image

With a piezo buzzer module

The wiring diagram between ESP32 S3 Uno ultrasonic sensor piezo buzzer module

This image is created using Fritzing. Click to enlarge image

Pin connections

Component Pin ESP32 S3 Uno Pin
Ultrasonic Sensor VCC 5V
Ultrasonic Sensor GND GND
Ultrasonic Sensor TRIG D6 (GPIO3)
Ultrasonic Sensor ECHO D7 (GPIO14) through the level converter
Piezo Buzzer (+) or Module I/O D3 (GPIO17)
Piezo Buzzer (-) or Module GND GND
Buzzer Module VCC 3.3V or 5V

WARNING

The ECHO pin of the HC-SR04 outputs 5V, but the ESP32 S3 Uno pins only accept up to 3.3V. Connect ECHO to D7 (GPIO14) through a 5V to 3.3V level converter. If you do not have one, use a voltage divider: a 1kΩ resistor from ECHO to D7, and a 2kΩ resistor from D7 to GND.

WARNING

TRIG is on D6, which is GPIO3 on the ESP32 S3 Uno. GPIO3 is a boot strapping pin, and the board reads it at startup. The TRIG input of the sensor does not drive this pin, so it normally works fine. If the board does not boot or upload, unplug the TRIG wire, upload the code, and then plug it back in.

ESP32 S3 Uno Code - Simple Sound

This first sketch sends a short pulse to the sensor every half second and times the echo with pulseIn(). It turns that time into centimeters. When the distance is under 50 cm, the buzzer pin goes HIGH; otherwise it goes LOW. The distance is also printed to the Serial Monitor so you can see what the sensor measures.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-ultrasonic-sensor-piezo-buzzer */ #define TRIG_PIN 3 // The ESP32 S3 Uno pin D6 (GPIO3) connected to the ultrasonic sensor's TRIG pin #define ECHO_PIN 14 // The ESP32 S3 Uno pin D7 (GPIO14) connected to the ultrasonic sensor's ECHO pin #define BUZZER_PIN 17 // The ESP32 S3 Uno pin D3 (GPIO17) connected to Piezo Buzzer's pin #define DISTANCE_THRESHOLD 50 // centimeters float duration_us, distance_cm; void setup() { Serial.begin (9600); // initialize serial port pinMode(TRIG_PIN, OUTPUT); // set ESP32 S3 Uno pin to output mode pinMode(ECHO_PIN, INPUT); // set ESP32 S3 Uno pin to input mode pinMode(BUZZER_PIN, OUTPUT); // set ESP32 S3 Uno 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 build your first distance alarm:

  1. New to the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
  2. Wire it up as shown in the diagram.
  3. Connect the board to your computer with a USB Type-C cable.
  4. Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
  5. Copy the code above and paste it into the Arduino IDE.
  6. Upload the code by clicking the Upload button.
  7. Open the Serial Monitor and set the baud rate to 9600.
  8. Move your hand in front of the sensor.
  9. Listen to the buzzer. It sounds when your hand is closer than 50 cm and stops when you pull it away.
  10. Tip: Change DISTANCE_THRESHOLD in the code to make the alarm react at a different distance.
∞
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
distance: 112.45 cm distance: 87.30 cm distance: 41.62 cm distance: 18.91 cm distance: 64.08 cm
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Code Explanation

Each line of the sketch has a comment that says what it does. Read them from top to bottom, and you will follow the whole flow: send the TRIG pulse, measure the ECHO time, work out the distance, and switch the buzzer.

ESP32 S3 Uno Code - Melody

A beep is useful, but a tune is more fun. This second sketch measures distance in the same way. When an object is closer than 50 cm, it plays the whole "Jingle Bells" chorus with tone(). For a real melody, use a passive buzzer or a passive buzzer module.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-ultrasonic-sensor-piezo-buzzer */ #include "pitches.h" #define TRIG_PIN 3 // The ESP32 S3 Uno pin D6 (GPIO3) connected to the ultrasonic sensor's TRIG pin #define ECHO_PIN 14 // The ESP32 S3 Uno pin D7 (GPIO14) connected to the ultrasonic sensor's ECHO pin #define BUZZER_PIN 17 // The ESP32 S3 Uno pin D3 (GPIO17) connected to Piezo Buzzer's pin #define DISTANCE_THRESHOLD 50 // centimeters 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 Uno pin to output mode pinMode(ECHO_PIN, INPUT); // set ESP32 S3 Uno 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

The melody needs a second file that holds the note frequencies. Follow these steps:

  1. Copy the code above and paste it into the Arduino IDE.
  2. Add a new tab for the note list. Click the button below the Serial Monitor icon and choose New Tab, or press Ctrl+Shift+N.
Arduino IDE 2 adds file
  1. Name the tab pitches.h and click OK.
Arduino IDE 2 adds file pitches.h
  1. Paste the note list below into the pitches.h tab.
/************************************************* * 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. Upload the code to the ESP32 S3 Uno with the Upload button.
  2. Move your hand in front of the sensor.
  3. Listen to the melody from the buzzer.
  4. Tip: Keep your hand still while the song plays. The sketch does not measure distance again until the melody ends.

Code Explanation

The comments inside the code explain each step, so read through them to see how the melody and note durations work.

※ NOTE THAT:

  • This sketch uses delay(), so the ESP32 S3 Uno can do nothing else while the song plays. If your project must keep running during the melody, use the ezBuzzer library. It plays beeps and songs in the background without blocking your code.
  • This code is made for learning. Ultrasonic readings can jump because of noise. For a real project, filter the readings first. See how to filter noise for the ultrasonic sensor.

Video Tutorial

Watch the video below to see this ESP32 S3 Uno project step by step.

FAQ

Can I power the HC-SR04 from the 3.3V pin of the ESP32 S3 Uno?

Most HC-SR04 modules need 5V to work well. At 3.3V they often give no reading or wrong values. Power the sensor from the 5V pin, and protect the ECHO line with a level converter or a voltage divider.

Why do I need a level converter on the ECHO pin?

With 5V power, the ECHO pin sends back a 5V pulse. The ESP32 S3 Uno pins are 3.3V only and are not 5V tolerant, so a 5V signal can damage the pin over time. The UNO R4 is a 5V board and does not need this, but the ESP32 S3 Uno does.

Can I use the same code as the UNO R4 version?

Yes, almost. The logic, pulseIn() and tone() work the same. Only the pin numbers change, because you write GPIO numbers in code: D6 is 3, D7 is 14, and D3 is 17. In Arduino IDE, select the ESP32S3 Dev Module board.

Why does the buzzer only beep and not play a song?

You are using an active buzzer. It has its own oscillator and makes one fixed tone, whatever frequency you send. Use a passive buzzer or a passive buzzer module to hear the melody.

What if my project needs more pins than the Uno headers give?

The ESP32 S3 Uno has two extra rows of holes with more GPIOs: GPIO15 and GPIO16 (I/O, PWM, analog), GPIO45 and GPIO35–GPIO42 (I/O, PWM), and GPIO47/GPIO48 (output only, PWM). Solder pin headers to use them and write the GPIO number in code. For a second sensor or a warning LED, GPIO35–GPIO42 are good choices. Do not use GPIO47/GPIO48 for an ECHO input, because they are output only. Be careful with two things. First, GPIO47 and GPIO48 may run at 1.8V instead of 3.3V on some modules (especially ones with "V" in the name, like R8V or R16V), so 3.3V devices can be damaged or act strangely. Second, GPIO0, GPIO3 (D6), GPIO45 and GPIO46 (D9) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code. Use the other extra pins first.

Troubleshooting

Problem Possible Cause Solution
No COM port in Arduino IDE USB driver missing or charge-only cable Install the CP210x or CH340 driver, use a data USB cable, or hold BOOT while pressing RESET
Upload fails or board does not start TRIG wire on D6 (GPIO3) affects the boot strapping pin Unplug the TRIG wire, upload the code, then plug it back in
Distance is always 0 and the buzzer never stops No echo comes back so pulseIn() returns 0 Check the ECHO wire and the level converter, and make sure VCC goes to 5V
Readings jump a lot Noise or a soft surface in front of the sensor Aim at a flat hard object and filter the readings in code
No sound from the buzzer Wrong polarity or wrong pin Connect + to D3 (GPIO17) and - to GND, and check module power
Melody sounds like one flat tone Active buzzer used instead of passive Use a passive buzzer or passive buzzer module
Compile error about NOTE_E5 pitches.h tab is missing or named wrong Add a new tab named exactly pitches.h and paste the note list
Serial Monitor shows strange characters Wrong baud rate Set the Serial Monitor to 9600 baud

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