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.
Thanks in advance.
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); :)
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.
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.
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.