How to use timer interrupts on the Arduino Due board?

The Arduino Due is a powerful microcontroller board based on the Atmel SAM3X8E microcontroller. One of its features is the ability to use timer interrupts, which can be a useful way to perform repetitive tasks at specific time intervals. This article will explain how to use timer interrupts using DueTimer library on the Arduino Due board with LED blink as an example.

What are Timer Interrupts?

Timer interrupts are events that occur at specific time intervals in a microcontroller. They are triggered by a timer module within the microcontroller, and can be used to perform repetitive tasks at regular intervals, such as reading a sensor, updating an OLED display, sending data over a network or simply blinking a LED as illustrated in this tutorial.

Using Timer Interrupts on the Arduino Due

The Arduino Due board has nine timers available for use: Timer 0 to Timer 9. In this example, we will use Timer 3 using DueTimer library. The DueTimer library is a flexible and easy-to-use library that allows you to program timers and interrupts on the Arduino Due. With this library, you can control the timing and pulse generation of your projects with precision.

Getting Started

To start using the DueTimer library, you will need to install the library on your computer. You can do this by navigating to Sketch > Include Library > Manage Libraries in the Arduino IDE. Search for the DueTimer library and install it.

After installing the library, you can start a new project in the Arduino IDE and include the DueTimer library in your code by adding the following line at the top of your sketch:

#include <DueTimer.h>

Blinking an LED

The first step in using the DueTimer library to blink an LED is to set up the LED. Connect an LED to a collector of a general purpose bipolar junction transistor BC547, connect emitter to ground of Arduino Due and connect the base to the digital pin 10 on the Arduino Due. We are using a transistor because the power from the Arduino Due and therefore a LED brightness is low and by using BJT transistor and using a +5V supply connected to the load the brightness can be increased. The Arduino Due sends high signal and low signal at interval of 500ms which is controlled by the interrupt service routine(ISR).

The following picture shows the LED blinking program using the Arduino Due timer interrupt.

 Arduino Due Timer Interrupt LED blink

Next, we will create a function to blink the LED. This function will change the state of the LED each time it is called. The LED will start in the low state, and then change to the high state and vice versa.

int LED = 10;
int state = LOW;

void blink() {
  state = !state;
  digitalWrite(LED, state);
}

Now, we will set up the timer in the setup function. We will attach the blink function to the timer and start the timer with a period of 500,000 microseconds. This means that the blink function will be called every 500,000 microseconds.

void setup() {
  Timer3.attachInterrupt(blink);
  Timer3.start(500000);
}

Finally, we will leave the loop function empty, as it is not necessary for this project.

void loop() {

}

The following is the full program for LED blink using the DueTimer library.

#include <DueTimer.h>

void setup() {
  Timer3.attachInterrupt(blink);
  Timer3.start(500000);
}
int LED = 10;
int state = LOW;

void blink() {
  state = !state;
  digitalWrite(LED, state);
}

void loop() {

}

The following is the complete Arduino blink program.

#include <DueTimer.h>

void setup() {
  Timer3.attachInterrupt(blink);
  Timer3.start(500000);
}
int LED = 10;
int state = LOW;

void blink() {
  state = !state;
  digitalWrite(LED, state);
}

void loop() {

}

This code is an example of how to use the timer interrupts using DueTimer library with example of blinking a LED with Arduino Due board.

  • The first line includes the DueTimer library, which provides timer functions for the Arduino Due.
  • In the setup function, Timer3 is attached to the blink function as an interrupt. This means that when Timer3 fires, it will trigger the blink function.
  • The Timer3.start function is then called with the argument 500000 which starts the timer and sets the interval at which it will trigger the interrupt. The interval is in microseconds.
  • The int LED = 10 line defines a constant integer named LED and assigns it the value 10, which is the digital pin connected to the LED.
  • The int state = LOW line defines a variable named state and assigns it the initial value LOW, which is a predefined constant in the Arduino language representing a low voltage state.
  • The blink function is defined next. It simply changes the value of state between LOW and HIGH and writes it to the LED using the digitalWrite function. This will turn the LED on and off.
  • Finally, the loop function is defined, but it is empty in this code and will not execute anything.

This code will cause the LED to turn on and off every 500 milliseconds, or half a second.

Conclusion

With the DueTimer library, you can easily program timers and interrupts on the Arduino Due. By following the steps in this post, you can use the DueTimer library  to blink an LED on the Arduino Due using timer interrupt. This is just the tip of the iceberg, as the DueTimer library has many other features that you can explore. Happy coding!

References:

- Arduino LED PWM(Pulse Width Modulation)

- Arduino LED with Button

Post a Comment

Previous Post Next Post