FreeRTOS on the ESP32 with no library
The ESP32's Arduino core runs on FreeRTOS: a sampler task, a queue, a dimmer task and a software timer, with nothing to install.
RTOS · Intermediate
In a real product: Smart speaker (Consumer electronics): Separate tasks sample, process and output at their own rates.
On an Uno, FreeRTOS is a library you add. On an ESP32 it is the foundation: the Arduino core itself is built on FreeRTOS, and setup() and loop() already run inside a task. So the whole FreeRTOS API is there without a libraries.txt or an #include.
What you'll learn
- That
loop()on an ESP32 is just one FreeRTOS task among others - Tasks and queues with the ESP32's bigger stacks (in bytes, not words)
- Software timers, which run a callback periodically without a task of their own
- Printing task information such as the stack high-water mark
How it works
sampler reads the potentiometer on GPIO 1 every 50 ms and sends the reading into a queue. dimmer waits on the queue and sets the brightness of the LED on GPIO 10. This is the same design as the Uno queue project, with two differences: no include is needed, and the stack size passed to xTaskCreate is in bytes on the ESP32 (2048 here), where the AVR port counts words.
A software timer, heartbeat, fires every second. Its callback runs in FreeRTOS's timer service task, so it must be short and must never block. It prints the uptime, the latest reading and how much of the dimmer's stack has never been used (uxTaskGetStackHighWaterMark). That figure is how you size stacks on real projects: start generous, measure, then trim.
loop() deletes its own task with vTaskDelete(NULL), since all the work happens elsewhere.
Try this
- The Tasks timeline panel works on the Uno, Nano and Mega, where the service can hook the FreeRTOS kernel; the ESP32's kernel comes precompiled. Open the Uno queue project to see the same design as a timeline.
- Lower the dimmer's stack to 1024 bytes and watch the high-water mark.
- Change the timer to 250 ms and make it one-shot (
pdFALSE), restarting it from the sampler when the knob moves. - Pin the two tasks to a core with
xTaskCreatePinnedToCore. Which ESP32 boards have a second core? - Open the Uno queue project and compare the two sketches line by line.
sketch.ino
// FreeRTOS is built into the ESP32 Arduino core: tasks, a queue and a software timer, with nothing to include.
const int KNOB = 1; // GPIO 1, an ADC input on the ESP32-C3
const int LED = 10;
QueueHandle_t readings;
TaskHandle_t dimmerTask;
volatile int latest = 0;
void sampler(void *) {
for (;;) {
int value = analogRead(KNOB); // 0..4095 on the ESP32
xQueueSend(readings, &value, pdMS_TO_TICKS(10));
vTaskDelay(pdMS_TO_TICKS(50));
}
}
void dimmer(void *) {
pinMode(LED, OUTPUT);
int value;
for (;;) {
if (xQueueReceive(readings, &value, portMAX_DELAY) == pdTRUE) {
latest = value;
analogWrite(LED, value / 16); // 0..255
}
}
}
// Runs in the timer service task: keep it short and never block.
void heartbeat(TimerHandle_t) {
Serial.printf("uptime %lu s, knob %d, dimmer stack unused %u bytes\n",
millis() / 1000, latest, (unsigned)uxTaskGetStackHighWaterMark(dimmerTask));
}
void setup() {
Serial.begin(115200);
readings = xQueueCreate(4, sizeof(int));
// Stack sizes are in bytes on the ESP32.
xTaskCreate(sampler, "sampler", 2048, NULL, 1, NULL);
xTaskCreate(dimmer, "dimmer", 2048, NULL, 2, &dimmerTask);
TimerHandle_t timer = xTimerCreate("heartbeat", pdMS_TO_TICKS(1000), pdTRUE, NULL, heartbeat);
xTimerStart(timer, 0);
}
void loop() {
vTaskDelete(NULL); // loop() is a task too; it has nothing to do, so it ends itself
}