skip navigational linksPJRC
Shopping Cart Download Website
Home Products Teensy Blog Forum
You are here: Teensy Teensyduino Libraries MsTimer2

PJRC Store
Teensy 4.1, $28.60
Teensy 4.0, $22.80
Teensy LC, $11.65
Main Page
Getting Started
How-To Tips
Code Library

MsTimer2 and FlexiTimer2 Libraries

MsTimer2, by Javier Valencia, lets you periodically run a function, by a configurable number of milliseconds.

FlexiTimer2 is version of MsTimer2 by Wim Leers, which makes the interval resolution configurable, rather than being fixed at 1 millisecond steps

Download: Included with the Teensyduino Installer
Latest MsTimer2 on Github
Latest FlexiTimer2 on Github

Hardware Requirements

TODO: table showing pins unusable as PWM: Teensy 2.0: 10 and 12. Teensy++ 2.0: 1 and 24.

MsTimer2 & FlexiTimer2 work on Teensy LC & 3.0 - 3.6. Internally, IntervalTimer is used these 32 bit boards. For new projects, direct use of IntervalTimer is recommended.

Basic Usage

TODO: write this

Example Program

TODO: add this

Interrupt Context Issues

MsTimer2 and FlexiTimer2 will call your function from interrupt context. Because it will interrupt your program at any moment, special design is necessary to share data with the rest of your program.

Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume variable can not spontaneously change. Because your function may change variables while your program is using them, the compiler needs this hint. But volatile alone is often not enough.

When accessing shared variables, usually interrupts must be disabled. Even with volatile, if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly. If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled for the entire sequence of your code which accesses the data.

TODO: an example.


The original version of these libraries are at:

MsTimer2 page.

FlexiTimer2 page.

New development at: and