2

I understand there are 3 timers on the Arduino UNO. What specific registers do I need to use to access them?

My goal is to use these timers to poll different sensors I am using with a ms period. Are there any pre-made libraries out there to do this?

3
  • Have you had a chance to look at the datasheet yet? Commented Dec 28, 2014 at 0:50
  • The datasheet for the ATMEL SOC, or the Arduino breakout board itself?
    – Sean
    Commented Dec 28, 2014 at 0:57
  • For the MCU (not SoC). Commented Dec 28, 2014 at 0:58

2 Answers 2

2

For the UNO, use Timer1.

An example of the way you can use it is as follows:

Download the TimerOne library: https://code.google.com/p/arduino-timerone/downloads/list

/*
 *  Timer1 library example
 */

#include "TimerOne.h"

void setup()
{
  pinMode(10, OUTPUT);
  Timer1.initialize(500000);         // initialize timer1, and set a 1/2 second period
  Timer1.pwm(9, 512);                // setup pwm on pin 9, 50% duty cycle
  Timer1.attachInterrupt(callback);  // attaches callback() as a timer overflow interrupt
}

void callback()
{
  digitalWrite(10, digitalRead(10) ^ 1);
}

void loop()
{
  // your program here...
}
1

Timer 0 is use'd by the arduino core libraries for functions like millis(). It is best left alone. Leaving timer1 and 3 are available for general use. Both 1 and 3 have well established libraries, for simple implementation, without the need to know the mechanics under the hood, about the registers and such.

You can find links and general explanations at timer1. I always recommend the github versions.

And if you are still wanting to know the details the source is quite readible.


Note you don't need 3 timers to read 3 sensors. One timer on a multiple of the the 3, should suffice which can then implement a statemachine to sample at the correct periods.

1
  • 1
    I think you mean timer2, not timer3.
    – Gerben
    Commented Dec 28, 2014 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.