Go Down

Topic: Adjusting PWM frequency on Leonardo (Read 2 times) previous topic - next topic


For example digital pin 10 shows (PCINT6/OC1B/OC4B/ADC13) PB6 I assume that "OC1B" tells me it is timer 1, or is it timer 4 (OC4B)??

Both. You can configure this pin to be altered by either timer 1 or timer 4.

My second question is how to work out what prescale factor to use and what settings are available.

The prescaler defines the PWM frequency while the OCRX(AB) register defines the pulse width. The value of the prescaler says how much clock cycles it needs for the timer to be increased by one. An 8bit timer counts from 0 to 255 and back again (in PWM mode), every time it matches the value in the OCRX(AB) register, it toggles the corresponding output pin (if it's configured so). This results in the simple formula for the PWM frequency:

clock speed / prescaler value / 2

in the example:

16'000'000 / 256 / 2 = 31250Hz (not kHz as in your comment)


Thanks for the info pylon, I understand this a lot better now :)

I have switched to using pin 9 so as not to affect the Arduino timekeeping. I can confirm the pins 9 and 10 on the Leonardo behave same as the Uno with regarding timer / pwm frequency settings, that is:

Pins 9 and 10: controlled by timer 1

Setting    Divisor    Frequency
0x01        1        31250
0x02        8        3906.25
0x03     64        488.28125
0x04     256        122.0703125
0x05        1024        30.517578125

TCCR1B = TCCR1B & 0b11111000 | <setting>;

Interestingly I have noticed that the speed of a fan will change when the PWM frequency is altered even although the duty cycle remains unchanged. Could to do with the characteristics of the fan or perhaps something to do with the MOSFET that I am driving it from??

Go Up