Source: https://mokxi.com/libraries/taskscheduler
Updated: 2026-11-22

Libraries / Data and time

Arduino library · Data and time

# TaskScheduler: cooperative multitasking for Arduino and ESP32

Yes. You can use the TaskScheduler library online in Mokxi with nothing to install, and the sketch runs in your browser. The example below, "Two tasks at two rates", runs on the Arduino Uno R3.

Run the example in Mokxi See the calls

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

## What TaskScheduler does

TaskScheduler by Anatoli Arkhipenko lets a sketch do several things at different rates without delay() and without a real operating system. Each job is a Task with an interval, a number of iterations and a callback; a Scheduler holds the tasks, and calling its execute() from loop() runs every task that is due. Blinking an LED every 250 ms while reporting a sensor every second becomes two lines of setup instead of a tangle of millis() variables.

It is cooperative, not preemptive. A callback runs to the end before the next one starts, so one long callback holds up every other task. The win is that no task can be interrupted halfway through, so there are no shared-variable races to guard against, unlike FreeRTOS threads on an ESP32.

Tasks can run forever (TASK_FOREVER) or a fixed number of times, start later with enableDelayed(), change their own interval, and tell how many times they have run with getRunCounter(). The example on this page blinks pin 13 every 250 ms and prints a report three times, once a second, then that task stops on its own. Optional features such as status requests and timeouts are switched on with #define lines before the include.

Library Manager name | TaskScheduler

Author | Anatoli Arkhipenko

Include | #include <TaskScheduler.h>
#include <TaskSchedulerDeclarations.h>
#include <TScheduler.hpp>
#include <TSchedulerDeclarations.hpp>

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 <TaskScheduler.h> and the sketch compiles, with no download, no zip and no Library Manager.

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

What to know in the simulator: The idle pass of _TASK_SLEEP_ON_IDLE_RUN sleeps with delay(1). _TASK_THREAD_SAFE takes the sketch’s own queue functions, as upstream.

- 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.

### Task tBlink(250, TASK_FOREVER, &blink)

A task that runs blink() every 250 ms forever. Declare the callback above it, or give a prototype, since the task takes its address.

### Scheduler runner

The object that holds the tasks and runs them. A sketch usually has one, declared as a global.

### runner.addTask(tBlink)

Puts a task in the scheduler’s chain. Adding does not start it: a task is created disabled unless you pass true as the last constructor argument.

### tBlink.enable()

Starts the task; its first run happens on the next execute(). enableDelayed(500) starts it half a second later instead.

### runner.execute()

Runs every enabled task that is due, once each. Call it on every pass of loop() and keep loop() free of delay().

### tReport.getRunCounter()

How many times the task has run since it was enabled, starting at 1 inside its first callback.

### tBlink.setInterval(100)

Changes the period while the task runs, for a blink that speeds up on an alarm, say.

### tReport.disable()

Stops the task. A task with a set number of iterations disables itself after the last one.

## Example: Two tasks at two rates

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

### 'blink' was not declared in this scope

The Task is declared above the function it calls. Add a prototype such as void blink(); before the Task line, as the example does, or move the callback above the Task declaration.

### A task is added but its callback never runs

Tasks start disabled. Call enable() on each one after addTask(), or construct it with the scheduler and true as the last arguments so it is added and enabled in one step.

### Tasks run late or bunch together

Some callback or loop() itself is blocking, usually with delay() or a slow sensor read. Since the scheduler is cooperative, break long work into shorter steps and let execute() come round often.

## Questions and answers

### Is TaskScheduler the same as FreeRTOS?

No. FreeRTOS switches between threads by itself, each with its own stack. TaskScheduler runs callbacks one after another from loop(), which costs little RAM and works on an Uno.

### Can a task run on micros() instead of millis()?

Yes. Put #define _TASK_MICRO_RES before the include and the intervals are counted in microseconds.

## Keep going

### Learn

- Arduino Multitasking With millis(), Step by Step
- Arduino Blink Without Delay: Two LEDs, Two Rates

### 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 TaskScheduler without installing anything

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

Start building free

Open the editor
