Index
Introductio
An active buzzer module is a simple sound-producing component that’s easy to integrate with Arduino. Unlike a passive buzzer, it generates sound when voltage is applied, making it ideal for alerts and alarms. This module typically has 3 pins (VCC, GND, and Signal), allowing for direct control of sound frequency and duration through Arduino. In this tutorial, we’ll explore how to program Arduino to play different tones on the buzzer using the tone()
function.
Required Components
- Arduino UNO, Nano
- Buzzer module (passive)
- Jumper wires
- Breadboard (optional)
Pinout
Circuit Diagram / Wiring
Connect the buzzer module to the Arduino as follows:
- BUZZER VCC → 5V (Arduino)
- BUZZER GND → GND (Arduino)
- BUZZER SIGNAL → Pin D9 (Arduino)
Arduino Code / Programming
The following code demonstrates how to generate different tones using the tone()
function with a passive buzzer.
int buzzerPin = 9; // Pin connected to the buzzer
void setup() {
// Nothing is required here
}
void loop() {
// Play a tone at 1000 Hz for 500 milliseconds
tone(buzzerPin, 1000, 500);
delay(1000); // Wait for 1 second before playing the next tone
// Play a tone at 1500 Hz for 500 milliseconds
tone(buzzerPin, 1500, 500);
delay(1000); // Wait for 1 second before playing the next tone
// Stop the tone for 1 second
noTone(buzzerPin);
delay(1000);
}
Explanation of the Code
- Pin Definitions: The pin connected to the buzzer is defined.
- tone() Function: This function generates a square wave of a specified frequency on the buzzer pin, allowing for different sounds or tones. It takes three parameters: the pin number, the frequency in hertz, and the duration in milliseconds.
- noTone() Function: Stops any tone being played on the specified pin.
- Delays: Used to add pauses between tones for distinct sound patterns.
Testing and Troubleshooting
- Verify Connections: Make sure the buzzer is connected correctly to the Arduino.
- Monitor Output: The buzzer should produce sound as specified in the code.
- Adjust Tone Frequency and Duration: Experiment with different values in the
tone()
function to produce various sounds.