How to Use a Timer in an Arduino Uno

This site contains affiliate links to products. We may receive a commission for purchases made through these links.

In 2005, the Arduino Development Platform was originally launched as a user-friendly programmable device for art and design projects. The main objective of this launch was to help those without engineering skills to work with micro-controllers and basic electronics without the need of having so much programmable knowledge.

x
What is Arduino?

However, due to its user-friendly capabilities, soon hobbyists and electronic beginners globally adapted it. It’s actually preferred by a lot of people worldwide for POC and prototype developments.

Although it is always okay to start with Arduino, the most recommended option is to move into the core micro-controllers slowly such as ARM, STM, AVR, PIC, or any other, and then program it with your native application.

The reason being, the Arduino Programming Language is quite easy to understand, since most work is done by pre-built functions such as AnalogWrite(), digitalWrite(), and Delay(), among others. The low-level machine language is usually hidden behind them.

However, one thing you need to keep in mind is that the Arduino programs are quite different from other Embedded C coding where you have to deal with bits that are registered and make them low or high on our program’s logic.

How to Use a Timer in an Arduino Uno

Arduino Timers that Don’t Have Delay

If you want to understand everything going on inside the pre-built functions, you first need to dig behind the stipulated terms. A good example is when you use the delay() function to set the Counter Register bits and Timer of the ATmega microcontroller.

In this post, we will mostly focus on the delay() function, although you can use the same functionalities on a similar Arduino IDE. Our Timer register bits will be set and we’ll also use the “Timer Overflow Interrupt” to help toggle an LED whenever there is an occurrence of an interrupt.

The good news is that you can also adjust the Timer bit’s preloader value through the use of push buttons to help with the controlling of the duration when the interrupt occurs.

Timer in Embedded Electronics

Timer is very similar to interrupt. It’s usually similar to a simple clock that is able to measure an event’s time interval. You should note that every microcontroller has an oscillator or clock. For example, in Arduino Uno it’s 16Mhz which is responsible for speed.

Therefore, the higher the clock’s frequency, the higher the processing speeds. It takes approximately 1/16000000 seconds or 62nano nanoseconds to make just one count in Arduino Uno. This means that in every 62nano seconds, Arduino moves instructions.

Arduino Uno Timers

There are 3 timers in Arduino Uno which are used in different functions. They include:

  • Timer0 – An 8-bit timer that is normally used in the delay() or millis() timer functions.
  • Timer1 16-bit timer that is normally used in the servo library.
  • Timer28-bit timer normally used in the tone() function.

The Arduino Timer Registers

Timer registers are normally used to change the configurations.

  1. Timer or Counter Control Registers

This is the register that holds the main control of the Timer and it is also used in controlling the timers’ pre-scalers. With this register, you can also control the timer’s mode with the use of the WGM bits.

Prescaler

The CS12, CS11, CS10 bits in TCCR1B are what set the value of the prescaler. A prescaler can help you set the timer’s clock speed. Some of the prescalers of an Arduino Uno include 1, 8, 64, 256, and 1024.

  1. Timer/Counter Register (TCNTn)

The counter value is normally controlled by the register and then set to the value of a preloader.

If you need the required time in just seconds, here is the formula that you should use:

Timer/counter register = 65535 – (16x 1010 x time in seconds/Prescaler value)

A formula for calculating the value of the preloader for timer 1 in a matter of two seconds:

Timer/counter register 1 = 65535 – (16×1010 x2/1024) which will be equal to 34,285.

Arduino Uno Timer Interrupts

There are several Arduino Uno timer interrupts which we are going to highlight below. Check them out.

  1. Timer Overflow Interrupt

When a timer reaches its maximum value such as the 16-bit 65535, an occurrence of Time Overflow Interrupt happens. Therefore, when this happens a routine ISR interrupt service is called right at the TOIEx in the timer interrupt mask register also known as TIMSKx.

Below is the ISR format:

ISR(TIMERx_OVF_vect)

The Output Compare Register

Whenever there is an occurrence of the Output Compare Match Interrupt, the ISR service is called. What’s more, the OCFxy flag bit is set in the TIFRx register. Normally, the ISR is made to function when the enable bit is set in OCIExy that is found in the TIMSKx register. Therefore, the TIMSKx is the Timer Interrupt Mask Register.

The Timer Input Capture

When this interrupt occurs, the ISR service (TIMERx_CAPT) is called, and the flag bit ICFx is set in TIFRx or Timer Input Interrupt Flag Register. You can enable this ISR by setting the ICIEx’s enable bit found in the TIMSKx register.

Components Required:

Some of the components that you need include:

  • 10k resistor(2), 2.2k(1)
  • LED
  • 2 push buttons
  • 16X2 LCD display
  • Arduino Uno

The process of setting Arduino Timers involves a lot of steps and processes which you must be keen with to ensure everything goes smoothly. What we’ve discussed in this post is just an overview of how you can use Timer in Arduino Uno for your convenience.

Overall, the entire process is quite in-depth and complex for first-time users, but once you get the hang of it, it tends to become easier making you able to do it faster and efficiently.

Bottomline

Even though it is a complicated process to learn how to use a timer in Arduino uno, one can eventually master the process. Following the processes that we have highlighted above is the first step towards ensuring that you are successful at using a timer in Arduino uno.

The many steps and procedures should not scare anyone as it is easy to master the process and get the hang of it.

Special offer for our visitors

Get your Soldering Iron Guide For DIYERS

We will never send you spam. By signing up for this you agree with our privacy policy and to receive regular updates via email in regards to industry news and promotions