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 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 ATmega1280* or ATmega2560* (m1280, m1281, m2560, m2561)
// B00000xxx - 3 last bits 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 OCRxA or OCRxB for first and second PWM pin) is compared against the value in an 8bit counter (0-255 and wrap around in register TCNTx).

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

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

Lineage 2 - Auto Clicker

ac2usb.jpg

ac2usb is a small usb piece, like a usb pen-drive. It actually is keyboard mouse combo which used for automated key press and mouse click for online game, Lineage II in specific. It works on all Lineage II servers with or without lameguard, smartguard or whatever-guard there might be. Yes, it works just like the very real-life keyboard mouse you are using, but without any buttons :D

[Read More…]

 

AVR - Transistor tester

avr-ttester-st7735.jpg

I always want one since it first appeared on the interweb around 2005. The very first version used PIC chip and it originated namely LC meter. I actually bought all components but at that time, I had no clue how to program PIC, and thus I failed to build one. I still have no idea how to program PIC chip until now :D

[Read More…]

 

PS3 pwned

PS3-PHAT.jpg Got a PS3 console from my cousin. It’s PHAT version, yeah, the big-fat version, back in 2008 or something. The first thing I do with this console is tear it down LOL. I don’t have any PS3 game and I don’t play game on console, so maybe I’ll put Linux on this thing. Then I have to jail-brake this PS3 first. This entry is about hacking an old PS3 console.

[Read More…]

 

Wordclock = WS2811B + Arduino = beautiful combination

wordclock_intro.jpg I wanted to make one a few years ago but I didn’t have much time as well as I didn’t know how to do it, or was too lazy to do it :D
Back then, I lacked of enthusiasm, motive and didn’t want to learn new things. Until this year, I had more free time and change of mind, I want to learn new things. So, I started this project, a few years after

[Read More…]

 

Serial Communication for debugging - dummies version

Com_port.jpg

If you are older than 28 years old, you might already know what a COM port looks like. Yes, COM port stands for Communication port. It was used widely to connect external device to the computer in the old days around 2005, such as: modem, mouse, even printer or transfer data between 2 PCs. Yes, COM port was understood as serial port.

[Read More…]

 

ATTiny13 Programming with Arduino IDE

attiny13a-dip.jpg Sometime, you need a small circuit to flash some LEDs or do some simple task, say, power on delay function for your DIY amplifier, power on reminder for your soldering station (beep every 3 mins) - which I forgot to turn off all the time… probably, an Attiny13 is just what you need.

[Read More…]

 

Quiz game show buzzer

buzzer-game-show-2-buttons.jpg

It is sometime called game show buzzer lockout system, but I thought many people confuse with the name so I changed it.

If you’ve watched some game shows like Family Feud, you probably know what a game show buzzer is. And yes, it is a set of buzzers that once a button is pressed, the other button is deactivated to indicate the player who takes answer fastest. It usually is 2 or 3 players at a time on TV. That is easy. But what if you need a buzzer set for 10+ players or teams at the same time. Well, not easy anymore.

[Read More…]

 

Middle mouse button fix for Asus touchpad in windows 10

asus-x302la-smartgesture.png

Just bought this laptop Asus x302l about 3 weeks ago. After a week, I found out that it (is) was impossible to do middle mouse button. There is no such an option for middle mouse button in ASUS Smart Gesture, or I should call ASUS not-so-Smart Gesture. In this entry, I will call it “Asus gesture”.

Tried everything I could find on the internet, also hit the support team of ASUS as well, none came with success. I was so disappointed, and still do. So, do I have carry an extra mouse on the go like forever?

[Read More…]

 

Asus X302L - fix loose touchpad makes clicking noise

asus-x302la.jpg
With 13.3” LCD, this lightweight laptop has the brain of i5-5200u, 4GBs RAM, 500GB Hard drive blah blah blah…

Just bought one yesterday from local store for AU$ 799 and it surprised me when it comes with 128GB SSD instead. Nice!

[Read More…]