Question about tone function and pwm

Hi everyone,
Can anyone tell me why of this:

"Use of the tone() function will interfere with PWM output on pins 3 and 11."

i can't find it.

(deleted)

i can't found that in datasheet :frowning:

FelipeFarias:
i can't found that in datasheet

What data sheet?

The one for the Arduino Uno tells you all about timers. You must read that in conjunction with the source code for the analogWrite function call.

Anyway why do you want to see it in a data sheet, do you not believe us?

In the datasheet of Atmega328P, i can't found where say that have the same timer (D3 and D11).
(Is just for learn to read the datasheet)
I believe in you <3

OK look at Timer 1 and Timer 2 in the data sheet. Now look at what port bits the PWM can be produced on.
For example for Timer 1

The OC0B pin is also the output pin for the PWM mode timer function.

The OC0B pin is Port D, Bit 5

Then cross reference that to what the Arduino calls those bits on that port. Like here
https://www.arduino.cc/en/Hacking/PinMapping168 You will see Port D, Bit 5 is PD5 which is called Pin 5.

From Arduino Timer and Interrupt Tutorial - Oscar Liang

Timer0:
Timer0 is a 8bit timer.
In the Arduino world Timer0 is been used for the timer functions, like delay(), millis() and micros(). If you change Timer0 registers, this may influence the Arduino timer function. So you should know what you are doing.

Timer1:
Timer1 is a 16bit timer.
In the Arduino world the Servo library uses Timer1 on Arduino Uno.

Timer2:
Timer2 is a 8bit timer like Timer0.
In the Arduino work the tone() function uses Timer2.

Now you will see the tone function uses Timer 2, so see what two pins Timer 2 can use to generate the PWM signals on. It is these two pins you can't use as a PWM output when you are using the tone libiary because they use the same timer and you can only configure a timer for one function at a time.

Ok... i see this image for Arduino Nano.

Here:
D3 -> PD3 -> INT1/OC2B/PCINT19
D11 ->PB4 -> MISO/PCINT4

That is why i was confused.

Thank you :smiley:

Yes, the Arduino abstracts the hardware so as to hide its structure from you. In this way different processors can appere to have identical pinout as the software maps the different port bits to a consecutive list of "pin" numbers.
This is so the same software will run on many processors. Personally I prefer a port or byte based output system normally rather than a bit based one.

I had a similar question, and that is would Tone interfere also with AC phase control. The AC phase control code that I leveraged, uses Timer 1.
However, it looks like Tone and PWM use Timer 2. Timers-Arduino
So maybe not.

And, any of these functions could be modified to use separate timers assuming 8/16 bits can be used, and not have the interference?

It depends on what the two functions are doing. If one works purely in the hardware like a PWM generator then no problem. However if both require triggering an ISR ( interrupt service routine ) then if they both triggered at the same time there might be a small delay in running one of the ISRs. This may or may not be a problem depending on what they are and how critical the timing is. Note that Timer 0 is going all the time ( unless you disable it ) to keep the millis and micros clocks going. Also any serial communications also use ISRs and take some time as well.