skip navigational linksPJRC
Shopping Cart Checkout Shipping Cost Download Website
Home MP3 Player 8051 Tools All Projects PJRC Store Site Map
You are here: Teensy Teensyduino Libraries MsTimer2

PJRC Store
Teensy 3.6, $29.25
Teensy 3.5, $24.25
Teensy 3.2, $19.80
Teensy LC, $11.65
Teensy 2.0, $16.00
Teensy++ 2.0, $24.00
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