Home

Guide no. 38

Two wires and one command: Arduino plays “Frère Jacques”. Why you need a passive buzzer, how to write the notes and how to turn it into an alarm.

By Ada Intelisano · informaticait.it technical team

Download the PDF (A4, 4 pages) Free to print and share, without changes — CC BY-ND 4.0
In short
  • The passive buzzer goes between a pin and GND: two wires.
  • Every note is a number of hertz: A is 440.
  • tone plays a note, an array holds the melody.
  • One note at a time; on the UNO it interferes with PWM on pins 3 and 11.
Time: 20 minutesLevel: day fourBefore: guide no. 34

A buzzer turns the signal from a pin into a sound. With a single command, tone, Arduino plays the note you want: by the end of this guide it will play “Frère Jacques”.

1Get the parts ready

  • a passive buzzer (piezo): the one with no electronics inside
  • Arduino UNO, breadboard, two jumper wires
New word: passive or activeAn active buzzer has an oscillator inside: just give it power and it always makes the same “beeep”. A passive one plays the note Arduino sends it: that's the one you need for melodies.

2Build the circuit

Buzzer on pin 8SCLSDAAREFGND131211109876543210IOREFRESET3.3V5VGNDGNDVINDIGITAL (PWM ~)POWERANALOG INUNO−++passive buzzer8GNDinformaticait.it

The buzzer has two legs: the one marked + goes to pin 8 (blue wire), the other to the − rail, which is connected to GND. Two wires in all.

3Notes are numbers

Every note is a vibration with a certain number of oscillations per second (hertz). These are the ones we use:

C
262
D
294
E
330
F
349
G
392
A
440
New word: tonetone(8, 440, 400) plays the 440 Hz note (the A) on pin 8 for 400 thousandths of a second. noTone(8) silences the buzzer.

4Write and upload the melody

// The buzzer plays “Frère Jacques”. Passive (piezo) buzzer between pin 8 and GND.
#include <Arduino.h>

const int BUZZER = 8;        // with the ESP32 write 25

// the notes in hertz (the same as in the pitches.h file from the Arduino examples)
const int NOTE_C4 = 262, NOTE_D4 = 294, NOTE_E4 = 330, NOTE_F4 = 349, NOTE_G4 = 392, NOTE_A4 = 440;

const int MELODY[]   = {NOTE_C4, NOTE_D4, NOTE_E4, NOTE_C4,  NOTE_C4, NOTE_D4, NOTE_E4, NOTE_C4,
                        NOTE_E4, NOTE_F4, NOTE_G4,  NOTE_E4, NOTE_F4, NOTE_G4};
const int DURATION[] = {400, 400, 400, 400,  400, 400, 400, 400,  400, 400, 800,  400, 400, 800};
const int NUM_NOTES = sizeof(MELODY) / sizeof(MELODY[0]);

void setup() {
  for (int i = 0; i < NUM_NOTES; i++) {
    tone(BUZZER, MELODY[i], DURATION[i]);    // play the note for the given time
    delay(DURATION[i] * 1.2);                // wait for it to end, plus a short pause
  }
  noTone(BUZZER);
}

void loop() {
  // the melody plays only once: press RESET to hear it again
}
New word: arrayA row of values with a single name. MELODY[] holds the notes in order, DURATION[] how long each one lasts; MELODY[0] is the first.
✓ What you should seeAs soon as it's uploaded, the buzzer plays the beginning of “Frère Jacques”. To hear it again, press the board's RESET button.
If it doesn't work
  • No sound: is the wire on pin 8? Is the buzzer passive? With an active one you'd hear a single tone.
  • It plays very quietly: that's normal, small piezos aren't loud. Hold it close to your ear or rest it on a box.
  • It always makes the same sound, whatever the note: it's an active buzzer, you need a passive one.

5Challenge yourself

Try it yourself
  • Change the durations: all 200 for a fast version.
  • Add the pushbutton from guide no. 35: the melody starts when you press it (move the code from setup to loop).
  • Make an alarm: two alternating notes, 880 and 440, until you press the button.

6Two things to know

  • Arduino plays one note at a time, and it can't go below 31 Hz.
  • On the UNO, while tone is playing, PWM on pins 3 and 11 doesn't work properly: for fading LEDs use other pins (guide no. 37).

7And with the ESP32?

Same circuit with the buzzer on GPIO 25; in the sketch write const int BUZZER = 25;. The melody is the same.

✓ What you learned
  • a passive buzzer plays the note you give it, an active one always makes the same sound;
  • tone(pin, frequency, duration) plays, noTone silences;
  • an array keeps many values in order under a single name.
Sources: Arduino, language reference: tone() and noTone(); “toneMelody” example (pitches.h) · informaticait guides no. 34, 35 and 37

Useful products

Other guides

All our guides →
Loading...
Back to top