A 12V active buzzer is loud enough for an alarm, but it needs 12V and more current than a GPIO pin can supply, so the Arduino Mega can't drive it directly. You need something in between to switch that 12V on and off, and this tutorial shows two options: a relay, which is simple but clicks every time it switches, and a MOSFET module, which switches silently and fast enough for quick alarm beeps.
The full tutorial, with every detail and the line-by-line code explanation, is on our site: Arduino Mega - Buzzer
What you need
- Arduino MEGA
- DIYables MEGA Development Board
- USB 2.0 cable type A/B (for USB-A PC)
- USB 2.0 cable type C/B (for USB-C PC)
- Relay
- 12V Active Buzzer
- DC Power Jack
- Jumper Wires
- Screw Terminal Block Shield for Arduino Uno/Mega
- Sensors/Servo Expansion Shield for Arduino Mega
- Breadboard Shield for Arduino Mega
- Enclosure for Arduino Mega
- DIYables Sensor Kit (18 sensors/displays)
Wiring
The 12V active buzzer has two wires: black goes to the ground of the 12V power supply, red goes to +12V. Instead of wiring the buzzer straight to the supply, you put a relay between the buzzer and the 12V power, and the Arduino Mega controls that relay through RELAY_PIN (pin 3 in the code). The Arduino itself never carries the 12V current, it just tells the relay when to close the circuit. For the MOSFET version, the buzzer and MOSFET module connections replace the relay, and if your buzzer is a magnetic type with a coil inside, add a flyback diode (1N4007 or 1N5819) across it, stripe side to buzzer +, other side to buzzer −.
How it works
The buzzer makes noise whenever it gets 12V, so all the Arduino has to do is open and close that power path. A relay is an electromechanical switch, so you hear a click each time it toggles, which is fine for slow beeps. A MOSFET module is a solid-state switch with no moving parts, so it switches silently and much faster, which matters if you want many short alarm beeps in a row instead of one slow on/off cycle.
Code
The relay version turns the buzzer on for two seconds and off for five seconds, repeating forever, using digitalWrite() on RELAY_PIN. The MOSFET version is built for a real alarm pattern: 10 fast beeps, then one long beep, then three seconds of silence, then it repeats. In both cases the buzzer is only ever fully on or fully off, driven with digitalWrite(). Don't use analogWrite() to try to control volume, since an active buzzer generates its own tone internally and PWM just makes it sound weak or distorted.
arduino-mega-buzzer.ino
/*
* This Arduino Mega code was developed by newbiely.com
*
* This Arduino Mega code is made available for public use without any restriction
*
* For comprehensive instructions and wiring diagrams, please visit:
* https://newbiely.com/tutorials/arduino-mega/arduino-mega-buzzer
*/
#define RELAY_PIN 3 // The Arduino Mega pin that controls the buzzer via relay
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin D4 as an output.
pinMode(RELAY_PIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(RELAY_PIN, HIGH); // turn on buzzer 2 seconds
delay(2000);
digitalWrite(RELAY_PIN, LOW); // turn off buzzer 5 seconds
delay(5000);
}
Things that go wrong
If you want slow, occasional beeps, the relay is fine and the click isn't a problem. If you're after rapid alarm-style beeping, the relay's clicking and switching speed get in the way, so switch to the MOSFET module instead. If you're using a magnetic buzzer with a MOSFET, skip the flyback diode and you risk damaging the MOSFET from voltage spikes when the coil switches off. Keep the 12V adapter unplugged while wiring, and only plug it in after the code is uploaded.
Full tutorial
The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/arduino-mega/arduino-mega-buzzer

Top comments (0)