Source: https://mokxi.com/libraries/mstimer2
Updated: 2026-11-02

Libraries / Data and time
Arduino library · Data and time

# MsTimer2: call a function on a millisecond schedule

MsTimer2 is a tiny library with one job: call your function every N milliseconds, from a hardware interrupt, so it happens on time even while loop() is stuck in a delay() or a long calculation. It uses Timer2, the 8-bit timer, set to fire once a millisecond, and counts those ticks up to the period you asked for.

Open the example in the editor See the calls

Built in on Mokxi: #include <MsTimer2.h> and it builds, with nothing to install. Free, and no account needed.

## What MsTimer2 does

There are only three calls, all static: MsTimer2::set(period, function), MsTimer2::start() and MsTimer2::stop(). That makes it a quick way to refresh a multiplexed seven-segment display, scan a keypad, or blink a status LED at a steady rate without rewriting loop() around millis().

Timer2 is also the timer tone() uses, and IRremote uses it on an Uno, and on the real board it drives PWM on pins 3 and 11. A sketch can use only one of those at a time. The ATmega32U4 on the Leonardo has no Timer2, so MsTimer2 is for the Uno, Nano, Pro Mini and Mega.

Library Manager name | MsTimer2

Author | Javier Valencia, Paul Stoffregen

Include | #include <MsTimer2.h>

Boards | Arduino Uno, Arduino Nano, Arduino Mega 2560, Arduino Pro Mini 3.3 V

In the simulator | Simulated: it runs against the part the way it does on the bench.

## Install it

It builds on Arduino Uno, Arduino Nano, Arduino Mega 2560, Arduino Pro Mini 3.3 V.

In Mokxi it is built in: #include <MsTimer2.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "MsTimer2" by Javier Valencia, Paul Stoffregen from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add MsTimer2 to lib_deps in PlatformIO. Mokxi follows the calls of release 1.1, so a sketch written here builds there too.

What to know in the simulator: Timer2 is also tone()’s and IRremote’s, as on the real board. The Leonardo has no Timer2.

Arduino Uno · Arduino Nano · Arduino Mega 2560 · Arduino Pro Mini 3.3 V

## The calls you will use

In the order a sketch meets them.

### MsTimer2::set(500, flash)

Sets the period in milliseconds and the function to call. The function takes no arguments and returns nothing.

### MsTimer2::start()

Starts the timer. The first call comes one period later.

### MsTimer2::stop()

Stops calling the function. start() begins counting the period again from zero.

### volatile unsigned long ticks;

Not a library call but the rule that goes with it: any variable both the timer function and loop() touch must be volatile so the compiler rereads it.

## Example: I2C LCD clock and thermometer

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3

Open it in the editor, wired and ready to run · I2C LCD clock and thermometer: the project page

## Common errors and how to fix them

### MsTimer2.h drives the ATmega328P’s and 2560’s Timer2, so it is for the Uno, Nano, Pro Mini and Mega.

The board has no Timer2, which is true of the Leonardo and every non-AVR board. Time the work with millis() in loop(), or use that board’s own timer library.

### tone() stopped working, or IR codes are no longer received

MsTimer2, tone() and IRremote all want Timer2 on an Uno. Keep one of them on Timer2 and move the other job to millis() timing or to TimerOne.

### Printing from the timer function makes the sketch hang

The function runs inside an interrupt, where Serial output can block waiting for an interrupt that cannot run. Set a flag or count in the function and print from loop().

## Keep going

### Learn

- Arduino Timer Interrupt: Timer1 Every Second
- Arduino millis(): Timers and Multiple Tasks

### More data and time libraries

- QRCode
- Madgwick
- PID
- TimerOne
- Low-Power
- RTClib
- DS1307RTC
- DS3231 (NorthernWidget) and DS3231 (Rinky-Dink Electronics)

Every built-in library · Open the editor

## Try MsTimer2 without installing anything

Open the example, change the sketch, and keep your version in a free account.

Start building free

Open the editor
