Source: https://mokxi.com/libraries/arduino-timer
Updated: 2026-11-23

Libraries / Data and time

Arduino library · Data and time

# arduino-timer: call a function later, or every interval

Yes. You can use the arduino-timer library online in Mokxi with nothing to install, and the sketch runs in your browser. The example below, "Blink and report without delay()", runs on the Arduino Uno R3.

Run the example in Mokxi See the calls

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

## What arduino-timer does

arduino-timer by Michael Contreras is a small software timer for sketches that need a few things to happen on a schedule. You make one timer, give it functions with every(1000, blink) for a repeat, in(250, once) for a single call after a delay, or at(time, report) for a single call at a millis() value, and call timer.tick() from loop(). Nothing uses a hardware timer, so it works on every board and leaves PWM and Servo alone.

The one rule to learn is the handler’s return value. A handler is bool name(void *) and for an every() task its return decides what happens next: true keeps the task repeating, false cancels it. Tasks made with in() and at() run once whatever they return. That makes "blink five times and stop" a counter and a return false, with no separate cancel call needed.

Compared with the Ticker library and TaskScheduler, arduino-timer is the lightest: one object, a fixed table of 16 tasks by default, and no task objects to declare. The example counts a 100 ms tick, prints once after 250 ms, and reports the count at 1050 ms, all from a loop() that does nothing but tick().

Library Manager name | arduino-timer

Author | Michael Contreras

Include | #include <arduino-timer.h>

Boards | every board Mokxi compiles for

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

## Install it

It builds on every board Mokxi compiles for.

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

On a real board, install "arduino-timer" by Michael Contreras from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add arduino-timer to lib_deps in PlatformIO. Mokxi follows the calls of release 3.0.1, so a sketch written here builds there too.

- ESP32-C3
- Arduino Uno
- Arduino Nano
- Arduino Mega 2560
- Arduino Leonardo
- SparkFun Pro Micro
- Arduino Micro
- ESP32-C6
- WCH CH32V003
- ATtiny85
- Raspberry Pi Pico
- Raspberry Pi Pico W
- Seeed XIAO SAMD21
- STM32
- STM32 Nucleo-F411RE
- STM32 Blue Pill
- micro:bit V2
- Arduino Uno R4
- ESP32 DevKit
- ESP32-S3-DevKitC-1
- ESP8266 NodeMCU
- Arduino Pro Mini 3.3 V
- Seeed XIAO ESP32-C3
- ESP32-C3 SuperMini
- Seeed XIAO ESP32-C6
- ESP32-DevKitC V4
- LOLIN32 Lite
- Wemos D1 R32
- Cheap Yellow Display
- TTGO T-Display
- Seeed XIAO ESP32-S3
- ESP32-S3-CAM
- Arduino Nano ESP32
- Arduino Nano RP2040 Connect
- Seeed XIAO RP2040
- Waveshare RP2040-Zero
- Arduino Uno R4 WiFi

## The calls you will use

In the order a sketch meets them.

### auto timer = timer_create_default()

A timer with room for 16 tasks, counting in milliseconds. Timer<4, micros> makes a smaller one in microseconds.

### timer.every(1000, toggle_led)

Calls toggle_led every 1000 ms for as long as it returns true. Returns a task handle you can keep for cancel().

### timer.in(250, once)

Calls once a single time, 250 ms from now.

### timer.at(millis() + 1050, report)

Calls report a single time when millis() reaches the given value.

### timer.tick()

Runs every task that is due. Call it on every pass of loop(); it returns the milliseconds until the next task.

### timer.cancel(task)

Stops a task by its handle and clears the handle.

### timer.size()

How many tasks are scheduled right now, useful to check the table is not full.

## Example: Blink and report without delay()

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

## Common errors and how to fix them

### invalid conversion from 'void (*)()' to handler type

The handler has the wrong shape. It must be declared bool name(void *) and return a value, for example bool blink(void *) { ... return true; }.

### The repeating task runs once and stops

The handler returns false, which cancels an every() task. Return true from any handler you want to keep repeating.

### every() returns no handle and the task never runs

The task table is full, 16 tasks by default. Cancel tasks you are done with, or make a bigger timer with Timer<32> timer; at the cost of a little RAM.

## Questions and answers

### Can I pass a value to the handler?

Yes. every(), in() and at() take an optional third argument that arrives as the handler’s void * parameter, so one handler can serve several pins.

## Keep going

### Learn

- Arduino millis(): Timers and Multiple Tasks
- Arduino Multitasking With millis(), Step by Step

### More data and time libraries

- QRCode
- Madgwick
- PID
- TimerOne
- MsTimer2
- Low-Power
- RTClib
- DS1307RTC
- Ds1302
- Chrono
- Tasker
- CountDown
- microDS3231
- DS1307 (Seeed)

Every built-in library · Open the editor

## Try arduino-timer without installing anything

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

Start building free

Open the editor
