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
Everything below is what the build uses. The links go to the parts we make and test.
- Official Arduino Uno
- DIYables STEM V3, Fully Compatible with Arduino Uno R3
- 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
- Sensors/Servo Expansion Shield for Arduino Uno
- Breadboard Shield for Arduino Uno
- Enclosure for Arduino Uno
- Prototyping Base Plate & Breadboard Kit for Arduino UNO
- DIYables STEM V3 Starter Kit (Arduino included)
- DIYables Sensor Kit (18 sensors/displays)
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.
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