Active buzzer on an Arduino Uno
An active buzzer on pin 12 sounds whenever the pin is HIGH, so the sketch only decides when: quick beeps, then slow ones. 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.
Build it as the Sound and Lights Kit: the parts in numbered bags and a booklet that checks every step. Retails for about $42, free on Mokxi.
What you'll learn
- Active versus passive buzzers
- Switching a part on and off with
digitalWrite - Writing your own helper function with parameters
- Rhythm from on and off times
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 3
How it works
An active buzzer has its own tone generator inside. Give it 5 V and it sounds. Take the 5 V away and it stops. Here the buzzer's pin 1 goes to pin 12 and pin 2 goes to ground, so the sketch only decides when pin 12 is HIGH.
The sketch plays eight quick beeps, 60 ms on and 60 ms off. Then it plays four slow ones, 250 ms on and 250 ms off. It rests half a second and repeats. It prints which pattern is coming to the serial monitor.
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 × BuzzerIn Starter Kit, Rocket Flight Computer Kit, Parking Assistant Kit.
Kits that can build it
These 9 starter kits have every part in the circuit. Each kit's page lists what else it can build.
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 |
|---|---|
| Buzzer pin 2 | Arduino Uno R3 pin GND |
| Buzzer 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.
// Active buzzer: a buzzer with its own oscillator, switched on and off.
//
// pin 12 the buzzer's long leg (+), the short leg to GND
//
// An active buzzer has the tone generator built in. Give it 5 V and it sounds;
// take the 5 V away and it stops. So the sketch never makes a tone itself: it
// only decides when the buzzer is on. Here that is a run of quick beeps, then
// a run of slower ones, over and over.
//
// The other buzzer in most kits is passive: it has no oscillator, stays
// silent on a steady 5 V, and needs tone() to make the sound wave for it.
const int BUZZER = 12;
void beeps(int count, int onMs, int offMs) {
for (int i = 0; i < count; i++) {
digitalWrite(BUZZER, HIGH);
delay(onMs);
digitalWrite(BUZZER, LOW);
delay(offMs);
}
}
void setup() {
pinMode(BUZZER, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: active buzzer");
}
void loop() {
Serial.println("quick beeps");
beeps(8, 60, 60);
Serial.println("slow beeps");
beeps(4, 250, 250);
delay(500);
}
Code walkthrough
The key functions of the sketch, and what each one does.
beeps sketch.ino
A helper that sounds the buzzer count times. Each time it sets BUZZER HIGH for onMs, then LOW for offMs. Passing different numbers gives quick or slow beeps without repeating code.
void beeps(int count, int onMs, int offMs) {
for (int i = 0; i < count; i++) {
digitalWrite(BUZZER, HIGH);
delay(onMs);
digitalWrite(BUZZER, LOW);
delay(offMs);
}
}setup sketch.ino
Makes pin 12 an output and starts serial at 9600 baud, then prints a hello line.
void setup() {
pinMode(BUZZER, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: active buzzer");
}loop sketch.ino
Prints the pattern name, then calls beeps(8, 60, 60) for quick beeps and beeps(4, 250, 250) for slow ones. A half-second delay separates the rounds.
void loop() {
Serial.println("quick beeps");
beeps(8, 60, 60);
Serial.println("slow beeps");
beeps(4, 250, 250);
delay(500);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- No sound This project
Check the buzzer's direction: pin 1 to pin 12 and pin 2 to GND. An active buzzer has a long leg (+) and a short leg. - You swapped in a passive buzzer and it stays silent This project
A passive buzzer needstone()to make the sound. This sketch only sets the pin HIGH. - The buzzer makes a constant tone, or you cannot change its pitch. Buzzer
An active buzzer has its own oscillator, so it only beeps on and off. A passive buzzer needs tone() to set pitch. Check which type you have. - The buzzer is silent or very quiet. Buzzer
Check polarity: the plus mark goes to the pin or supply. For loud buzzers, drive them through a transistor instead of straight from a pin. - 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.