IAN LANG ELECTRONICS

CIRC-06

Go Back

I'm in somewhat of a difficult situtation here because whilst I have very little expertise in making piezo buzzers make aesthetically pleasing noises I can't help but feel when I look at the supplied code that whoever wrote it is doing something like travelling from Dover to London and going via Scunthorpe. Nevertheless, let's build the circuit and run the code.

Copy & Paste Code from Here:

A very easy construct this one but placing the piezo buzzer can be tricky as the shape precludes seeing where the pins are going. I left it tilting with the tips of the pins just inside of the holes in the breadboard and put the wires in, then just slid the buzzer in alongside. Worked first go.

 

When you fire this up with the supplied code it plays Twinkle Twinkle little star at you, and you can change it to happy birthday. Difficult to figure out how the code is working though; there's so much passing of various parameters around functions I haven't got a scooby's where anything is at any given time. So, with that in mind, here's some more code for you to try:

//musical scale///

//Ian Lang////////

 

int (f[]) ={261,294,329,349,392,440,493,523}; //sets frequencies

int (t)=500; //sets start speed

 

 

void setup () {

 

  tone (9,523,1000);//beeps

  delay (2000);// wait

 

}

 

void loop (){

  for (int j=0; j<8;j=j+1){//loop for scale upwards

    tone (9,f[j],t*0.75); // tone (PIN,FREQUENCY,DURATION)

delay (t);} //wait until duration period has passed

  for (int j=6; j>0;j=j-1){//loop for scale downwards

    tone (9,f[j],t*0.75); //tone (PIN,FREQUENCY,DURATION)

delay (t);} //wait until duration period has passed

t=t-100; //decrease the time interval between notes

if (t<100) {//if the interval's too small

  t=500;}//reset it

}

 

 

Now assuming all's gone well, what you should hear is a beep of about one second, and then the buzzer will play a rising and falling musical scale faster and faster, until it gets very fast and goes back to the original speed.

This is because every time this line is encountered:

 

    tone (9,f [ j ],t*0.75); // tone (PIN,FREQUENCY,DURATION)

 

the ATmega chip on your Arduino board sends an oscillation to pin 9 at the frequency stipulated at whatever position in the f array the cycle is currently at. The position is determined by the argument j, which is the counter in the for/next loop. For a rising scale, we have a loop going 0 to 7, and for the falling scale 6 to 1 (as we don't want the doh at the top and bottom of the scale twice).

 

Alrighty then using this method how can we make it do a tune? Like this:

 

//Mozart///

//Ian Lang////////

int (nl)=19;// how many notes to play

int (f[]) ={392,294,392,294,392,294,392,492,588,0,522,440,522,440,522,440,370,440,294}; //sets frequencies

int (d[]) ={2,1,2,1,1,1,1,1,2,2,2,1,2,1,1,1,1,1,2}; //sets rhythm

void setup () {

 

}

 

void loop (){

  for (int j=0; j<nl;j=j+1){//loop for nl value

    tone (9,f[ j] ,300*d[ j] ); // tone (PIN,FREQUENCY,DURATION)

delay (300*d [j]);} //wait until duration period has passed

  delay (2000);

}

 

Fire your Arduino board up with this code and you should hear something quite familiar.

It works due to these three variables being set up:

 

int (nl)=19;// how many notes to play

int (f[ ]) ={392,294,392,294,392,294,392,492,588,0,522,440,522,440,522,440,370,440,294}; //sets frequencies

int (d[ ]) ={2,1,2,1,1,1,1,1,2,2,2,1,2,1,1,1,1,1,2}; //sets rhythm

f is the frequency you want the pin to oscillate at. 440Hz is A. Here's a pitch to frequency calculator:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

If you follow a musical score exactly, beware. This is wriiten in G Major, and so there's an F# to consider, and since you ask, no I didn't at first. Twenty minutes scratching my head about that before I realised.

d is the duration of the note. I worked as 1 for a quaver, two for a crotchet, 3 for a minim and four for a semibreve. Multiply by 1.5 if there's a dot.  Then in the for next loop just multiply the duration of the note and the delay to allow it to pass by whatever tempo you want. If you change them both to 100 in the above code, you get very fast Mozart.

 

Piezo buzzers are more usually used for making noises than they are music as they are not the most high fidelity transducer. Here's a bleeper:

void setup(){}

void loop (){

  for (int t=50;t<50;t++);{  

  tone (9,1568,50);delay(100);}

 

}

 

here's a two tone bleeper:

void setup(){}

void loop (){

 

  tone (9,1568,50);delay(500);

  tone (9,784,50);delay(500);

}

 

 

and finally an alert siren:

 

void setup () {

 

}

 

void loop (){

 

  for (int j=200; j<400;j=j+5){//loop for nl value

    tone (9,j,30); // tone (PIN,FREQUENCY,DURATION)

delay (30);} //wait until duration period has passed

 

}

 

 

And so on to the next lesson, which we have already touched upon, pushbuttons.

 

 

 

 

 

 

 

 

 

 

 

CIRC-07