Home / Learn Chip Programming / RTOS Software Timers and Tick Accuracy

RTOS Software Timers and Tick Accuracy

Run functions after a delay or periodically without a task per timer.

In shortRun functions after a delay or periodically without a task per timer.

Theory in plain words

Software timerxTimerStart(t, 0)timer service task counts ticksperiod elapsescallback runs
Software timer

What it is

Software timers call a callback after a period (one-shot or auto-reload). They run in a timer service task using the tick as time base.

Why we use it

Timeouts, retries, debouncing and periodic sampling are easy with timers and save tasks (and RAM).

How it works

xTimerCreate with period in ticks, xTimerStart, callback runs in the timer task so it must not block. Tick resolution is configTICK_RATE_HZ (usually 1000).

Where it is used

  • Button debounce
  • Retry after failed connection
  • Blinking status LED pattern

Tick resolution

Period rounding with different tick ratesTry it Yourself

vTaskDelay(1) can last anywhere between 0 and 1 tick. Use a hardware timer if you need microsecond accuracy.

Auto-reload timer and one-shot
static void blink_cb(TimerHandle_t t) { toggle_led(); }            // runs in the timer task: keep it short, never block
TimerHandle_t blink = xTimerCreate("blink", pdMS_TO_TICKS(500), pdTRUE, NULL, blink_cb);   // pdTRUE = auto reload
xTimerStart(blink, 0);
TimerHandle_t once = xTimerCreate("timeout", pdMS_TO_TICKS(3000), pdFALSE, NULL, on_timeout);
xTimerReset(once, 0);                                               // restart: use as a watchdog for a protocol
Tip:Use vTaskDelayUntil for fixed-rate loops: it measures from the previous wake time, so execution time does not accumulate as drift.

Quick quiz

1. Which runs software timer callbacks?

2. vTaskDelayUntil prevents...