The Sensor Kit has two buzzers that look almost the same. The active one makes a single beep. The passive one can play any note, because the UNO decides how fast it vibrates. In this lesson you write a melody as a list of notes and lengths, and play it at the press of a button.
What you'll learn
- The difference between active and passive buzzers
- How
tone()produces a note of a given pitch - How to store a melody in two arrays
- How note names map to frequencies
What you need
- UNO R3 board and USB cable (UNO R3 Starter Kit)
- Passive buzzer module (45-in-1 Sensor Kit)
- Button module (45-in-1 Sensor Kit)
- Breadboard and jumper wires (UNO R3 Starter Kit)
Wiring

| From | To |
|---|---|
| Passive buzzer module S | UNO pin 8 |
| Passive buzzer module − | GND |
| Button module S | UNO pin 2 |
| Button module + and − | 5V and GND |
Sensor modules from different makers can label and order their pins differently. Always follow the labels printed on your module.
The sketch
const int BUZZER_PIN = 8;
const int BUTTON_PIN = 2;
// Note frequencies in Hz
const int C4 = 262, D4 = 294, E4 = 330, F4 = 349, G4 = 392, A4n = 440, REST = 0;
// "Twinkle, Twinkle, Little Star"
const int melody[] = { C4, C4, G4, G4, A4n, A4n, G4, REST, F4, F4, E4, E4, D4, D4, C4 };
const int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2 };
const int NOTES = sizeof(melody) / sizeof(melody[0]);
const int BEAT_MS = 350; // length of one beat: smaller = faster
int buttonRest;
void playSong() {
for (int i = 0; i < NOTES; i++) {
int length = beats[i] * BEAT_MS;
if (melody[i] != REST) tone(BUZZER_PIN, melody[i], length * 0.9);
delay(length); // the small gap separates repeated notes
}
noTone(BUZZER_PIN);
}
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
buttonRest = digitalRead(BUTTON_PIN); // works with modules that read HIGH or LOW when idle
}
void loop() {
if (digitalRead(BUTTON_PIN) != buttonRest) {
playSong();
while (digitalRead(BUTTON_PIN) != buttonRest) delay(10); // wait for release
}
}
Upload the sketch and press the button. The melody plays once each time.
How it works
Sound is air vibrating. The note A above middle C vibrates 440 times per second, or 440 hertz (Hz). tone(pin, 440) makes the pin switch on and off 440 times per second, and the passive buzzer's membrane moves with it. An active buzzer has its own oscillator inside, so it can only beep at its one built-in pitch.
The melody is stored in two arrays that belong together: melody[3] is the fourth note and beats[3] is how long it lasts. sizeof counts the notes for you, so you can make the song longer without changing anything else. Each note plays for 90 % of its time and is silent for the rest, so two equal notes in a row sound like two notes and not one long one.
Try this next
- Write your own melody. Search for "note frequencies" to find the numbers for more notes.
- Use a second button to play a different song.
- Light the RGB LED module in a different colour for each note.
Troubleshooting
- Only clicks, no notes: you may be using the active buzzer. The passive one usually has a green circuit board visible underneath; the active one is sealed.
- The song plays by itself: check the button wire on pin 2.
- Very quiet: passive buzzers are quieter than active ones. Notes between 1000 and 3000 Hz sound loudest.