Arduino Timer Interrupt Examples

 Here are some example of using Arduino timer interrupt using TimerOne library. TimerOne timer library uses the timer 1 of the Arduino which is based on ATmega328p microcontroller. Using this library you can create timer interrupt to execute codes and functions based on timed events or you can use it to create timed PWM signals. 

One of the benefit of TimerOne is that it is easy to use. Another advantage is that it generates accurate signal timing as opposed to built in delay() function. With the TimerOne library you can create timing delay from 1 microsecond(1 MHz) to 8,388,480 microseconds which is about 8.4 seconds. Also with the TimerOne library one can generate PWM signals with better duty cycle control. With analogWrite() function one will be able to set the duty cycle from 0 to 255 but with TimerOne library one can set the duty cycle from 0 and 1023.

To install the TimerOne library one can use the manage libraries in Arduino IDE,

or, use the following download link:

https://playground.arduino.cc/Code/Timer1

Now following are examples of using Arduino Timer interrupts.

Example 1: Square wave generation

In this example square wave is generated using the TimerOne library. The following is the library 

Arduino TimerOne Square Wave

The following code is an example of using TimerOne library to generate 1KHz square wave signal at the digital pin 10.

#include <TimerOne.h>

int wavePin = 10;
volatile int sqWave = LOW;

void setup(){
pinMode(wavePin, OUTPUT);
Timer1.initialize(500);
Timer1.attachInterrupt(squareWaveOutput);
}

void loop(){
}

void squareWaveOutput(){
digitalWrite(wavePin, sqWave);
sqWave = !sqWave;
}

In the above code first we have to set the pin 10 called wavePin as output. And then in order to use any methods(such as initialize, attachInterrupt) of the TimerOne library, we must initialize the period. According to the Arduino TimerOne library website, the initialize method is stated as below.

initialize(period)
You must call this method first to use any of the other methods. You can optionally specify the timer's period here (in microseconds), by default it is set at 1 second. Note that this breaks analogWrite() for digital pins 9 and 10 on Arduino.

The Timer1 is the object of the TimerOne library. With the Timer1 object method initialize, we set the period of the square wave of 500micro-seconds. Then using the attachInterrupt() method we call a user defined function called squareWaveOutput(). Once the period previously set is reached, timer interrupt is fired and the function squareWaveOutput() is invoked. In the squareWaveOutput() function we send low and high signal alternatively to the wavePin which is pin 10. 

The following is the graph of the square generated by the above program.

graph square wave
 

Example 2: PWM signal generation

TimerOne can be used PWM signal on pin 9 and 10 on Arduino Uno. Here we will generate PWM signal on both pin 9 and 10 simultaneously at different duty cycle. The following shows the circuit wiring diagram.

Arduino TimerOne PWM signal

The program to generate PWM signals on pin 9 and 10 using TimerOne.

#include <TimerOne.h>

void setup(){
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
Timer1.initialize(1000);
Timer1.pwm(9, 512);
Timer1.pwm(10, 255);
}

void loop(){
}

 In the above code first we have to set the pin 9 and 10 as output. Then in order to use any methods of the TimerOne library, we must use the initialize function and set the period as previously done in square wave generation code. Here the period is set to 1000us, which in frequency is 1KHz. Then the pwm method is used to set the duty cycle of 512 for pwm signal on pin 9 and duty cycle of 255 is set for pwm signal on pin 10.

The following shows the PWM signal on pin 9.

PWM signal on Arduino pin 9
 

The following shows the PWM signal on pin 10. 

PWM signal on Arduino pin 10

Here it was illustrated how to use Arduino Timer Interrupt. Another way to generate interrupt is using the Arduino Hardware Interrupt, see Simple Arduino Hardware Interrupt Example. Arduino Uno/Arduino Nano are based on ATmega328p microcontroller. See the following tutorials on ATmega328p timers and interrupt service routine(ISR):

- ATmega328p Timer 0 Time Delay with Overflow

- Arduino 8MHz Signal Generator with ISR

Post a Comment

Previous Post Next Post