Utility Bar

ESP32 Using a Passive Buzzer Tutorial

ESP32 Passive Buzzer Tutorial

Ethan Zaitchik |

Table of Contents

    This ESP32 passive buzzer tutorial is designed for beginners who want to generate sound using their ESP32. In this lesson you will learn how a passive buzzer works, how to connect it to an ESP32, and how to generate different tones using simple Arduino code.

    This lesson is part of our ESP32 tutorials series, where each lesson builds on the previous one.

    What you will learn

    • How a passive buzzer works
    • How to connect a passive buzzer to an ESP32
    • How to generate simple tones using Arduino code
    • How to control sound duration and patterns

    What you will need

    • ESP32 board
    • Passive buzzer
    • Breadboard
    • Jumper wires
    • USB cable

    Step 1 - Understand the passive buzzer

    A passive buzzer is different from an active buzzer. It does not make sound on its own. Instead the ESP32 must generate a fast on and off signal, called a square wave, to create sound.

    By changing how fast this signal switches, you can control the pitch of the sound. This makes passive buzzers ideal for learning how frequency and timing work.

    A passive buzzer has two pins. These are often marked with plus and minus symbols. Unlike an active buzzer, polarity is not critical, but following the markings is recommended for clarity.

    Step 2 - Wire the buzzer

    1. Place the passive buzzer onto the breadboard
    2. Connect one pin of the buzzer to GPIO 32 on the ESP32
    3. Connect the other pin of the buzzer to a GND pin on the ESP32
    ESP32 Passive Buzzer Wiring Diagram

    Step 3 - Upload the buzzer test sketch

    Open the Arduino IDE and copy the following code. This program will make the buzzer play a tone for one second, then stay silent for one second, repeating continuously.

    // Passive buzzer test
    int buzzerPin = 32;
    
    void setup() {
      pinMode(buzzerPin, OUTPUT);
    }
    
    void loop() {
      tone(buzzerPin, 1000);
      delay(1000);
      noTone(buzzerPin);
      delay(1000);
    }
    

    Click Upload and wait for the upload to complete.

    How the Code Works

    This program repeatedly turns a sound on and off using a passive buzzer.

    int buzzerPin = 32;
    This line stores the GPIO pin number used to control the buzzer. In this lesson GPIO 32 is used.
    void setup()
    This function runs once when the ESP32 starts.
    pinMode(buzzerPin, OUTPUT);
    This tells the ESP32 that the buzzer pin will be used as an output.
    void loop()
    This function runs continuously.
    • tone(buzzerPin, 1000) generates a 1000 Hz sound
    • delay(1000) keeps the sound playing for one second
    • noTone(buzzerPin) stops the sound
    • delay(1000) waits one second before repeating

    Step 4 - Listen to the buzzer

    After uploading the code the buzzer should beep for one second, pause for one second, and repeat.

    • Check that the buzzer is wired correctly
    • Confirm the buzzer is passive and not active
    • Try a different GPIO pin if needed

    Step 5 - Optional exercise

    Try changing the frequency value in the tone function. Values like 500, 1500, or 2000 will produce different pitch sounds.

    You can also adjust the delay values to create short beeps, long beeps, or simple sound patterns.

    ESP32 Lessons

    Lesson complete / Next lesson

    You have successfully generated sound using a passive buzzer and your ESP32.

    Next lesson: ESP32 Light Sensor Tutorial using a photoresistor

    Leave a comment

    Please note: comments must be approved before they are published.

    // Table of contents