Project 3 Passive Buzzer
Project 3 Passive Buzzer
There are prolific interactive works completed by Arduino. The most common
one is sound and light display. We always use LED to make experiments. For
this lesson, we design circuit to emit sound. The universal sound components
are buzzer and horns. Buzzer is easier to use. And buzzer includes about
active buzzer and passive buzzer. In this experiment, we adopt passive
buzzer. While using passive buzzer, we can control different sound by
inputting square waves with distinct frequency. During the experiment, we
control code to make buzzer sound, begin with “tick, tick” sound, then make
passive buzzer emit “do re mi fa so la si do”, and play specific songs.
Material:
Connection:
VCC --- 5V
I/O --- D3
GND --- GND
Test Code:
int tonepin = 3; // Set the Pin of the buzzer to the digital D3
void setup ()
{
pinMode (tonepin, OUTPUT); // Set the digital IO pin mode to
output
}
void loop ()
{
unsigned char i, j;
while (1)
{
for (i = 0; i <80; i ++) // output a frequency sound
{
digitalWrite (tonepin, HIGH); // Sound
delay (1); // Delay 1ms
digitalWrite (tonepin, LOW); // No sound
delay (1); // Delay 1ms
}
for (i = 0; i <100; i ++) // output sound of another frequency
{
digitalWrite (tonepin, HIGH); // Sound
delay (2); // delay 2ms
digitalWrite (tonepin, LOW); // No sound
delay (2); // delay 2ms
}}}
Test Result:
From the above code, 80 and 100 decide frequency in “for” statement. Delay
controls duration, like the beat in music.
We will play fabulous music if we control frequency and beats well, so let’s
figure out the frequency of tones. As shown below:
After knowing the frequency of tone, next to control the time the note plays.
The music will be produces when every note plays a certain amount of time.
The note rhythm is divided into one beat, half beat, 1/4 beat, 1/8 beat, we
stipulate the time for a note to be 1, half beat is 0.5, 1/4 beat is 0.25, 1/8 beat
is 0.125....., Therefore, the music is played. We will take example of “Ode to
joy”
From notation, the music is 4/4 beat.
There are special notes we need to explain:
1.Normal note, like the first note 3, correspond to 350(frequency), occupy 1
beat
2.The note with underline means 0.5 beat
3.The note with dot(3.)means that 0.5 beat is added, that is 1+0.5 beat
4.The note with”—” represents that 1 beat is added, that is 1+1 beat.
5.The two successive notes with arc imply legato, you could slightly modify
the frequency of the note behind legato(need to debug it yourself), such like
reducing or increasing some values, the sound will be more smoother.
#define NTD0 -1
#define NTD1 294
#define NTD2 330
#define NTD3 350
#define NTD4 393
#define NTD5 441
#define NTD6 495
#define NTD7 556