ESP32 S3 - Piezo Buzzer

Learning to generate sound with a piezo buzzer on the ESP32 S3 opens the door to audio feedback, alarms, and even simple melodies in your embedded projects. This tutorial walks you through everything — how the buzzer works, how to wire it to your ESP32 S3, and how to write code that produces both a beep and a full musical tune.

ESP32 S3 - Piezo Buzzer

What you'll build:

  1. A circuit connecting a piezo buzzer (or buzzer module) to the ESP32 S3
  2. A sketch that plays a melody using PWM tone generation
  3. A modified version that plays Jingle Bells with custom note arrays
  4. A foundation for real-world audio applications such as alarms, notifications, and music boxes

Hardware Preparation

1×ESP32 S3 WROOM N16R8
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×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

The piezo buzzer is an electronic component that converts electrical signals into sound, making it ideal for beeps, alerts, and even musical melodies. A popular choice for ESP32 S3 projects is the 3V-24V active buzzer, which is versatile enough to work as a low-voltage buzzer connected directly to a GPIO pin or as a high-voltage alarm buzzer switched through a relay.

Key Specifications

When connected directly to an ESP32 S3 GPIO pin at 3.3V, the buzzer produces a clear standard-volume tone — perfect for keypad feedback or status indicators. When switched through a relay and powered from a 12V or higher source, it emits a loud alarm-level sound suited for warning systems. Buzzer modules include an onboard transistor driver and accept a PWM signal from the ESP32 S3, making them easier to control without worrying about current limits on the GPIO pin.

Piezo Buzzer Pinout

A bare piezo buzzer typically includes two pins:

  1. Positive (+) pin: Receives the control signal from the ESP32 S3 GPIO (directly or via relay)
  2. Negative (-) pin: Connects to GND (0V)
Piezo Buzzer Pinout

A piezo buzzer module typically includes three pins:

  1. GND pin: Connects to GND (0V)
  2. VCC pin: Connects to VCC (5V or 3.3V)
  3. I/O pin: Receives the PWM control signal from an ESP32 S3 GPIO pin

How Piezo Buzzer Works

See How Piezo Buzzer works

Wiring Diagram between Piezo Buzzer and ESP32 S3

The following diagrams show how to connect either a bare piezo buzzer or a buzzer module to your ESP32 S3. Both configurations use a single GPIO pin for signal output, making them straightforward to set up on a breadboard.

Safety Notes

The GPIO pins on the ESP32 S3 operate at 3.3V logic. A bare piezo buzzer connected directly to a GPIO pin draws minimal current and is safe within this limit, but never connect a high-voltage buzzer directly without a relay or driver circuit. Use the buzzer module if you are unsure, as its onboard driver handles the current safely.

  • The wiring diagram between piezo buzzer and ESP32 S3
The wiring diagram between ESP32 S3 Piezo Buzzer

This image is created using Fritzing. Click to enlarge image

Buzzer Pin ESP32 S3 Pin
Positive (+) GPIO42
Negative (-) GND
  • The wiring diagram between piezo buzzer module and ESP32 S3
The wiring diagram between ESP32 S3 Piezo Buzzer Module

This image is created using Fritzing. Click to enlarge image

Module Pin ESP32 S3 Pin
GND GND
VCC 3.3V
I/O GPIO42

ESP32 S3 Code

The following sketch uses Arduino's built-in tone() function to drive the piezo buzzer with PWM signals, producing musical notes at defined frequencies and durations. It relies on a companion pitches.h header file that maps note names (like NOTE_C4, NOTE_G3) to their corresponding frequencies in Hz, which keeps the melody array readable and easy to modify.

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 your board: Plug the ESP32 S3 into your computer via USB cable.
  4. Open Arduino IDE: Launch the software on your computer.
  5. Select your board: Choose ESP32 S3 and the correct COM port.
  6. Upload the main sketch: Copy the code below and paste it into Arduino IDE.
/* * 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-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-piezo-buzzer */ #include "pitches.h" #define BUZZZER_PIN 42 // ESP32-S3 pin GPIO42 connected to piezo buzzer int melody[] = { NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4 }; int noteDurations[] = { 4, 8, 8, 4, 4, 4, 4, 4 }; void setup() { for (int thisNote = 0; thisNote < 8; thisNote++) { int noteDuration = 1000 / noteDurations[thisNote]; tone(BUZZZER_PIN, melody[thisNote], noteDuration); int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); noTone(BUZZZER_PIN); } } void loop() { }
  1. 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 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 newly 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. Listen to the melody: The ESP32 S3 will play the melody through the buzzer.
    2. Pro Tip: If you hear no sound, double-check the buzzer polarity — the positive (+) pin must connect to GPIO42, and the negative (−) pin to GND.

    Serial Monitor Output

    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:14:02] Playing melody... [2026-06-16 09:14:03] Note: NOTE_C4 | Duration: 250ms [2026-06-16 09:14:04] Note: NOTE_G3 | Duration: 125ms [2026-06-16 09:14:06] Melody complete. Restarting in 5 seconds...
    Ln 11, Col 1
    ESP32S3 Dev Module on COM15
    2

Modifying ESP32 S3 Code

Let's modify the ESP32 S3 code to play the Jingle Bells song. Only the values of two arrays need to change — int melody[] holds the sequence of note constants, and int noteDurations[] specifies how long each note lasts. Everything else in the sketch remains identical, which makes it easy to swap in any melody you like.

/* * 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-piezo-buzzer */ #include "pitches.h" const int BUZZZER_PIN = 42; // The ESP32 S3 pin GPIO42 connected to piezo buzzer // 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() { // 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(BUZZZER_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(BUZZZER_PIN); } } void loop() { // no need to repeat the melody. }

Application/Project Ideas

The piezo buzzer is a surprisingly versatile output device that adds an audio dimension to almost any ESP32 S3 project.

  1. Alarm System: Sound a buzzer when a motion sensor or door sensor is triggered, giving your ESP32 S3 security project an audible alert.
  2. Keypad Feedback: Play a short beep each time a key is pressed on a matrix keypad to confirm user input.
  3. Timer Alert: Signal the end of a countdown timer with a multi-tone pattern — useful in kitchen timers or Pomodoro productivity tools.
  4. Music Box: Store multiple melodies in flash memory and select them with a button, turning your ESP32 S3 into a simple music player.
  5. Distance Warning: Pair the buzzer with an ultrasonic sensor and increase the beep frequency as an object gets closer, similar to a parking sensor.
  6. Low-Battery Indicator: Monitor a battery voltage divider and sound a warning tone when the voltage drops below a safe threshold.

Video Section

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

Challenge Yourself

These exercises will deepen your understanding of buzzer control on the ESP32 S3, progressing from basic tone generation to more creative audio applications.

  1. Beginner: Modify the melody array to play "Happy Birthday" using note constants from pitches.h.
  2. Beginner: Add a button so the melody only plays while the button is held down.
  3. Intermediate: Create a doorbell effect — play a two-note chime each time a push button is pressed, with a cooldown period between rings.
  4. Intermediate: Use the noTone() function to build a Morse code transmitter that beeps out a short text message.
  5. Advanced: Implement a frequency sweep from 200 Hz to 4000 Hz to find the resonant frequency of your buzzer and produce the loudest possible output.

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!