Arduino Mega - Buzzer

This guide teaches you how to use the Arduino Mega to make a 12V active buzzer loud. If you need to work with a 5V active or passive buzzer, read our Arduino Mega Piezo Buzzer tutorial.

Hardware Preparation

1×Arduino Mega
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×Relay
1×12V Active Buzzer
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of 12V Active Buzzer

The 12V active buzzer makes a loud sound, good for alarm systems.

Pinout

Arduino Mega 12V Active Buzzer Pinout

A 12-volt active buzzer usually has two pins.

  • Connect the black wire to the power supply's ground (GND).
  • Connect the red wire to the power supply's +12V output.

How to Control 12V Active Buzzer

When a 12V active buzzer gets power from a 12V supply, it makes a sound. To run a 12V active buzzer with an Arduino Mega, put a relay between them. The Arduino Mega can turn the buzzer on and off through this relay. If you don't know much about relays (pin layout, how they work, or how to program them), check the Arduino Mega relay tutorial.

Wiring Diagram

The wiring diagram between Arduino Mega 12V Active Buzzer

This image is created using Fritzing. Click to enlarge image

Arduino Mega Code

The code makes the 12V buzzer stay on for two seconds, then off for five seconds, and repeats.

/* * 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); }

Detailed Instructions

Follow these steps one by one.

  • Connect the parts as shown in the diagram.
  • Connect the Arduino Mega board to your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • Choose the correct board (Arduino Mega) and the COM port.
  • Paste the code into the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to your Arduino Mega.
  • Listen to the sound from the 12V active buzzer.

Code Explanation

The explanation is in the comments in the Arduino code shown above.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!