
Video
Circuit Diagram

Source Code
1// ─────────────────────────────────────────
2// BlueGrays · bluegrays.com
3// Using a Buzzer
4// ─────────────────────────────────────────
5// The buzzer beeps at 400 Hz for 0.3 s, stays silent for 0.3 s and keeps
6// repeating. Shows how to use tone() and noTone().
7//
8// Pins:
9// D7 -> Buzzer + (other leg to GND)
10
11int frequency = 400; // Tone frequency (Hz)
12int buzzerPin = 7; // Buzzer output pin
13
14void setup() {
15 // tone() sets the pin as an output itself
16}
17
18void loop() {
19 tone(buzzerPin, frequency); // Start the 400 Hz tone
20 delay(300);
21 noTone(buzzerPin); // Stop the sound
22 delay(300);
23}
24FAQ
What is the difference between an active and a passive buzzer?▾
An active buzzer contains an oscillator; it produces a constant tone only when a voltage is applied. A passive buzzer requires an external frequency source; it can play different notes using the `tone()` function. Passive buzzers are required for melody projects.
How do you use the tone() function?▾
tone(pin, frequency) plays a sound at the specified frequency; noTone(pin) silences it. A duration can also be specified using the format tone(pin, frequency, duration). For example, 440 Hz is the note A.
The volume is very low. How can I turn it up?▾
The Arduino pin drives the buzzer with a limited current. For a louder sound, you can drive the buzzer through an NPN transistor (e.g., BC547) or choose a configuration that doesn't block the buzzer's hole.
Will using `tone()` cause problems on the other pins?▾
In Uno, the `tone()` function uses Timer2; therefore, PWM (analogWrite) does not work properly on pins 3 and 11 while it is playing. If you need PWM at the same time, select different pins.
