Electronic – arduino – Any reason not to use Timer0 on AVR

arduinoatmegaavrinterrupts

Just a basic question… For arduino/avr/ATMega328 I find lots of examples using Timer1 (there's even a whole library for it) but rarely any that utilize Timer0 (or Timer2).

Now, I know that using any of these timers to control an ISR method will disable the use of PWM on it's associated pins, but is there a specific reason that people seem to stay away from Timer0? Is it used for something else internally that one should not mess with?

My current application is fine using Timer1, I'm just curious if I'm missing something about these other often neglected timers.

Best Answer

If the question is about a bare AVR microcontroller, then no, there is no constraint on using any of the timers.

  • For the Arduino, Timer0 is best avoided, as the millis(), delay() and all internal timekeeping in the Arduino libraries use this timer. Changing it's time constant will impact these operations.
  • Timer1, as the question states, is the popular first choice for any timer / counter tasks, through libraries such as the TimerOne library, as well as direct manipulation. It is a 16-bit timer, thus with finer resolution than the 8-bit timers.
  • Timer2 is used by the tone() library, but since that is not so ubiquitously used, there is also the msTimer2 library that many find very useful, much like TimerOne. Direct manipulation of Timer2 is fine too.
  • The additional timers on the Mega, Timer3, Timer4 and Timer5, each a 16-bit timer, are also available for use, if you use an Arduino Mega board.