Programming Exercises
Programming Exercises
PASSIVE BUZZER
A passive buzzer requires a DC signal to make a sound. It is like an electromagnetic speaker,
where a changing input signal produces the sound. You just need to connect the negative wire to
GND and the positive to any digital pin. You can recognize the positive pin by looking at the top
side of the buzzer, you will have a point marked "+",
In order to make it work, you just need to use the functions tone and notone. Let’s see how they
work, imagine you attach a buzzer to pin 7:
tone ( 7 , 2093);
It makes the buzzer in pin 7 sound with a frequency of 2093 Hz, so that it sounds like a
DO.
notone ( 7 );
The Frequency you can pass to a passive buzzer ranges from 31 to 4978. Here you have just an
example of one of the scales, you can use these frequencies or you can try some different ones.
mid C → 261
C# → 277
D → 294
D# → 311
E → 330
F → 349
F# → 370
G → 392
G# → 415
A → 440
Example of programming:
void setup(){
pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
}
void loop(){
tone(buzzer, 294); // Send 294 Hz sound signal…
delay(500); // ...for 0,5 sec
tone(buzzer, 415); // Send 415 Hz sound signal…
delay(1000); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(2000); // ...for 2 sec
}
Your turn:
Exercise 1:
Use some leds (no more than 6), a button and a passive buzzer to create a melody and a light
show. The melody must have sense and it has to last, at least, 10 seconds. The leds must turn
on and off following the rhythm.
Exercise 2:
The button must start the melody and when the melody finishes, it won’t start again until you
press the button again.