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 Runs in your browser. Free, and no account needed.
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);
}
Parts list
3 parts, plus the jumper wires. Every one is in the editor's parts bin.
- 1 × Arduino Uno R3
- 1 × Half breadboard
- 1 × Buzzer
How it is wired
2 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Ground: Arduino Uno R3 pin GND; Buzzer pin 2
- Arduino Uno R3 pin 12; Buzzer pin 1
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.