Passive buzzer on pin 8, on an Arduino Uno
A passive buzzer on pin 8 plays the eight notes of a C major scale with tone(), one every half second, and names each note as it plays. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $30. On Mokxi they're free.
What you'll learn
- Passive versus active buzzers
- Frequency and pitch
- Playing notes with
tone - Storing a tune in an array
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 3
How it works
A passive buzzer has no oscillator inside, so a steady HIGH only gives one click. Pin 8 goes to one leg and ground to the other. The sketch uses tone to make pin 8 swing up and down at a set frequency, and the buzzer turns that into a note.
The sketch walks through eight frequencies, from 262 Hz up to 523 Hz. That is a C major scale. Each note plays for 500 ms, with a short gap, and its name and frequency are printed. After the top note it waits a second and starts again.
Parts list
3 parts, plus the jumper wires. They retail for about $30; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Piezo speakerIn Starter Kit, Claw Machine Kit, Vending Machine Kit.
Kits that can build it
These 11 starter kits have every part in the circuit. Each kit's page lists what else it can build.
- ELEGOO UNO R3 Super Starter Kit
- ELEGOO UNO R3 Most Complete Starter Kit
- Arduino Starter Kit
- Freenove Ultimate Starter Kit
- SunFounder Inventor Lab Kit
- REXQualis UNO R3 Super Starter Kit
- LAFVIN Super Starter Kit
- keyestudio Super Starter Kit
- Vilros Arduino Uno 3 Ultimate Starter Kit
- SunFounder Universal Maker Sensor Kit
- Adafruit MetroX Classic Kit
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Piezo speaker pin 2 | Arduino Uno R3 pin GND |
| Piezo speaker pin 1 | Arduino Uno R3 pin 8 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Passive buzzer on pin 8: a C major scale, one note at a time.
//
// pin 8 the buzzer's + leg
// GND the buzzer's other leg
//
// A passive buzzer has no oscillator inside, so a steady HIGH only clicks it
// once. tone(pin, frequency) makes the pin swing up and down that many times
// a second, and the buzzer turns the swinging into a note: 262 Hz is middle C,
// and each octave up doubles it.
//
// The scale climbs C D E F G A B C, then waits a second and starts again.
// Change the numbers in NOTES to play something else.
const int BUZZER = 8;
const int NOTES[] = {262, 294, 330, 349, 392, 440, 494, 523};
const char *NAMES[] = {"C5", "D5", "E5", "F5", "G5", "A5", "B5", "C6"};
const int COUNT = 8;
const int NOTE_MS = 500;
void setup() {
Serial.begin(9600);
Serial.println("Mokxi Uno: passive buzzer on pin 8");
}
void loop() {
for (int i = 0; i < COUNT; i++) {
Serial.print(NAMES[i]);
Serial.print(" ");
Serial.print(NOTES[i]);
Serial.println(" Hz");
tone(BUZZER, NOTES[i], NOTE_MS);
delay(NOTE_MS + 50);
}
delay(1000);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Starts serial at 9600 and prints a title line. The buzzer pin needs no setup because tone takes care of it.
void setup() {
Serial.begin(9600);
Serial.println("Mokxi Uno: passive buzzer on pin 8");
}loop sketch.ino
Steps through the eight entries of NOTES. For each it prints the name from NAMES and the frequency, plays it with tone(BUZZER, NOTES[i], NOTE_MS), and waits NOTE_MS + 50. After the last note it waits 1000 ms and repeats.
void loop() {
for (int i = 0; i < COUNT; i++) {
Serial.print(NAMES[i]);
Serial.print(" ");
Serial.print(NOTES[i]);
Serial.println(" Hz");
tone(BUZZER, NOTES[i], NOTE_MS);
delay(NOTE_MS + 50);
}
delay(1000);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Only a click, not a note This project
A passive buzzer needs a changing signal. Usetoneto make the pin swing. - Wrong notes This project
Change the numbers inNOTESto change the pitches, and keepNAMESin the same order. - A passive piezo stays silent when you just set the pin HIGH. Piezo speaker
A passive piezo needs a changing signal. Use tone(pin, frequency) or a PWM wave. Around 2 to 4 kHz is loudest. A buzzer with a built in oscillator makes sound from plain DC instead. - Sound is very quiet or the pin seems overloaded. Piezo speaker
Drive the piezo from a pin directly, or through a 100 ohm resistor. For a bigger buzzer, switch it with a transistor. Check that you wired the right two legs and joined grounds. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.