buzzer with an Arduino Uno
Wiring the Buzzer
1. Active Buzzer: An active buzzer has a built-in oscillator, so it can generate sound
when power is applied.
o Connect the positive terminal (long pin) of the active buzzer to digital pin 8
on the Arduino.
o Connect the negative terminal (short pin) to the GND (ground) pin on the
Arduino.
2. Passive Buzzer: A passive buzzer requires a PWM signal to generate sound.
o Connect one pin to digital pin 9 on the Arduino.
o Connect the other pin to GND.
Code for Active Buzzer:
// Define pin for the buzzer
const int buzzerPin = 8; // Connect to the active buzzer
void setup() {
pinMode(buzzerPin, OUTPUT); // Set the buzzer pin as an output
}
void loop() {
digitalWrite(buzzerPin, HIGH); // Turn the buzzer on
delay(1000); // Wait for 1 second
digitalWrite(buzzerPin, LOW); // Turn the buzzer off
delay(1000); // Wait for 1 second
}
Code for Passive Buzzer:
// Define pin for the buzzer
const int buzzerPin = 9; // Connect to the passive buzzer
void setup() {
pinMode(buzzerPin, OUTPUT); // Set the buzzer pin as an output
}
void loop() {
// Play a tone
tone(buzzerPin, 1000); // Play 1kHz tone
delay(1000); // Wait for 1 second
noTone(buzzerPin); // Stop the tone
delay(1000); // Wait for 1 second
}
Uploading the Code
1. Open the Arduino IDE on your computer.
2. Copy and paste the desired code (for active or passive buzzer) into the IDE.
3. Select the correct board and port from the Tools menu.
4. Click the Upload button (right arrow icon) to upload the code to the Arduino.
Testing
Once the code is uploaded, the buzzer should start sounding according to the code you
uploaded:
o The active buzzer will beep on and off every second.
o The passive buzzer will play a tone for one second, then pause for one second.