0% found this document useful (0 votes)
18 views

Programming Exercises

This document discusses using a passive buzzer with an Arduino. It can be made to sound by connecting the positive wire to a digital pin and grounding the negative wire. The tone() and noTone() functions are used to generate tones of different frequencies, ranging from 31Hz to 4978Hz. Example code is provided to play tones for set durations using delay to create a simple melody that turns on LEDs in time with the music. Exercises are given to design a longer melody over 10 seconds using multiple LEDs that can be started and restarted with a button press.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Programming Exercises

This document discusses using a passive buzzer with an Arduino. It can be made to sound by connecting the positive wire to a digital pin and grounding the negative wire. The tone() and noTone() functions are used to generate tones of different frequencies, ranging from 31Hz to 4978Hz. Example code is provided to play tones for set durations using delay to create a simple melody that turns on LEDs in time with the music. Exercises are given to design a longer melody over 10 seconds using multiple LEDs that can be started and restarted with a button press.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

EXERCISE 1.

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 );

It stops the buzzer in pin 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:

int buzzer = 9; //buzzer to arduino pin 9

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.

You might also like