Passive Buzzer¶
Overview¶
In this project, use these two functions to make the passive buzzer vibrate and produce sound. The function tone()
generates a square wave with a specified frequency (and 50% duty cycle) on a pin. A duration can be specified, or the wave continues until noTone()
is called.
Similar to the active buzzer, the passive buzzer also utilizes electromagnetic induction to operate.
The difference is that a passive buzzer does not have its own oscillating source, so it will not emit sound if DC signals are used.However, this allows the passive buzzer to adjust its own oscillation frequency and produce different notes such as “do, re, mi, fa, sol, la, ti”.
Required Components¶
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Elite Explorer Kit |
300+ |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
---|---|
- |
|
Wiring¶
Note
When connecting the buzzer, make sure to check its pins. The longer pin is the anode and the shorter one is the cathode. It’s important not to mix them up, as doing so will prevent the buzzer from producing any sound.
Schematic Diagram¶
Code¶
Note
You can open the file
16-passive_buzzer.ino
under the path ofelite-explorer-kit-main\basic_project\16-passive_buzzer
directly.Or copy this code into Arduino IDE.
At the time when you finish uploading the codes to the R4 board, you can hear a melody containing seven notes.
Code Analysis¶
Including the pitches library: This library provides the frequency values for various musical notes, allowing you to use musical notation in your code.
Note
Please place the
pitches.h
file in the same directory as the code to ensure proper functioning. pitches.h#include "pitches.h"
Defining constants and arrays:
buzzerPin
is the digital pin on the Arduino where the buzzer is connected.melody[]
is an array that stores the sequence of notes to be played.noteDurations[]
is an array that stores the duration of each note in the melody.
const int buzzerPin = 8; int melody[] = { NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4 }; int noteDurations[] = { 4, 8, 8, 4, 4, 4, 4, 4 };
Playing the melody:
The
for
loop iterates over each note in the melody.The
tone()
function plays a note on the buzzer for a specific duration.A delay is added between notes to distinguish them.
The
noTone()
function stops the sound.
void setup() { for (int thisNote = 0; thisNote < 8; thisNote++) { int noteDuration = 1000 / noteDurations[thisNote]; tone(buzzerPin, melody[thisNote], noteDuration); int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); noTone(buzzerPin); } }
Empty loop function: Since the melody is played only once in the setup, there’s no code in the loop function.
Feel free to experiment with altering the notes and durations in the
melody[]
andnoteDurations[]
arrays to create your own melodies. If you’re interested, there is a GitHub repository (robsoncouto/arduino-songs ) that offers Arduino code for playing various songs. While their approach may differ from this project, you can consult their notes and durations for reference.