Using tone to generate sound

I have a Arduino Leonardo on com port 5 in windows XP . I can run various programs to blink LED's just fine. I am trying to use the tone command to output a tone to a speaker. I am using a simple Sketch to generate a 1000hz tone on pin 8 for 2 seconds : here is the Sketch:

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly: 
  tone(8,1000,2000);
}

When I try to upload to my Leonardo the TX/RX lights blink like it is uploading the code and it says it's finished but nothing is output to the speaker on pin 8. If I try to upload the code again the arduino will not do anything unless I hold down the reset button until the Uploading message appears. If I upload the BLINK sketch everything works fine.
Any Ideas or help with this?
Thanks

John

have you tried the example as in http://arduino.cc/en/Tutorial/Tone ?

PGTBOOS:
have you tried the example as in http://arduino.cc/en/Tutorial/Tone ?

Yes! That is the first program for sound that I tried. That is when I noticed that the system would "hang" or " lock-up" when I tried to upload it.
I would always have to hold down the Reset button until the uploading message displayed before anything else would upload after that.

Thank you

John

are you sure it hangs ??

try this in the main program loop blink a led with some delay.
if the program hangs you wont see it blinking,
if it does, it doesnt hang and it might just be that the speaker is the problem. (but i'm not that deep into electronics).
These days there are cheap small speakers, as used in mobile phones / head sets, maybe you can try another speaker.

johntech:
I am trying to use the tone command to output a tone to a speaker.

How is the speaker connected to the Arduino?

PGTBOOS:
are you sure it hangs ??

try this in the main program loop blink a led with some delay.
if the program hangs you wont see it blinking,
if it does, it doesnt hang and it might just be that the speaker is the problem. (but i'm not that deep into electronics).
These days there are cheap small speakers, as used in mobile phones / head sets, maybe you can try another speaker.

Here is the updated program so I could monitor what is going on:

void setup() {
  // put your setup code here, to run once:
  pinMode(13,OUTPUT);
  Serial.begin(9600);
  
}

void loop() {
  // put your main code here, to run repeatedly: 
  delay(5000); 
  Serial.println("Turn LED on");
  digitalWrite(13, HIGH);
  delay(1000); 

   tone(8,1000,2000);
   
   Serial.println("Turn LED off");
  digitalWrite(13, LOW);
  delay(1000); 
   
}

I have a 5 second delay so I can turn on the monitor before it runs. I get the message "Turn LED ON" and the pin 13 LED turns on and that is as far as the program goes. If I comment out the tone statement, the program runs fine!

PeterH:

johntech:
I am trying to use the tone command to output a tone to a speaker.

How is the speaker connected to the Arduino?

Peter
I have one term of the speaker to gnd and the other through a cap to pin 8 - all the documentation I have seen says put a 100 ohm resistor in series with the driving pin. I have yet to be able to use the tone command without locking up my Leonardo.

John

johntech:
I have one term of the speaker to gnd and the other through a cap to pin 8 - all the documentation I have seen says put a 100 ohm resistor in series with the driving pin. I have yet to be able to use the tone command without locking up my Leonardo.

Without the series resister, the capacitor will allow a very high initial current which may be more than the Arduino can supply. I'd have thought you'd overheat the pin driver rather than crash the Arduino, but who knows. I suggest you add the suggested series resister to limit the peak current, and see if that helps.

PeterH:

johntech:
I have one term of the speaker to gnd and the other through a cap to pin 8 - all the documentation I have seen says put a 100 ohm resistor in series with the driving pin. I have yet to be able to use the tone command without locking up my Leonardo.

Without the series resister, the capacitor will allow a very high initial current which may be more than the Arduino can supply. I'd have thought you'd overheat the pin driver rather than crash the Arduino, but who knows. I suggest you add the suggested series resister to limit the peak current, and see if that helps.

Pete There is a known problem with the tone command and the Leonardo. There is supposed to be a fix out there that I have already tried that still doesn't work.

The software crashes whether I have the speaker connected or not. I can write a loop shetch just to turn pin 8 on and off at different speeds and the speaker works fine with the capacitor.

John

johntech:
Pete There is a known problem with the tone command and the Leonardo. There is supposed to be a fix out there that I have already tried that still doesn't work.

The software crashes whether I have the speaker connected or not. I can write a loop shetch just to turn pin 8 on and off at different speeds and the speaker works fine with the capacitor.

John

Not a problem I'd heard about, but then I don't have a Leonardo and have never used the tone() function. From what you're saying I gather it is a known problem in the Arduino runtime library and nothing to do with what you have attached to the Arduino. To confirm that, do you still see the problem when Arduino has no external hardware at all connected to it other than the USB connection to the PC?

Hi guys,

I’ve amended the tone.cpp file for compatibility with ATmegaA32U4 boards (e.g. Leonardo) and Arduino IDE 1.00 onwards.

Just replace this in the library and it should work. It worked for my Leonardo! :slight_smile:

Kwok Leong

Tone.cpp (18.2 KB)

Write your own tone out, blink without delay style:

void loop(){
currentTime = micros();
if ( (currentTime -nextTime) >=tonehalfPeriod){ // time to transition high/low or low/high?
nextTime = nextTime + halftonePeriod; // save time for next transition
PINB = 0x08; // toggle PORTB bit 3 for example
}
//do other stuff while waiting
}
all time elements are unsigned long
Add another time check around this if you only want it to last a certain length, or to only run if a flag from something else is set.