# Varying the pwm frequency for timer 0 or timer 2?

Hi, I'm trying to use the Diecimila Arduino board to pulse solenoid valves, at a frequency of 125Hz. I can get pins 9 and 10 to work fine using the following:

TCCR1A=0x00; TCCR1B=0x12; ICR1=0x1F40;

However I have no idea how to change the pwm frequency at pins 3, 5, 6 and 11. I understand these don't use timer 1, but timer0 and timer 2 instead, and that these don't have the ICR1 input capture unit, which I believe the above code was using to vary the pwm frequency. So is it possible to change the pwm frequncy of the other timers? And if so how?

Thanks in advance, and any help will be much appreciated!

I brought up this subject as a feature request a while back. A few suggestions were made on how to do this. Not sure if it has what you need. But it may help:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1234764073

Thanks for the reply - I had actually already found that thread, and like you found the document on controlling timers far beyond my programming ability!

If someone has achieved this (low frequency pwm on a tleast 4 channels) I'd be very grateful if they could let me know how!

Cheers,

bump...

How to adjust Arduino PWM frequencies

Pins 5 and 6:

Setting Divisor Frequency
0x01 1 62500
0x02 8 7812.5
0x03 64 976.5625
0x04 256 244.140625
0x05 1024 61.03515625
``````TCCR0B = TCCR0B & 0b11111000 | <setting>;
``````

Pins 9 and 10:

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>;
``````

Pins 11 and 3:

Setting Divisor Frequency
0x01 1 31250
0x02 8 3906.25
0x03 32 976.5625
0x04 64 488.28125
0x05 128 244.140625
0x06 256 122.0703125
0x07 1024 30.517578125
``````TCCR2B = TCCR2B & 0b11111000 | <setting>;
``````

All frequencies are in Hz and assume a 16000000 Hz system clock.

macegr, that deserves to be a sticky posting in the FAQ section, and in the reference pages under advanced topics.

It was wrong, I fixed it all. The last two sections are divided by two for the Arduino. I also left in the decimals because they might factor into someone's results.

I was figuring out how to smoothly dim/fade some neon sign segments and they needed something close to 100Hz, so I had calculated most of the data and checked it with an oscilloscope already.

Macegr, you are THE MAN! Thank you so much!

So some observations/questions:

The PWM range increased from 0-255 to 0-1023 in the cases I tested. Is this true for all values?

delay() and millis() are no longer behaving as expected after changing the TCCR0B settings. Do you know of any other side effects of changing these timers?

I don't know of other side effects...the ones you mentioned were enough make me avoid changing the TCCR0B register.

Pins 5 and 6:
Setting Divisor Frequency
0x03 64 976.5625
TCCR0B = TCCR0B & 0b11111000 | ;

Pins 11 and 3:
Setting Divisor Frequency
0x04 64 488.28125
TCCR2B = TCCR2B & 0b11111000 | ;

Does timer2 really run half the frequency of timer0 with the same prescaler? I coudn’t find anything obvious to account for such a discrepancy in the AVR datasheet, although it matches observations. What am I missing?

thanks a lot! I'll try this out today. I'm pulsing some solenoid valves to control a pair of air muscles, and they just arrived!

westfw, start with the first section on page 136 of the atmega168 datasheet. I am not able to fully make sense of it all, but it looks like timers 0 and 1 source from different clock inputs (one 8bit vs one 16bit), so even with the shared prescaler logic and settings, they have different frequency outputs; timer 2 is more independent. I wonder what the design considerations were, but I think they were going for maximum variation between the three, while sharing a lot of logic.

macegr: I’m afraid that the frequencies in your post for pins 3, 9, 10 and 11 are a little of. The correct formula is f = clock/(510 N), where N is the divisor (prescaler) and clock = 16,000,000. That gives f = 490.196 Hz for the default setting. But close enough. See the ATmega168 datasheet.

westfw: The reason is that Timer0 runs in the FastPWM mode to feed the millis() function. That gives a frequency f = clock / (256 N), N = 64 by default. In FastPWM, the counter is always incrementing until it reaches 256 when it overflows to 0. The frequency f = 976.563 that is close enough to generate milliseconds.
Timers 1 and 2 run in the Phase correct PWM, which is apperently better for motors. Then the counter increments until it reaches the top 255 and then starts decrementing until it reaches 0. And then again incrementing… Thus for each cycle it has to go through 255 + 255 = 510 steps, hence the frequency clock/ (510 N), N = 64. That gives you ~~ 490Hz.

If you change TCCR0B, it affects millis() and delay(). They will count time faster or slower than normal if you change the TCCR0B settings. Below is the adjustment factor to maintain consistent behavior of these functions:

Default: delay(1000) or 1000 millis() ~ 1 second

0x01: delay(64000) or 64000 millis() ~ 1 second 0x02: delay(8000) or 8000 millis() ~ 1 second 0x03: is the default 0x04: delay(250) or 250 millis() ~ 1 second 0x05: delay(62) or 62 millis() ~ 1 second (Or 63 if you need to round up. The number is actually 62.5)

Also, the default settings for the other timers are: TCCR1B: 0x03 TCCR2B: 0x04

There may be other side effects from changing TCCR0B. For example my project would not properly run with TCCR0B set to 0x02 or 0x01. But it worked fine at 0x03 and higher. YMMV

Hello,

Does anyone know which timer (TCCR**) that controls which pins on a Arduino Mega? I would like to change the pwm frequency on to of the pins.

/Flotec

Do you know why timer2 on mega board has no OC2C output aka PWM T2C since all 16bit-timers on mega have three registers: OCRnA, OCRnB and OCRnC ?

Do you know when timer5 will be supported by IDE ?

Hi, selfonlypath, I am not aware of how the Arduino designers chose which resources to expose.

But looking at the source code, I think PWM A and B on Timer5 may actually work. Try doing an analogWrite on pins 45 and 46. Let me know if it works and I will update the spreadsheet.

hello mem,

Just done the test with mega board and version 15 running on my Macintosh.

I confirm timer5 works perfect, here is my code where I generate 799,92Hz freq, PWM of 75% on OC5B and PWM of 1% on OC5C with clock system, phase & freq correct PWM

``````int outputPsuB = 45;  // Timer5-B
int outputPsuC = 44;  // Timer5-C

void setup()
{
// PSU outputs via timer5
pinMode(outputPsuB, OUTPUT);  // select Pin as ch-B
pinMode(outputPsuC, OUTPUT);  // select Pin as ch-C

TCCR5A = B00101001; // Phase and frequency correct PWM change at OCRA
TCCR5B = B10001;  // System clock
OCR5A = 10000; // 799,92Hz
OCR5B = 7500; // 75%
OCR5C = 100; // 1%
}
``````

i've tried pre-scaling to diminish frequency and works perfect.

For me it looks 16-bits timer3, timer4 & timer5 are fully compatible with version 15 on Macintosh.

I'll look later on timer1 issue you've raised on your doc :-/

hello again mem,

Ok just made some test on timer1 which as you wisely mention on your doc can only have 2 pins outputs (11 and 12).

Well, it is a bit more tricky when reading atmega1280 spec because pin13 is SHARED by timer0 and timer1 so it can either be related to OCOA or OC1C.

If one make sure to only use timer1 and NOT use timer0, then timer1 becomes a full 16bit timer as timer3, timer4 and timer5 with three PW outputs.

Don't know what is best on how to describe this on your doc but with the above restriction, pin13 can be either TOA (as you have it now) and T1C :P