Arduino - Timers & Interrupts

Timers are an important part of the functionalities in microcontrollers and play a vital role in controlling various aspects of it. A timer or a counter is a piece of hardware built in the Arduino board to measure events and carry out specific tasks at a particular interval of time.

Few of the applications of the timers are as follows:

  1. PWM — Pulse Width Modulation is a method to control the duty cycle, thereby the digital output of the controller. It has a wide range of applications from motor control to LED dimming and much more.
  2. Timer Interrupts — In a lot of cases we need to count the exact time before an event occurs or give a desired output of a specific pin after some time — timer interrupts help us achieve these actions.

Timers in Arduino

Depending on each microcontroller, there are multiple timers available to work with. Every timer has a counter which increments the timer on each tick of the timer’s clock. The ATmega328 built on the Arduino Uno has a total of 3 timers available, which are:

  • Timer0 — An 8 bit timer used by Arduino functions delay(), millis() and micros().
  • Timer1 — A 16 bit timer used by the Servo() library
  • Timer2 — An 8 bit timer used by the Tone() library

Note: Difference between 8 and 16 bit is the timer resolution — 8 bit ranges for (0–255) values and 16 bit ranges for (0–65535) values.

In this blog, we’ll be exploring these timers and have a look at an example to get an idea about using them.

A 16Mhz clock acts as a base clock in the Arduino Uno, but since 16Mhz is too fast for our application, we’ll have to divide it by some number in order to make it feasible for our use. This number which we use to divide is known as the Prescaler, it helps us in bringing the high-frequency base clock down to match our application. A prescaler dictates the speed of your timer according the the following equation:

timer speed (Hz) = (Arduino clock speed (16 Mhz))/ prescaler

After this, let’s refer to the data sheet of ATmega328 as it’ll help us understand the rest of the process more coherently.

This is the counter block diagram — The prescaler receives a pulse from a clock cycle and then passes it to the Control Logic, henceforth the Control Logic increments the TCTn register by 1.

Now we can compare the value of TCNTn with a specific value when the TCNTn register arrives at this value, we know that it is passed a specific time.

This method is called CTC mode for “Clear Timer on Compare”. The value in the TCNTn register is compared to the OCRn register, and when a compare match occurs the TOVn generates an interrupt.

Another important step is to determine the value of the OCRn register for counting a specific time.

It is important to set the prescaler at the end because after this instruction the timer starts to count, and if we need to stop it, we must reset all the bits of TCCR0B.

Now we’ll look at a simple example with which we can turn a LED on/off with the use of timers.

You can use the following schematic for setting up the arrangement

Once you run this code, it should turn the LED on and off every second, with the help of the internal timer.

In this blog, we learnt the working of the internal timers of the Arduino, how to use them to set them for a particular action that we need to achieve after a period with the help of interrupts.