How do you increment the frequency of a piezo buzzer for a specific range ?

Hello,

I am Hannah ^^

I am still getting used to Arduino, so please bear with me. I am working on a project for class. I am struggling with developing the code that will increment the frequency of piezo buzzer for a specific the range(20-20,000 Hz). My goal is to allow the piezo buzzer to produces sounds of increasing frequency and that each frequency has a duration of 10 ms.

So far, I know how to do this:

int piezoBuzzer = 9;

void setup()
{
Serial.begin(9600);
pinMode(piezoBuzzer, OUTPUT);

}

voidlooop()
{
tone(piezoBuzzer, 2000, 1000)
}

I am still a bit in confused on the steps I would need to take to increment the frequency such that it is producing sounds with increasing frequencies and that it does not have instances of silence in between each frequency. I am briefly familar with writing ranges in for loops:

for (int i=20; i<20,000; i++)
{
do stuff
}

But, I am struggling with how I could implement this is the piezo buzzer. Please help me and let me know what I can do?

-Hannah

Put this line
tone(piezoBuzzer, 2000, 1000)

in here
for (int i=20; i<20,000; i++)
{
do stuff
}
and make i the variable that tone uses for the frequency.
Incrementing by 1 from 20 to 20,000 and holding each for 0.01s means it will take nearly 200 seconds for each ramp up in frequency. Is that what you wanted?
You might for instance might want to make it more musical. Make an array of the frequency of musical notes and step thru all of those.

for (long i=20; i<20000; i++) // can't use an int here as 20,000 is too big for an int
{
tone(piezoBuzzer, i, 10); //  10 ms duration
}

int covers -32787 to 32767, unsigned int 0 to 65535

It did not work because the frequency would go up down and up and down. I'm not sure what I am doing wrong. I put the same code.

CrossRoads:
Put this line
tone(piezoBuzzer, 2000, 1000)

in here
for (int i=20; i<20,000; i++)
{
do stuff
}
and make i the variable that tone uses for the frequency.
Incrementing by 1 from 20 to 20,000 and holding each for 0.01s means it will take nearly 200 seconds for each ramp up in frequency. Is that what you wanted?
You might for instance might want to make it more musical. Make an array of the frequency of musical notes and step thru all of those.
Table of Musical Notes and Their Frequencies and Wavelengths

Grumpy_Mike:

for (long i=20; i<20000; i++) // can't use an int here as 20,000 is too big for an int

{
tone(piezoBuzzer, i, 10); //  10 ms duration
}

Are you putting 20,000 or 20000 it is the latter you need to do.

It did not work because the frequency would go up down and up and down. I'm not sure what I am doing wrong. I put the same code.

Did it cover the full range? If so then yes it will go up and when it reaches the top tone the whole thing will repeat from the bottom again. The tone will not slide down but jump.

That is how you have written the code. If that is not what you want to happen then say what you do want.

Hello,

I am sorry for bringing this question up again but I really need some help! I want to increment the frequencies of the buzzer on my arduino from 20-20000. The video gives an example of how I would like it to sound. However, I am struggling so much with how to write the code for it. I tried it before with the for and while loops and the frequency just kept going up and down (not in chronological order from 20-20000. It was jumping in between) instead of just going up. Please help me with this? It's for a project and I'm really struggling.

Hi,
What are you using as the output device?
A buzzer will buzz if you apply 5V to it, does yours?
Can you post a picture of your "buzzer"?

You probably need a loud speaker to hear the correct output from the controller if you want to hear tones.


You will certainly get better fidelity.

Tom.... :slight_smile:

@hannahwho, please do not cross-post. Threads merged.

tone is asynchronous (runs in the background). A delay or state-machine is necessary. Something like this...

for (unsigned int i=31; i<20000; i++)
{
  tone( piezoBuzzer, i );
  delay( 10 );
}

Note: The minimum usable frequency is 31 Hz and the correct datatype is unsigned int.