Arduino Timer / PWM cheat sheet


Arduino Timer

Arduino UNO (ATmega328p) has three 8bit timers

Timer0 - used for millis() micros() delay()… and is on pin 5, 6
Timer1 - 16bit timer is on pin 9, 10
Timer2 - 8bit timer is on pin 3, 11

Arduino Mega has six 8bit timers

Timer0 - millis, micros… and is on pin 4, 13
Timer1 - is on pin 11, 12
Timer2 - is on pin 9, 10
Timer3 - is on pin 2, 3, 5
Timer4 - is on pin 6, 7, 8
Timer5 - is on pin 46, 45, 44


Arduino PWM

Arduino UNO (ATmega328p) has six 8bit PWM (on timer0-timer2)

// For Arduino using ATmega8, 168 or 328* (m8, m168, m328, m238p)
// B00000xxx - 3 last bits (least significant bit or LSB) decide the divisor of the timer
// Frequency = 16Mhz / (256 * divisor)
// Example: divisor = 1, frequency = 16Mhz / (256) = 62.5Khz

//----- PWM frequency for D5 & D6 -----
//Timer0 divisor = 1, 8, 64, 256, 1024
//TCCR0B = TCCR0B & B11111000 | B00000001;    // 62.5KHz
//TCCR0B = TCCR0B & B11111000 | B00000010;    // 7.8KHz
  TCCR0B = TCCR0B & B11111000 | B00000011;    // 976Hz (default)
//TCCR0B = TCCR0B & B11111000 | B00000100;    // 244Hz
//TCCR0B = TCCR0B & B11111000 | B00000101;    // 61Hz
 
//----- PWM frequency for D9 & D10 -----
//Timer1 divisor = 2, 16, 128, 512, 2048
//TCCR1B = TCCR1B & B11111000 | B00000001;    // 31KHz
//TCCR1B = TCCR1B & B11111000 | B00000010;    // 3.9KHz
  TCCR1B = TCCR1B & B11111000 | B00000011;    // 490Hz (default)
//TCCR1B = TCCR1B & B11111000 | B00000100;    // 122.5Hz
//TCCR1B = TCCR1B & B11111000 | B00000101;    // 30.6Hz
 
//----- PWM frequency for D3 & D11 -----
//Timer2 divisor = 2, 16, 64, 128, 512, 2048
//TCCR2B = TCCR2B & B11111000 | B00000001;    // 31KHz
//TCCR2B = TCCR2B & B11111000 | B00000010;    // 3.9KHz
//TCCR2B = TCCR2B & B11111000 | B00000011;    // 980Hz
  TCCR2B = TCCR2B & B11111000 | B00000100;    // 490Hz (default)
//TCCR2B = TCCR2B & B11111000 | B00000101;    // 245Hz
//TCCR2B = TCCR2B & B11111000 | B00000110;    // 122.5Hz
//TCCR2B = TCCR2B & B11111000 | B00000111;    // 30.6Hz 

Arduino Mega (ATmega2560) has fifteen 8bit timers (on six timer0-timer5)

//For Arduino using ATmega2560* (m1280, m1281, m2560, m2561)
// B00000xxx - 3 LSBs decide the divisor of the timer
// Frequency = 16Mhz / (256 * divisor)
// Example: divisor = 1, frequency = 16Mhz / (256) = 62.5Khz

//----- PWM frequency for D4 & D13 ------------------------------ 
//Timer0 divisor = 1, 8, 64, 256, 1024
//TCCR0B = TCCR0B & B11111000 | B00000001;    // 62.5KHz
//TCCR0B = TCCR0B & B11111000 | B00000010;    // 7.8KHz
  TCCR0B = TCCR0B & B11111000 | B00000011;    // 976Hz (default)
//TCCR0B = TCCR0B & B11111000 | B00000100;    // 244Hz
//TCCR0B = TCCR0B & B11111000 | B00000101;    // 61Hz
 
 
//----- PWM frequency for D11 & D12 -----------------------------
//Timer1 divisor = 1, 8, 64, 256, 1024
//TCCR1B = TCCR1B & B11111000 | B00000001;    // 31KHz
//TCCR1B = TCCR1B & B11111000 | B00000010;    // 3.9KHz
  TCCR1B = TCCR1B & B11111000 | B00000011;    // 490Hz
//TCCR1B = TCCR1B & B11111000 | B00000100;    // 122.5Hz
//TCCR1B = TCCR1B & B11111000 | B00000101;    // 30.6Hz
 
//----- PWM frequency for D9 & D10 ------------------------------
//Timer2 divisor = 2, 16, 64, 128, 256, 512, 2048
//TCCR2B = TCCR2B & B11111000 | B00000001;    // 31KHz
//TCCR2B = TCCR2B & B11111000 | B00000010;    // 3.9KHz
//TCCR2B = TCCR2B & B11111000 | B00000011;    // 980Hz
  TCCR2B = TCCR2B & B11111000 | B00000100;    // 490Hz
//TCCR2B = TCCR2B & B11111000 | B00000101;    // 245Hz
//TCCR2B = TCCR2B & B11111000 | B00000110;    // 122.5Hz
//TCCR2B = TCCR2B & B11111000 | B00000111;    // 30.6Hz
 
//----- PWM frequency for D2, D3 & D5 ---------------------------
//Timer3 divisor = 2, 16, 128, 512, 2048
//TCCR3B = TCCR3B & B11111000 | B00000001;    // 31KHz
//TCCR3B = TCCR3B & B11111000 | B00000010;    // 3.9KHz
  TCCR3B = TCCR3B & B11111000 | B00000011;    // 490Hz
//TCCR3B = TCCR3B & B11111000 | B00000100;    // 122.5Hz
//TCCR3B = TCCR3B & B11111000 | B00000101;    // 30.6Hz
 
//----- PWM frequency for D6, D7 & D8 ---------------------------
//Timer4 divisor = 2, 16, 128, 512, 2048
//TCCR4B = TCCR4B & B11111000 | B00000001;    // 31KHz
//TCCR4B = TCCR4B & B11111000 | B00000010;    // 3.9KHz
  TCCR4B = TCCR4B & B11111000 | B00000011;    // 490Hz
//TCCR4B = TCCR4B & B11111000 | B00000100;    // 122.5Hz
//TCCR4B = TCCR4B & B11111000 | B00000101;    // 30.6Hz
 
//----- PWM frequency for D44, D45 & D46 ------------------------
//Timer5 divisor = 2, 16, 128, 512, 2048
//TCCR5B = TCCR5B & B11111000 | B00000001;    // 31KHz
//TCCR5B = TCCR5B & B11111000 | B00000010;    // 3.9KHz
  TCCR5B = TCCR5B & B11111000 | B00000011;    // 490Hz
//TCCR5B = TCCR5B & B11111000 | B00000100;    // 122.5Hz
//TCCR5B = TCCR5B & B11111000 | B00000101;    // 30.6Hz

Default frequencies of analogWrite():

Arduino Pins 5 and 6: 976Hz
Arduino Pins 9, 10, 11, and 3: 490Hz

Too low for switch mode power supply (buck converter) and generate audible noise when use for motor or brushless fan (audible tone around 490Hz). One trick is use the commands above (eg: TCCR1B = TCCR1B & B11111000 | B00000011;) after calling analogWrite() to change the default frequency. Otherwise, a custom analog output function should be used instead.

analogWrite(PWMpin, value);

The input value (stored in OCRnA or OCRnB for first and second PWM pin) is compared against the value in an 8bit counter (0-255 and wrap around in register TCNTn).

If value <= counter, output HIGH
If value > counter, output LOW

+-------+            +-------+
|	|            |       |
|	|            |       |
|	|            |       |
+	+------------+       +------------+
0      value        255/0    value       255
 

Add comment

Fill out the form below to add your own comments

User data




Add your comment


[color=red] TEXT [/color]
[quote] TEXT [/quote]
[code=arduino avrasm cpp css java javascript nginx perl python] TEXT [/code]
Clear format