2

according to the tone() documentation using tone() interferes with PWM output:

Use of the tone() function will interfere with PWM output on pins 3 and 11 (on boards other than the Mega).

On my Arduino Micro it seems to interfere with PWM on pin 5. After using tone() the PWM does not seem to work anymore. If I use pin 3,9 instead everything works fine.

So can someone explain why that is happening and if there is a way to circumvent that?

2 Answers 2

2

It's called a resource conflict. The Arduino only has limited resources, and some resources are shared between different tasks. You can use that resource for one task or the other but not both.

In this case the resource is a timer. You can use that timer to generate PWM or you can use it to drive the tone() system - but not both.

By default the timer is configured for PWM, but once you use tone() it re-assigns the timer to be used to trigger an interrupt at a specific frequency to toggle an IO pin.

4
  • ok thanks, but I don't want to use both simultaneously. Can I manually re-assign the timer back to the PWM?
    – D-rk
    Commented Nov 14, 2015 at 16:18
  • It may be doable, but there isn't (AFAIK) a single function to do it. You will have to undo whatever tone does and re-do whatever PWM does. Look at the source code to both the tone() command and the analogWrite() command and associated functions to see what they do.
    – Majenko
    Commented Nov 14, 2015 at 16:39
  • ok I understand. That's not worth the effort for me. I will just avoid pin 5 then.
    – D-rk
    Commented Nov 14, 2015 at 19:59
  • You could just extend disableTimer, to reset properly timer 3, like it does for timer2. Or force it to use timer 2 (here). Shouldn't be to hard, but might indeed not be worth the effort.
    – Gerben
    Commented Nov 14, 2015 at 20:16
3

The pin 3 and 11 only refers to boards based on the ATMega328. On these boards timer2 is used by tone, disabling PWM on pins 3 and 11. See Majenko answer about timers.

The Micro (ATMega32u4) tone uses timer 3, which is connected to pin 5 only.

So on the Micro, just don't use pin 5 for PWM when using tone.

Secondly don't trust everything on the arduino.cc website. There quite a bit of misinformation on there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.