Arduino - Buzzer

by DIYables in Circuits > Arduino

4 Views, 0 Favorites, 0 Comments

Arduino - Buzzer

This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://arduinogetstarted.com/tutorials/arduino-buzzer

This build shows how to control a 12V active buzzer with Arduino to produce a loud sound, useful for an alarm system. Since Arduino's digital pins cannot drive a 12V buzzer directly, a relay sits between the Arduino and the buzzer to switch the 12V power on and off under program control.

Supplies

Wiring

The 12V active buzzer has two pins: a black negative pin that goes to GND of the 12V DC power supply, and a red positive pin that goes to the 12V supply through the relay. The relay is wired between the Arduino and the buzzer so the Arduino's D4 pin switches the relay, and the relay switches the 12V power to the buzzer. If the project runs for a long time or sits inside an enclosure, use a Screw Shield or Proto Screw Terminal Block Shield so the wires are fixed with screws instead of loose jumper connections.

How It Works

The buzzer makes sound whenever it receives 12V from the power supply. Arduino cannot switch 12V directly, so it toggles the relay instead, and the relay connects or disconnects the 12V supply to the buzzer. Setting the relay control pin HIGH turns the buzzer on, and setting it LOW turns it off.

Code

The sketch sets pin D4 as an output in setup, since that pin drives the relay. In loop, it writes D4 HIGH to turn the buzzer on for 2 seconds, then writes it LOW to turn the buzzer off for 5 seconds, repeating this cycle forever.

arduino-buzzer.ino - the sketch from the tutorial, unchanged.

/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-buzzer
*/

#define RELAY_PIN 4 // the Arduino 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 nothing happens, check that the buzzer's black wire goes to GND and the red wire goes to the 12V side through the relay, since reversed polarity will stop it working. Also confirm the relay is wired between Arduino D4 and the buzzer's power path, since the Arduino pin alone cannot power a 12V buzzer directly.

Full Tutorial

The complete tutorial, wiring diagram and code explanation: https://arduinogetstarted.com/tutorials/arduino-buzzer