Let's Make Robots!

Arduino "Tone" command and the ATmega8 - it just won't work

Recently Frits pointed out that sound was used much with robots on this site. I agree and am trying to add sound to a miniature bi-ped robot I am designing. I am now using an ATmega8A processor set up as an Arduino and recently tried using the tone command with strange results. I only get a few frequencies. The digital oscilloscope shows that I am only getting 5 frequencies  which I guess change when the timers prescaler is changed. These frequencies are:

61Hz, 244Hz, 976Hz, 3.9KHz and 31.2KHz

The code was just a blank sketch with the tone command in the setup function. This works fine with when I use an ATmega168. I have searched the internet and tried using one of the older versions of the Tone library as well as the version built into Arduino 0018. Still no luck.

Has anyone had this problem before? If someone has an old Arduino with an ATmega8 could they test the tone command? According to all the information I found the tone library version 4 and later should work with the ATmega8.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Good and bad news. Together with bhagman we've found one bug in the Arduino Tone lib. The good news is: now the frequency output is correct for the ATMega8.

The bad news. There must be another bug. If you use the duration parameter with the tone command, iIt is not working. Only the first note of a melody is playing (for example in the toneMelody sketch). If you change the sketch using a delay command instead of the duration parameter, the sketch is working fine.


I thought i noticed the duration was not working, that is why i switched to the delay when testing.

Found the 2nd bug by myself. Here you can find a fixed tone.cpp file ro replace the existing lib in Arduino018. Copy it to folder hardware\arduino\cores\arduino\ and recompile your sketches. Now everything is working fine, I hope.

I tested the new library this morning on the oscilloscope. Works great now. Thanks to you and Brett for all your help.

PS. I haven't tested it on any of the other processors yet but from what I understood of the original code your changes would only affect the ATmega8?

Correct, it should work with all processors now.

Now that I know for sure that the problem is the "tone" command and not my homemade Arduino I can focus on fixing the tone command.

The FreqOut command will solve the problem of playing a simple tune but I also need to generate a 38KHz carrier wave for IR signals. This requires the hardware to produce the wave in the background while the software deals with the serial communications.

The tone command looked like a good candidate to solve this problem. Unfortunately for this project I am forced to use an ATmega8 to keep the price down so I will have to either fix the tone command or create a new function.

I have opend an new issue on the Arduino code repository. Maybe they will find a solution.

I was going to do the same and in the end I emailed the author of the code directly after posting a comment on his site. Hopefully he can fix it since he wrote it.

For now I have l continued to design the hardware under the asumption that eventually this issue will be resolved.

I did a little test

int x;


 void setup() {




 void loop() {


tone(8, x);







by watching x and listening to the pitch, sounds like it only changes at the different rates you have above (61hz, 244hz, etc.)

I have tested the toneMelody sample an a ATmega8 board. Only one tone is playing, the rest is silence. Seems to be an issue in the tone library for ATmega8. On a Arduino Duemilanove everything works.

The FreqOut function should work, because no timer were used. It is similar to the function I used for the Ardubot motor sound