A lightweight, non-blocking, template-based timer library specifically optimized for the Raspberry Pi Pico (RP2040) using the Earle Philhower Arduino Core.
PicoTimer allows you to execute functions at specific intervals without using delay(). It is designed to be "fire and forget," handling repetition logic and timing wrap-around (rollover) automatically.
- Non-Blocking: Uses polling via
.update(), ensuring your main loop remains responsive. - Multiple Modes: * Infinite: Continuous repetition.
- One-Shot: Runs once and stops (perfect for alarms/timeouts).
- Finite Burst: Runs a fixed number of times (perfect for LED pulses).
- Dual Precision: Supports
MillisTimer(ms) andMicrosTimer(μs). - Multicore Ready: Includes
PicoTimerSafeusing hardware critical sections for safe communication between Core 0 and Core 1. - Zero Dependencies: No extra libraries required.
- Download this repository as a
.zipfile. - In the Arduino IDE, go to Sketch -> Include Library -> Add .ZIP Library...
- Select the downloaded file.
- Alternatively, extract the folder into your
Documents/Arduino/libraries/directory.
#include <PicoTimer.h>
void onTick() {
Serial.println("1 Second Passed!");
}
// Create an infinite timer: 1000ms period, calls onTick, repeats = 0 (infinite)
MillisTimer heartbeat(1000, onTick, 0);
void setup() {
Serial.begin(115200);
}
void loop() {
// You must call update() in the infinite loop for the timer to work
heartbeat.update();
}The RP2040 is a dual-core MCU. If Core 0 is running a timer but Core 1 needs to stop or reset it based on a sensor event, use MillisTimerSafe. This prevents memory corruption by using hardware-level locking.
#include <PicoTimer.h>
void emergencyCallback() {
digitalWrite(LED_BUILTIN, HIGH);
}
// Thread-safe timer
MillisTimerSafe safetyTimer(500, emergencyCallback, 0);
void loop() {
safetyTimer.update(); // Running on Core 0
}
void loop1() {
// Running on Core 1: Safely stop the timer running on Core 0
if (analogRead(A0) > 800) {
safetyTimer.stop();
}
}| Class | Timing Unit | Thread Safe | Description |
|---|---|---|---|
| MillisTimer | Milliseconds | No | Standard timer for single-core logic. |
| MicrosTimer | Microseconds | No | High-precision timer for single-core logic. |
| MillisTimerSafe | Milliseconds | Yes | Safe for use across Core 0 and Core 1. |
| MicrosTimerSafe | Microseconds | Yes | High-precision safety for multicore. |
All classes provide the following methods:
| Method | Return Type | Description |
|---|---|---|
update() |
void | Polls the timer. Must be called in loop() or loop1(). |
reset() |
void | Restarts the timer to the current time and restores the original repeat count. |
stop() |
void | Pauses the timer; update() will no longer trigger the callback. |
resume() |
void | Resumes a stopped timer from its current state. |
setPeriod(val) |
void | Dynamically updates the interval duration (uint32_t). |
isRunning() |
bool | Returns true if the timer is active and has repeats remaining. |
The Safe variants utilize the RP2040's hardware critical_section to ensure that data is not corrupted when accessed simultaneously by Core 0 and Core 1. It is recommended to use the standard (non-safe) versions if you are only working on a single core to minimize CPU overhead.