Arduino - Piezo Buzzer
This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://arduinogetstarted.com/tutorials/arduino-piezo-buzzer
This build shows how to drive a piezo buzzer with an Arduino Uno to produce beeps and short melodies. It covers the difference between active and passive buzzers, how to wire either a bare buzzer or a buzzer module to the board, and how to use the Arduino tone() and noTone() functions to play a sequence of notes without generating the square wave by hand.
Supplies
Everything below is what the build uses. The links go to the parts we make and test.
- Official Arduino Uno
- DIYables STEM V3, Fully Compatible with Arduino Uno R3
- USB 2.0 cable type A/B (for USB-A PC)
- USB 2.0 cable type C/B (for USB-C PC)
- 3-24V Active Piezo Buzzer
- Active Piezo Buzzer Module
- Passive Piezo Buzzer Module
- Breadboard
- Jumper Wires
- Screw Terminal Block Shield for Arduino Uno
- Sensors/Servo Expansion Shield for Arduino Uno
- Breadboard Shield for Arduino Uno
- Enclosure for Arduino Uno
- Prototyping Base Plate & Breadboard Kit for Arduino UNO
- DIYables STEM V3 Starter Kit (Arduino included)
- DIYables Sensor Kit (18 sensors/displays)
Wiring
For a standard two-pin buzzer, connect the negative pin to GND and the positive pin to an Arduino digital pin, which supplies the control signal directly (or via a relay). The example code uses pin 8 as BUZZER_PIN.
For a piezo buzzer module with three pins, connect GND to GND, VCC to a 5V or 3.3V supply, and the I/O pin to the Arduino pin that will carry the PWM control signal.
How It Works
An active buzzer makes a constant sound as soon as VCC is applied to its positive pin, and makes a tone if you feed it a square wave at a given frequency with 50% duty cycle. A passive buzzer stays silent with plain VCC; it only produces sound when driven with that square wave. In both cases, changing the frequency of the square wave changes the pitch, which is how a sequence of tones becomes a melody.
The Arduino tone() and noTone() functions generate and stop that square wave for you, so the sketch never has to toggle the pin manually.
Code
The sketch includes a pitches.h file that defines note-frequency constants like NOTE_C4. The melody[] array lists the notes to play, and noteDurations[] lists their lengths as fractions of a second (4 = quarter note, 8 = eighth note). In setup(), a loop walks through both arrays, calls tone(BUZZER_PIN, note, duration) to sound each note, waits slightly longer than the note's duration for separation, then calls noTone() before moving to the next note. loop() stays empty since the melody plays once.
arduino-piezo-buzzer.ino - the sketch from the tutorial, unchanged.
Full Tutorial
The complete tutorial, wiring diagram and code explanation: https://arduinogetstarted.com/tutorials/arduino-piezo-buzzer