MsTimer2 or TimerOne or another timer library?

Hello,

I am trying to implement a timer interrupt using Arduino UNO. I want to have a function that runs at a defined time interval, like every 5 ms. But during my research, i have seen several timer libraries compatible with the UNO, like the TimerOne, MsTimer2, TimerThree, etc.

I want to know which library is better and why? I have seen my classmates using the MsTimer2 library but for the sake of my own understanding, i want to know how to justify using that library. I would rather not just blindly follow what other projects are using at my school.

Thanks for any help. :slight_smile:

Your arduino has multiple timers with 8 or 16 bit resolution. Timer0 is used for millis() so usually you don’t mess around with that one. On a UNO that leaves you with timer1 (16 bits) and timer2 (8 bits).

You would need to read the libraries docs to see optimisations but mainly timerOne plays with the timer1 whereas MsTimer2 is a small and very easy to use library to use Timer2. It's called MsTimer2 because it "hardcodes" a resolution of 1 millisecond on timer2. FlexiTimer2 is version of MsTimer2 which makes the interval resolution configurable, rather than being fixed at 1 millisecond steps.

All libraries bring a level of abstraction and would work on multiple architectures. Some libraries such as Servo or Tone would already make use of a timer so you would have to use another one. PWM needs on some pins might also drive you to select a timer versus another one.

If you have a known arduino to play with, What about configuring the timer of your choice directly? It’s not rocket science for the use you describe (tons of on line tutorials) and you have nothing to justify besides why you picked a 16 bit or 8 bit timer

Here is an example of tutorial amongst many

J-M-L:
If you have a known arduino to play with, What about configuring the timer of your choice directly? It’s not rocket science for the use you describe (tons of on line tutorials) and you have nothing to justify besides why you picked a 16 bit or 8 bit timer

Here is an example of tutorial amongst many

This is what I would do.

Interrupts are low-level enough that - IMO - it's better to do it by hand.