How to Use Buzzer with Arduino: Beginner’s Guide
Introduction
Buzzers are simple yet powerful components that allow you to add sound to your Arduino projects. Whether you’re building an alarm system or a musical device, understanding how to control a buzzer is essential for any Arduino beginner. This article will explore what buzzers are, their types, and how to use them in your projects.
What is a Buzzer?
A buzzer is an electrical device that produces sound through vibration. It creates a buzzing noise when voltage is applied. Buzzers are commonly found in devices like alarms, timers, and electronic toys to provide auditory feedback or notifications.
Types of Buzzers
You’ll encounter two main types of buzzers when working with Arduino: Active Buzzers and Passive Buzzers.
Active buzzer
An active buzzer is simple to use because it contains a built-in oscillator. When powered with a DC signal, it automatically produces a tone without needing external control from Arduino. Active buzzers are commonly used for simple applications like alarms and beepers.
Passive Buzzer
A passive buzzer, on the other hand, requires an external signal to generate sound. It needs to be controlled by a square wave signal (like a PWM signal) from the Arduino, allowing you to play different tones and melodies. Passive buzzers are versatile and ideal for projects requiring more complex sound patterns.
How a Buzzer Works
A buzzer vibrates a diaphragm to create sound waves. When electricity flows through the buzzer, the diaphragm oscillates, producing vibrations that are heard as sound. The pitch of the sound can vary depending on the type of buzzer and the signal sent to it.
The sound is fixed for active buzzers, which turn on when voltage is applied. You can control the signal frequency for passive buzzers to create different tones or patterns.
Wiring a Buzzer to Arduino
Wiring an active or passive buzzer to your Arduino is straightforward. Below is the basic setup for connecting an active buzzer:
Wiring for an Active Buzzer
- Positive leg: Connect the positive leg of the buzzer to a digital pin on the Arduino (e.g. pin 9).
- Negative leg: Connect the negative leg to the GND pin on the Arduino.
This is all you need to get started with an active buzzer. The wiring for a passive buzzer is similar, but you’ll need to use the Arduino to control the signal being sent.
Code
Let’s now move on to writing some simple code for both an active and passive buzzer.
Controlling an Active Buzzer
Here’s a basic sketch to control an active buzzer by turning it on and off every second:
int buzzerPin = 9; // Pin connected to the buzzer
void setup() {
pinMode(buzzerPin, OUTPUT); // Set the buzzer pin as output
}
void loop() {
digitalWrite(buzzerPin, HIGH); // Turn on the buzzer
delay(1000); // Wait for 1 second
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
delay(1000); // Wait for 1 second
}
Explanation: In this code, the digitalWrite() function controls the buzzer, sending a HIGH signal to turn it on and a LOW signal to turn it off. The delay(1000) function creates a one-second interval between turning the buzzer on and off.
Playing Tones with a Passive Buzzer
To control a passive buzzer and generate different tones, we’ll use the tone() function, which allows you to control the frequency of the signal sent to the buzzer.
int buzzerPin = 9;
void setup() {
// No need for pinMode for tone
}
void loop() {
tone(buzzerPin, 1000); // Play a 1000 Hz tone
delay(1000); // Wait for 1 second
noTone(buzzerPin); // Stop the tone
delay(1000); // Wait for 1 second
}
Explanation:
- tone(buzzerPin, 1000): Sends a square wave of 1000 Hz to the buzzer, producing a sound with that frequency.
- noTone(buzzerPin): Stops the sound from playing.
Conclusion
Once you’ve mastered the buzzer basics, you can move on to more advanced projects. By following this guide, beginners will get a solid understanding of how to use buzzers with Arduino. Whether making an alert system or playing with tones, buzzers provide a fun and practical way to explore sound in electronics.