Siren on a passive buzzer (pin 9), on an Arduino Uno
A passive buzzer on pin 9 sweeps up from 100 Hz to 1000 Hz and back down with tone(), over and over, like a siren. 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
- Playing a note with tone
- Frequency sets pitch
- Sweeping a value with a for loop
- Passive versus active buzzers
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 3
How it works
A passive buzzer has no sound maker of its own, so it plays whatever note the board drives it with. Pin 9 connects to the buzzer's pin 1, and its pin 2 goes to GND.
The sketch steps the frequency from 100 Hz up to 1000 Hz in 10 Hz steps, waiting 5 ms each step. Then it steps back down. The rise and fall sound like a siren.
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 9 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// A siren: a passive buzzer on pin 9, swept up and down with tone().
//
// pin 9 -> the buzzer's + leg; the other leg -> GND
//
// A passive buzzer has no oscillator of its own, so it makes whatever note
// the board drives it with. tone(pin, frequency) plays a square wave at that
// frequency until the next tone() or noTone(). Stepping the frequency up by
// 10 Hz every few milliseconds, then back down, gives the wail of a siren.
const int BUZZER = 9;
void setup() {
Serial.begin(9600);
Serial.println("Mokxi Uno: a siren on the passive buzzer on pin 9");
}
void loop() {
for (int freq = 100; freq <= 1000; freq += 10) {
tone(BUZZER, freq);
delay(5);
}
for (int freq = 1000; freq >= 100; freq -= 10) {
tone(BUZZER, freq);
delay(5);
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Starts the serial port at 9600 baud and prints a hello message. It does not set up the buzzer pin, because tone does that itself.
void setup() {
Serial.begin(9600);
Serial.println("Mokxi Uno: a siren on the passive buzzer on pin 9");
}loop sketch.ino
The first for loop raises freq from 100 to 1000 in steps of 10, calling tone(BUZZER, freq) and waiting 5 ms each time. The second loop counts back down. Together they make one up and down wail.
void loop() {
for (int freq = 100; freq <= 1000; freq += 10) {
tone(BUZZER, freq);
delay(5);
}
for (int freq = 1000; freq >= 100; freq -= 10) {
tone(BUZZER, freq);
delay(5);
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- No sound. This project
Pin 9 goes to the buzzer's pin 1 and GND goes to its pin 2. Check both are connected. - The siren is too fast or too slow. This project
Change thedelay(5)in each loop. A bigger number makes the sweep slower. - 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.