FreeRTOS Tutorial on ESP32: Tasks, Queues, Semaphores and a Worked Example
A fresher's FreeRTOS tutorial on the ESP32 with ESP-IDF v6.1.1: why an RTOS beats a super-loop, tasks, priorities and the 100 Hz tick, the dual-core scheduler, xTaskCreate vs xTaskCreatePinnedToCore, vTaskDelay vs xTaskDelayUntil, queues, ISR-to-task semaphores, mutexes and the Mars Pathfinder priority inversion, stack sizing, the task watchdog, and a complete C example checked against the FreeRTOS V11.3.3 and ESP-IDF APIs.

FreeRTOS is the real-time operating system built into ESP-IDF, Espressif's official framework for the ESP32, and into the Arduino-ESP32 core. It lets you split firmware into tasks with priorities; the kernel always runs the highest-priority task that is ready, preempting lower ones, on both of the ESP32's cores. This tutorial takes you from a super-loop to a working RTOS design: tasks and the tick, creating tasks, delays, queues, semaphores for interrupts, mutexes and priority inversion, software timers, stack sizing and the task watchdog, ending with a complete C example.
It is written for students and freshers in India who have an ESP32 board and have only written loop-style sketches so far. Every function name and signature below was checked against the FreeRTOS kernel V11.3.3 header files and the ESP-IDF v6.1.1 documentation and sources on 29 November 2026, and the example was syntax-checked with clang against those prototypes. We have not built it with ESP-IDF or run it on a board, so treat it as a checked starting point, not a tested product.
In short
- Tick rate: ESP-IDF's default is 100 Hz, a 10 ms tick (CONFIG_FREERTOS_HZ, Kconfig default 100). The Arduino-ESP32 core is built with 1000 Hz.
pdMS_TO_TICKS(25)is 2 ticks = 20 ms at 100 Hz, because the macro truncates.- Priorities: 25 levels, 0 (idle) to 24 (configMAX_PRIORITIES = 25).
app_main()runs in a task at priority 1 with 3,584 bytes of stack by default.- Stack units: bytes on ESP-IDF, words on vanilla FreeRTOS.
xTaskCreate(..., 2048, ...)reserves 2,048 bytes on the ESP32 but 8,192 bytes on a vanilla 32-bit port.uxTaskGetStackHighWaterMark()also returns bytes on ESP-IDF.- Two cores: IDF FreeRTOS (based on vanilla v10.5.1) schedules each core independently. Core 0 keeps time. An unpinned task may run on either core; pin it with
xTaskCreatePinnedToCore().- Locks: a mutex has priority inheritance and cannot be used in an ISR; a binary semaphore has no inheritance and is the right tool for ISR-to-task signalling.
- Task watchdog: 5 s by default, watching both idle tasks. A task that spins without blocking triggers it.
Why use an RTOS instead of a super-loop on the ESP32?
A super-loop runs every job in turn inside one while (1). It works until one job must wait: a 200 ms sensor settle time, a Wi-Fi reconnect, a blocking read. Then every other job waits too, and a button press during that wait is lost or late. An RTOS gives each job its own task with its own stack and priority. A task that waits is put in the Blocked state and costs no CPU; the kernel runs whichever ready task has the highest priority, so a button task answers as soon as its interrupt fires while a sensor loop and Wi-Fi carry on underneath. Queues, semaphores and mutexes replace the global flags that make super-loops racy, and xTaskDelayUntil() gives you a fixed period without counting cycles.
On the ESP32 you have no real choice anyway: ESP-IDF's Wi-Fi, Bluetooth and TCP/IP stacks are FreeRTOS tasks, and app_main() itself already runs inside one. If you know bare-metal programming from our STM32 bare-metal guide, this is the next layer up.
What are tasks, priorities and the tick in FreeRTOS?
A task is a C function with an infinite loop, its own stack and a priority. At any moment it is Running, Ready (could run but a higher-priority task is running), Blocked (waiting for a delay, queue, semaphore or notification) or Suspended. FreeRTOS is a fixed-priority preemptive scheduler: the moment a higher-priority task becomes ready, for example when an ISR gives it a semaphore, it runs, and the lower task resumes later exactly where it stopped. Tasks of equal priority share the CPU in round-robin time slices.
The tick is a periodic timer interrupt that advances the kernel's clock, wakes tasks whose delays have expired and switches between equal-priority tasks. It is not what makes preemption happen: an interrupt can preempt between ticks. On ESP-IDF the tick rate is CONFIG_FREERTOS_HZ, default 100 Hz, so one tick is 10 ms. Every timeout you pass to the kernel is in ticks, and the conversion macro rounds down:
At 100 Hz, pdMS_TO_TICKS(25) is 2 ticks (20 ms) and pdMS_TO_TICKS(5) is 0, so vTaskDelay(pdMS_TO_TICKS(5)) does not delay at all. If you need millisecond timing, raise the tick rate in menuconfig (the Kconfig range is 1 to 1,000 Hz) or use a hardware timer for the fine part.
ESP-IDF sets configMAX_PRIORITIES to 25, so you can use priorities 0 to 24. The idle task sits at 0, one per core. The main task that calls app_main() runs at priority 1 (one above the minimum) with a 3,584-byte stack by default, pinned to core 0; the software-timer service task also defaults to priority 1. A sensible fresher layout is 2 to 5 for background and logging work, 5 to 10 for control loops, and above 10 only for tasks woken by interrupts that must respond at once.
xTaskDelayUntil(). Illustrative durations, schedule checked in calc.py; in the example the logger is unpinned, so this is the case where it happens to be on core 1 with the button task.How does the ESP32's dual-core scheduler differ from vanilla FreeRTOS?
IDF FreeRTOS is Espressif's symmetric-multiprocessing (SMP) fork, based on vanilla FreeRTOS v10.5.1. Each core runs the scheduler independently and picks the highest-priority ready task that is allowed on that core, either pinned to it or unpinned. Four facts from Espressif's documentation matter in practice:
- When an unpinned task becomes ready with a higher priority than what both cores are running, the scheduler preempts only the core it is currently running on; the lower-priority task on the other core keeps going.
- Round robin between equal-priority tasks is "best effort". For exact round robin, pin all tasks of one priority to the same core.
- Both cores take a tick interrupt of the same period, possibly out of phase, but only core 0 advances the tick count. Suspending the scheduler on core 0 makes the whole system's timekeeping lag.
- Disabling interrupts is not a critical section on an SMP system, because the other core keeps running. ESP-IDF critical sections take a spinlock:
taskENTER_CRITICAL(&my_spinlock).
With CONFIG_FREERTOS_UNICORE set, ESP-IDF builds a single-core kernel, which is what the single-core ESP chips use.
How do you create a task: xTaskCreate or xTaskCreatePinnedToCore?
Both create a task from a function. xTaskCreate() is the standard FreeRTOS call; on ESP-IDF it creates an unpinned task that may run on either core. xTaskCreatePinnedToCore() adds a final xCoreID argument: 0, 1 or tskNO_AFFINITY. Pin a task when it touches something core-specific, when you want predictable round robin, or when you want a control loop kept away from the Wi-Fi task, which ESP-IDF pins to core 0 by default.
// FreeRTOS-Kernel V11.3.3, task.h
BaseType_t xTaskCreate( TaskFunction_t pxTaskCode, const char * const pcName,
const configSTACK_DEPTH_TYPE uxStackDepth, void * const pvParameters,
UBaseType_t uxPriority, TaskHandle_t * const pxCreatedTask );
// ESP-IDF v6.1.1, idf_additions.h
BaseType_t xTaskCreatePinnedToCore( TaskFunction_t pxTaskCode, const char * const pcName,
const uint32_t ulStackDepth, void * const pvParameters,
UBaseType_t uxPriority, TaskHandle_t * const pxCreatedTask,
const BaseType_t xCoreID );
Both return pdPASS on success. The task function takes one void * and must never return; a task that has finished calls vTaskDelete(NULL). The one trap for anyone copying freertos.org examples is the stack unit: vanilla FreeRTOS counts the stack in words ("the number of words, not bytes"), ESP-IDF counts it in bytes, so the same literal 2048 means 2,048 bytes on the ESP32 and 8,192 bytes on a 32-bit vanilla port. Unlike a normal FreeRTOS main(), app_main() may return after creating tasks; the scheduler is already running.
vTaskDelay vs xTaskDelayUntil: which should a periodic task use?
vTaskDelay(ticks) blocks for a number of ticks counted from the moment it is called. xTaskDelayUntil(&last_wake, period) blocks until an absolute time, last_wake + period, and updates last_wake for you. Use vTaskDelay() for "wait a bit" and xTaskDelayUntil() for anything that must run at a fixed rate: sampling, control loops, heartbeats.
The difference is drift. Suppose a sampling task does 3 ms of work and then calls vTaskDelay(pdMS_TO_TICKS(100)) on a 1 ms tick. Its real period is about 103 ms, so in an hour it takes 34,951 samples instead of 36,000 and its schedule slips by . With xTaskDelayUntil() the wake times stay on the 100 ms grid as long as the work fits inside the period. If the task falls behind, xTaskDelayUntil() returns pdFALSE instead of blocking, so it catches up rather than stacking up delays; the older vTaskDelayUntil() is now a macro that throws that return value away.
TickType_t last_wake = xTaskGetTickCount();
for (;;) {
do_work(); // must take less than the period
xTaskDelayUntil(&last_wake, pdMS_TO_TICKS(100));
}
How do queues pass data between tasks?
A queue is a fixed-length FIFO of fixed-size items. xQueueCreate(length, item_size) allocates it; xQueueSend(q, &item, ticks_to_wait) copies an item in; xQueueReceive(q, &buf, ticks_to_wait) copies one out. The kernel copies by value, not by reference, which is why queues are safe: the producer can reuse its local variable the moment xQueueSend() returns. A receiver that passes portMAX_DELAY blocks for ever at zero CPU cost until something arrives; a sender that passes 0 returns errQUEUE_FULL at once instead of waiting.
Size the queue from the rates. Our example posts a 12-byte sample_t (a uint32_t time stamp, a float and a uint8_t channel, padded to 12 bytes) every 100 ms into a queue of 8: 96 bytes of item storage, buffering 800 ms of samples if the logger stalls on a slow UART. Queues can also be written from ISRs with xQueueSendFromISR(); for variable-length data such as UART bursts, ESP-IDF's ring buffers are the better tool.
How does an interrupt wake a task? Binary semaphores and xSemaphoreGiveFromISR
An ISR should do the minimum and hand the rest to a task. The FreeRTOS pattern is a binary semaphore: the task blocks in xSemaphoreTake(sem, portMAX_DELAY); the ISR calls xSemaphoreGiveFromISR(sem, &hp_woken) and then portYIELD_FROM_ISR(hp_woken). If giving the semaphore unblocked a task with a higher priority than the one that was interrupted, the kernel sets hp_woken to pdTRUE and the yield switches to that task as soon as the ISR returns, with no wait for the next tick. On ESP-IDF's Xtensa port portYIELD_FROM_ISR() takes the flag as an optional argument.
Two details trip up beginners. A semaphore from xSemaphoreCreateBinary() starts empty: it must be given before it can be taken, which is what you want for an event. And never call the ordinary xSemaphoreGive(), xQueueSend() or anything that can block inside an ISR; only the ...FromISR variants are interrupt-safe. On ESP-IDF, per-pin GPIO handlers registered with gpio_isr_handler_add() do not need IRAM_ATTR unless you pass ESP_INTR_FLAG_IRAM to gpio_install_isr_service(). Task notifications (vTaskNotifyGiveFromISR()) do the same job with less RAM when only one task waits; learn the semaphore first, because interviewers ask about it.
What is a mutex, and what is priority inversion?
A mutex (mutual exclusion) protects a resource that only one task may use at a time: a UART, an SPI bus, a shared data structure. A task takes it before the critical section and gives it back after; a second task that tries to take it blocks until then. FreeRTOS mutexes come from xSemaphoreCreateMutex() and, unlike binary semaphores, include a priority inheritance mechanism. The kernel's header puts the rules bluntly: a task that takes a mutex "MUST ALWAYS" give it back, and mutexes "cannot be used from within interrupt service routines".
Priority inversion is the failure that inheritance limits. A low-priority task L holds the mutex. A high-priority task H needs it and blocks, which is expected and short. Then a medium-priority task M, which has nothing to do with the mutex, becomes ready. M preempts L, so L cannot finish and release, so H, the most important task in the system, waits for as long as M wants to run. The priority order has been inverted. With inheritance, the kernel raises L to H's priority while H waits on the mutex L holds; M can no longer preempt L, L finishes its short critical section, and H's wait is bounded by that section rather than by M's work.
The classic case is NASA's Mars Pathfinder, which landed on 4 July 1997. A few days into the mission the lander began resetting itself and losing data. Glenn Reeves, who led the flight software team at JPL, described the cause: under the VxWorks RTOS, a low-priority meteorological task (ASI/MET) held a mutual-exclusion semaphore inside the select() mechanism, was preempted by several medium-priority tasks, and so blocked the much higher-priority bus-distribution task; when the top-priority bus scheduler found that task had not completed its cycle it declared an error, and the spacecraft reset its computer. The team reproduced the fault on the lab testbed in under 18 hours, found that the priority inheritance option had been left off in the default select library ("We just missed it"), changed a global variable so the semaphores were created with inheritance, tested, and patched the spacecraft. The theory behind that fix is the 1990 paper by Sha, Rajkumar and Lehoczky, which defined the basic priority inheritance protocol that FreeRTOS mutexes use, and the stricter priority ceiling protocol. FreeRTOS's own documentation warns that inheritance does not cure priority inversion, it only bounds it, so keep critical sections short.
Two rules follow. Protect shared resources with a mutex, not a binary semaphore and never a global flag. And never hold a mutex across a blocking call or a long computation: copy what you need out, give it back, then work.
What are software timers for?
A software timer runs a callback after a period, once or repeatedly, without a task of its own. xTimerCreate(name, period_ticks, auto_reload, timer_id, callback) makes one in the dormant state and xTimerStart(timer, ticks_to_wait) starts it. The callback, void cb(TimerHandle_t xTimer), runs in the timer service task (the daemon task), whose priority on ESP-IDF defaults to 1. So a callback must never block (the kernel's own examples warn that a blocking timer API call inside a callback can deadlock), and a long callback delays every other timer. Use timers for LED patterns, debounce timeouts and housekeeping; use a task with xTaskDelayUntil() for real work.
How big should a task's stack be? Using uxTaskGetStackHighWaterMark
Measure, don't guess. uxTaskGetStackHighWaterMark(handle) (pass NULL for the calling task) returns the smallest amount of free stack the task has had since it started: in bytes on ESP-IDF, in words on vanilla FreeRTOS. If a 4,096-byte task reports a low point of 1,200 bytes after every code path has run, its peak use was 2,896 bytes, 71% of the stack. Leave headroom for the paths you did not exercise. (On ESP-IDF, interrupt handlers run on a separate per-core ISR stack, 1,536 bytes by default, so they do not eat into a task's figure.) Formatted logging of floats through ESP_LOGI() is the usual surprise, which is why the logger task in the example gets 4,096 bytes and the others 3,072. ESP-IDF enables stack-overflow checking by default (canary bytes, FreeRTOS method 2), so an overflow ends in a panic that names the problem instead of the silent corruption you would get bare-metal. The idle task's stack defaults to 1,536 bytes, so idle hooks must stay tiny.
What does the task watchdog do, and why does it fire on my first project?
The Task Watchdog Timer (TWDT) catches tasks that run without yielding for too long. By default it is enabled at start-up with a 5 s timeout and subscribed to the idle task of each core. If anything stops an idle task from running for 5 s, which is exactly what a while (1) {} or a busy-poll with no vTaskDelay() does, the watchdog prints "Task watchdog got triggered" with a backtrace, the tasks that did not reset it, for example IDLE0 (CPU 0), and the tasks running on each CPU. It resets the chip only if CONFIG_ESP_TASK_WDT_PANIC is set; the Arduino core builds with that option on.
The fix is almost always to block properly: a delay, a queue receive or a semaphore take inside every loop. For a task that legitimately runs long, subscribe it with esp_task_wdt_add(NULL) and call esp_task_wdt_reset() in its loop, or change the timeout with esp_task_wdt_init() and an esp_task_wdt_config_t (timeout_ms, idle_core_mask, trigger_panic). A separate Interrupt Watchdog, fed by the tick interrupt, fires after 300 ms (800 ms with PSRAM) if interrupts stay disabled or an ISR spins that long.
A complete example: sensor task, queue, logger and a button ISR
This ESP-IDF program ties everything together. A sensor task at priority 5 samples every 100 ms and posts to a queue. A logger at priority 2 blocks on the queue and prints each sample. The BOOT button (GPIO 0, active low) raises an interrupt that gives a binary semaphore; a button task at priority 6, pinned to core 1, takes it and prints its own stack high-water mark. A mutex serialises the two tasks' ESP_LOGI() calls. Replace read_sensor() with a real driver call. The file and the stub headers used for the syntax check are kept in the run folder for this post.
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "esp_err.h"
#define BUTTON_GPIO GPIO_NUM_0 /* BOOT button on most ESP32 dev boards, active low */
#define SAMPLE_PERIOD_MS 100
#define QUEUE_LEN 8
static const char *TAG = "rtos-demo";
typedef struct {
uint32_t t_ms; /* time stamp from the tick count */
float value; /* sensor reading */
uint8_t channel;
} sample_t; /* 12 bytes on a 32-bit ABI (4 + 4 + 1, padded) */
static QueueHandle_t s_sample_q;
static SemaphoreHandle_t s_button_sem;
static SemaphoreHandle_t s_log_mutex;
/* Stand-in for a real driver call (ADC read, I2C sensor...). */
static float read_sensor(void)
{
static float x = 25.0f;
x += 0.1f;
return x;
}
/* Priority 5. Fixed 100 ms period with xTaskDelayUntil(): no drift from the work done each cycle. */
static void sensor_task(void *arg)
{
(void)arg;
TickType_t last_wake = xTaskGetTickCount();
for (;;) {
sample_t s = {
.t_ms = xTaskGetTickCount() * portTICK_PERIOD_MS,
.value = read_sensor(),
.channel = 0,
};
if (xQueueSend(s_sample_q, &s, 0) != pdPASS) {
/* Queue full: the logger has fallen 8 samples behind. Drop and count it in a real design. */
}
xTaskDelayUntil(&last_wake, pdMS_TO_TICKS(SAMPLE_PERIOD_MS));
}
}
/* Priority 2. Blocks (uses no CPU) until a sample arrives. */
static void logger_task(void *arg)
{
(void)arg;
sample_t s;
for (;;) {
if (xQueueReceive(s_sample_q, &s, portMAX_DELAY) == pdTRUE) {
xSemaphoreTake(s_log_mutex, portMAX_DELAY);
ESP_LOGI(TAG, "t=%lu ms ch%u value=%.2f", (unsigned long)s.t_ms, (unsigned)s.channel, (double)s.value);
xSemaphoreGive(s_log_mutex);
}
}
}
/* ISR: no printing, no blocking, no mutex. Give the semaphore and ask for a context switch if needed. */
static void button_isr(void *arg)
{
(void)arg;
BaseType_t hp_woken = pdFALSE;
xSemaphoreGiveFromISR(s_button_sem, &hp_woken);
portYIELD_FROM_ISR(hp_woken);
}
/* Priority 6, pinned to core 1. Runs only after the ISR gives the semaphore. */
static void button_task(void *arg)
{
(void)arg;
for (;;) {
if (xSemaphoreTake(s_button_sem, portMAX_DELAY) == pdTRUE) {
xSemaphoreTake(s_log_mutex, portMAX_DELAY);
ESP_LOGI(TAG, "button pressed; this task's free stack low point: %u bytes",
(unsigned)uxTaskGetStackHighWaterMark(NULL));
xSemaphoreGive(s_log_mutex);
vTaskDelay(pdMS_TO_TICKS(50)); /* crude debounce: ignore bounces for 50 ms */
}
}
}
void app_main(void)
{
s_sample_q = xQueueCreate(QUEUE_LEN, sizeof(sample_t)); /* copies 12-byte items */
s_button_sem = xSemaphoreCreateBinary(); /* starts empty: must be given first */
s_log_mutex = xSemaphoreCreateMutex(); /* priority inheritance built in */
configASSERT(s_sample_q != NULL && s_button_sem != NULL && s_log_mutex != NULL);
gpio_config_t io = {
.pin_bit_mask = 1ULL << BUTTON_GPIO,
.mode = GPIO_MODE_INPUT,
.pull_up_en = GPIO_PULLUP_ENABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_NEGEDGE, /* falling edge = press on an active-low button */
};
ESP_ERROR_CHECK(gpio_config(&io));
ESP_ERROR_CHECK(gpio_install_isr_service(0));
ESP_ERROR_CHECK(gpio_isr_handler_add(BUTTON_GPIO, button_isr, NULL));
/* Stack sizes are in BYTES on ESP-IDF (words on vanilla FreeRTOS). */
xTaskCreate(sensor_task, "sensor", 3072, NULL, 5, NULL);
xTaskCreate(logger_task, "logger", 4096, NULL, 2, NULL);
xTaskCreatePinnedToCore(button_task, "button", 3072, NULL, 6, NULL, 1);
/* app_main may return on ESP-IDF; the tasks keep running under the scheduler. */
}
Reading the design: the sensor never waits for the logger, because the queue decouples them and a full queue returns at once with a wait of 0. The logger uses no CPU between samples. The button path is ISR, semaphore, task, the only safe way to do work from an interrupt. The mutex wraps the log call only, so no task holds it for more than one line of output. To build it, drop it into the main/ folder of an ESP-IDF hello_world project as main.c, run idf.py set-target esp32 and idf.py build flash monitor, and press BOOT. At the default 100 Hz tick the 50 ms debounce is 5 ticks.
Can I use FreeRTOS from the Arduino IDE on the ESP32?
Yes. The Arduino-ESP32 core (release 4.0.0 at the time of writing) is built on ESP-IDF, so every API above is available in a sketch with the same headers. setup() and loop() run inside a FreeRTOS task named loopTask, created at priority 1 with an 8,192-byte stack, on core 1 by default. The core's prebuilt libraries are configured with a 1,000 Hz tick, so pdMS_TO_TICKS() is exact to the millisecond, and with the task watchdog set to panic on timeout. Those settings are fixed unless you rebuild the core with Espressif's lib-builder. For learning and prototypes, Arduino is fine; for a product, ESP-IDF gives you menuconfig and the debugger.
Where does this take you? RTOS skills and embedded jobs in India
"RTOS" appears in almost every Indian embedded job description, from automotive ECUs and BMS firmware to IoT gateways and medical devices, and FreeRTOS tasks, queues and priority inversion are stock interview questions alongside the C topics in our embedded C interview questions. The same ideas transfer to Zephyr, ThreadX and AUTOSAR OS, and they underpin the protocol work in our CAN and UDS diagnostics posts, where a bus task, a diagnostic task and an application task share one controller.
Skill-Lync's own placement records give a sense of the starting point. Based on 3,711 Skill-Lync placement records up to 7 November 2026 (data snapshot of 15 November 2026), the Embedded track has n = 278 placements with a median starting offer of 4.0 LPA (middle half 3.5 to 5.0 LPA). The most frequent employers were Cyient (n = 18), Nexteer Automotive (n = 17), Expleo Engineering (n = 9) and Tata Technologies (n = 8). This is a self-selected sample of learners who reported a placement, mostly 2021-23 starting offers, with thin recent years; read it as a pointer, not a market survey. Our embedded engineer salary page has the breakdown by role and year.
To build the base around this tutorial, Skill-Lync's Fundamentals of Embedded Systems covers microcontroller architecture and peripherals, Embedded Programming Essentials covers the C you need for tasks and ISRs, and the PG Program in Embedded Systems for EV Applications takes RTOS work into automotive projects; Programming the Internet of Things is where the ESP32 itself gets a workout. For theory, unit 14 of our free microcontrollers book introduces real-time operating systems, and our embedded systems architecture post shows where the RTOS sits in the firmware stack.
What is the catch?
- Not run on hardware. The example was checked against the kernel headers and ESP-IDF documentation and syntax-checked with clang, not built with ESP-IDF or flashed. Expect to adjust log formats, stack sizes and the GPIO for your board.
- Versions move. Facts here are for FreeRTOS-Kernel V11.3.3 and ESP-IDF v6.1.1, whose kernel is based on vanilla v10.5.1. Every call used in this post exists in both, but check the documentation for the version you actually build with.
- An RTOS adds its own bugs. Deadlocks (two tasks each holding the mutex the other wants), starvation (a priority-10 task that never blocks) and stack overflows replace the super-loop's timing bugs. The watchdog and the high-water mark exist because these happen to everyone.
- Priority inheritance is a bound, not a cure. A mutex held across a 50 ms I2C transaction still makes a higher task wait 50 ms.
Frequently asked questions
Is FreeRTOS already included in the ESP32, or do I install it?
It is included. ESP-IDF ships IDF FreeRTOS, an SMP fork of FreeRTOS v10.5.1, and starts the scheduler before app_main() runs. The Arduino-ESP32 core is built on ESP-IDF, so a sketch can call xTaskCreate() after including freertos/FreeRTOS.h and freertos/task.h; nothing extra is installed.
What is the default FreeRTOS tick rate on the ESP32?
100 Hz, a 10 ms tick, set by CONFIG_FREERTOS_HZ in ESP-IDF v6.1.1 and adjustable from 1 to 1,000 Hz in menuconfig. The Arduino-ESP32 core is prebuilt with 1,000 Hz. Because pdMS_TO_TICKS() truncates, delays shorter than one tick round down to zero at 100 Hz.
What is the difference between a binary semaphore and a mutex in FreeRTOS?
A mutex has priority inheritance, must be given back by the task that took it, and cannot be used from an ISR; use it to protect a shared resource. A binary semaphore has no inheritance, starts empty, and can be given from an ISR with xSemaphoreGiveFromISR(); use it to signal an event from an interrupt or another task.
Why does my ESP32 print "Task watchdog got triggered"?
A task ran for more than 5 s (the default) without blocking, so the idle task on that core never ran and could not feed the watchdog. Add a vTaskDelay(), a queue receive or a semaphore take to the loop, or subscribe the task with esp_task_wdt_add() and call esp_task_wdt_reset() from it.
Should I pin tasks to a core on the ESP32?
Pin when it helps predictability: keep a control loop on core 1 away from Wi-Fi on core 0, keep equal-priority tasks on one core for exact round robin, or satisfy a driver that expects one core. Otherwise leave tasks unpinned with xTaskCreate() and let the scheduler use both cores.
What was the Mars Pathfinder priority inversion?
Days after landing on 4 July 1997, the Pathfinder lander reset itself repeatedly because a low-priority weather task holding a VxWorks mutex was preempted by medium-priority tasks, blocking a high-priority bus task; the system detected the missed cycle and reset. JPL reproduced it in the lab and fixed it by enabling priority inheritance on the mutex, which is what a FreeRTOS mutex does by default.
Sources and further reading
- FreeRTOS, FreeRTOS-Kernel V11.3.3, include/task.h (xTaskCreate, vTaskDelay, xTaskDelayUntil, uxTaskGetStackHighWaterMark), queue.h, semphr.h and timers.h (accessed 29 Nov 2026).
- FreeRTOS, xTaskCreate API reference and xSemaphoreCreateMutex API reference, freertos.org.
- Espressif, ESP-IDF Programming Guide v6.1.1: FreeRTOS (IDF): SMP scheduler, tick, stack units, critical sections, API reference.
- Espressif, ESP-IDF v6.1.1: Watchdogs (Task and Interrupt Watchdog Timers).
- Espressif, ESP-IDF v6.1.1: GPIO & RTC GPIO and Application Startup Flow.
- Espressif, ESP-IDF v6.1.1 sources on GitHub: components/freertos/Kconfig (FREERTOS_HZ default 100), components/esp_system/Kconfig (watchdog and main-task defaults), FreeRTOSConfig.h (configMAX_PRIORITIES 25) and idf_additions.h (xTaskCreatePinnedToCore).
- Espressif, arduino-esp32 cores/esp32/main.cpp, Kconfig.projbuild and esp32-arduino-lib-builder defconfig.common (loopTask, CONFIG_FREERTOS_HZ=1000).
- G. E. Reeves (JPL), "What really happened on Mars?", e-mail of 15 December 1997 (copy hosted by UNC).
- M. Jones, "What really happened on Mars Rover Pathfinder", RISKS Digest 19.49, 7 December 1997.
- NASA Science, Mars Pathfinder mission page: landing 4 July 1997, final transmission 27 September 1997.
- L. Sha, R. Rajkumar and J. P. Lehoczky, "Priority Inheritance Protocols: An Approach to Real-Time Synchronization", IEEE Transactions on Computers, vol. 39, no. 9, pp. 1175-1185, September 1990.
- Skill-Lync placement records, data snapshot of 15 November 2026 (3,711 records up to 7 November 2026), Embedded track.


