Pages: [1]   Go Down
Author Topic: tone()  (Read 407 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all.

Arduino documentation states that the tone() function generates a frequency in Hertz equal to the second parameter passed to the function.

But I tested in two boards: MEGA and MAGA2560, and concluded that this is not the case. For example: tone(21,0) generates a frequency of about 60Hz at pin 21.

More generally, tone(p,k) seems to generate a frequency of about k*60 Hz at pin p.

Any comments?

Thanks in advance.

-SR
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8821
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Tone seems to be intended for audio tone generation.  I would not expect it to work properly for frequencies outside the range of human hearing (roughly 16 Hz to 20 kHz).  It's not that the ATmega chips are not capable of very hight and very low frequencies.  It's just that sometimes that takes extra effort.


If you want to generate a 0 Hz output the proper command is:  digitalWrite(21, HIGH);   smiley
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you John.

In fact I don't want zero Hertz. I was just amazed by the fact that tone() didn't work as described. In fact, integer multiples of 60Hz are great.

Best regards,

-SR
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8821
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think the lowest possible frequency is 30.517578125 Hz  (16 MHz clock / 1024 prescaler / 256 counts of the 8-bit timer / 2 because it toggles).  I don't know what to expect for frequencies below that.

If you set up Timer1 (a 16-bit timer) the frequencies could go much lower.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, you are correct. The frequencies of tone() are multiples of 30.5 Hz, not 60 Hz as I stated before. Thanks for the explanation.

-SR
Logged

Pages: [1]   Go Up
Jump to: