Passive buzzer scale on pin 12, on an Arduino Uno
A passive buzzer on pin 12 plays the eight notes of a C major scale with tone(), half a second each, and names every note 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
- Active versus passive buzzers
- Frequency and musical notes
- Playing a tone with
tone - Looping over an array
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 3
How it works
A passive buzzer has no tone generator inside. It clicks once each time the voltage across it changes. The tone function switches pin 12 on and off at a chosen frequency, and that frequency is the note you hear.
The sketch holds the eight notes of a C major scale, from C4 at 262 Hz to C5 at 523 Hz. For each one it prints the name and frequency, plays it for half a second, then waits a full second before the next. After the last note it pauses one more 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 12 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Passive buzzer: a C major scale on pin 12.
//
// pin 12 -> passive buzzer + , the other leg -> GND
//
// A passive buzzer has no oscillator of its own: it clicks once each time
// the voltage changes. tone() makes pin 12 switch on and off at a frequency,
// and that frequency is the note you hear. Any pin will do, PWM or not.
//
// Each note plays for half a second, then rests for half a second.
const int BUZZER_PIN = 12;
const int NOTES[8] = {262, 294, 330, 349, 392, 440, 494, 523};
const char *NAMES[8] = {"C4", "D4", "E4", "F4", "G4", "A4", "B4", "C5"};
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: passive buzzer on pin 12");
}
void loop() {
for (int i = 0; i < 8; i++) {
Serial.print(NAMES[i]);
Serial.print(" ");
Serial.print(NOTES[i]);
Serial.println(" Hz");
tone(BUZZER_PIN, NOTES[i], 500);
delay(1000);
}
delay(1000);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Makes pin 12 an output, starts serial at 9600 baud and prints a hello line.
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: passive buzzer on pin 12");
}loop sketch.ino
A for loop walks the NOTES array. Each pass prints the note name and frequency, then calls tone(BUZZER_PIN, NOTES[i], 500) to play it for 500 ms. A 1000 ms delay leaves a rest before the next note.
void loop() {
for (int i = 0; i < 8; i++) {
Serial.print(NAMES[i]);
Serial.print(" ");
Serial.print(NOTES[i]);
Serial.println(" Hz");
tone(BUZZER_PIN, NOTES[i], 500);
delay(1000);
}
delay(1000);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- A steady 5 V gives no sound This project
A passive buzzer needs a changing signal. That is whattoneprovides. - No sound at all This project
Check the buzzer's pin 1 goes to pin 12 and pin 2 goes to GND. - 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.