Arduino - Piezo Buzzer

by DIYables in Circuits > Arduino

13 Views, 0 Favorites, 0 Comments

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

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.

/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-piezo-buzzer
*/

#include "pitches.h"

#define BUZZER_PIN 8 // The Arduino pin connected to the buzzer

// notes in the melody:
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};

void setup() {
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; 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);
}
}

void loop() {
// no need to repeat the melody.
}

Full Tutorial

The complete tutorial, wiring diagram and code explanation: https://arduinogetstarted.com/tutorials/arduino-piezo-buzzer