Increase PWM frequency


I'd like to increase the PWM frequency on pin 5 of my Arduino Micro.
I added the following line in the setup():

TCCR0B = TCCR0B & B11111000 | B00000001;

I check with an oscilloscope the output, and it remains at ~490Hz.

Would you know why?


You copied that line from a code for the Atmega328 of the Arduino UNO, isn't it? :wink:

The Arduino Micro has an Atmega32U4 on it and the micro is different. And different is the pinout too.
If you look at the schematic of the board, you'll notice the pin D5 is connected to pin OCR3A, so you should change the settings of timer 3, while that line of code changes the timer 0.
Moreover, the timer 0 on the Atmega328 is an 8-bits timer while the timer 3 on the Atmega32U4 should be a 16-bits timer. You should check the datasheet and look for the proper registers to manipulate.

On an Uno changing the clock prescaler for timer0 will break millis(), micros() and
delay(), note, since timer0 is used for these.

PWM pins 5 and 6 are connected to timer0 on the ATmega328.

On the ATmega32U4 the timer to pin mapping is different, but timer0 is still
used for millis() etc I believe. In general that means you try to leave timer0 alone
if possible. From my notes the 32U4 has timer0 connected to pin 3

@Anduriel, do not cross-post…

Thread locked.