Timers in Arduino Uno


As discussed earlier, Arduino Uno has 3 timers: Timer0, Timer1 and Timer2. Timer0 and Timer2 are 8-bit counters (they count from 0 to 255), while Timer1 is a 16-bit counter (it counts from 0 to 65535). Internally, Timer0 is used for the millis() function, and therefore, it is recommended not to mess with it. You can use Timer1 and Timer2 for your custom requirements.

Note that the clock frequency of Arduino Uno is 16 MHz. Therefore, no timer can have intervals shorter than (1/16000000). However, for most applications, you would want longer intervals (lower frequencies). In other words, you would want to count slower than the max rate. For this, pre-scalers are used. They divide the input clock frequency by a factor, and the timers then use that reduced frequency to count. For example, Timer0 uses a divisor of 64. Thus, its frequency is (16MHz/64) = 250 KHz. It makes one count in every (1/250,000) = 0.000004 seconds. Thus, it counts from 0 to 255 in 0.000004*255 = 0.00102 seconds, which is approximately 1 millisecond. Now you can understand why the millis() function uses Timer0. 

You can perform similar calculations when using Timer1 and Timer2. However, you need to perform these calculations only when setting the registers manually. If you are using a library like TimerOne, you need not worry about these calculations. All you need to do is specify the interval, and the library will take care of everything else.

Updated on: 24-Mar-2021

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements