Most ESP32 sketches never mention which core they run on, and the Arduino framework hides it well. But the classic ESP32 has two CPU cores and runs FreeRTOS underneath every sketch, and knowing what runs where is the difference between a project that stutters whenever Wi-Fi gets busy and one that stays responsive.
Quick answer: setup() and loop() run as one FreeRTOS task (loopTask) on core 1; the Wi-Fi and Bluetooth stacks run on core 0. To run code in parallel, create a task with xTaskCreatePinnedToCore(function, "name", stackBytes, param, priority, &handle, core), give it a for (;;) loop that calls vTaskDelay(), and pass data between tasks with a queue or protect shared resources with a mutex. Keep heavy work on core 1. The ESP32-S2, C3, C6 and H2 have one core, so use xTaskCreate() on them.
This guide covers the default layout, creating and sizing tasks, priorities and the watchdog, queues and mutexes, a complete two-task example, single-core chips, and the crash messages you'll see when something's wrong.
Where Your Code Actually Runs by Default
The Arduino-ESP32 core is built on top of FreeRTOS, a real-time operating system, and every sketch is already multitasking whether you've written a single task or not. Internally, setup() and loop() are wrapped in a FreeRTOS task called loopTask, which is pinned to Core 1 (labeled APP_CPU in Espressif's documentation) by default. Core 0 (PRO_CPU) is where the WiFi and Bluetooth stacks run their own background tasks, largely invisible to your sketch.
| Core | Espressif name | Default occupant |
|---|---|---|
| Core 0 | PRO_CPU | WiFi/Bluetooth stack, system tasks |
| Core 1 | APP_CPU | Your setup()/loop() code (as loopTask) |
You can check it yourself: Serial.println(xPortGetCoreID()); inside loop() prints 1. The Arduino loopTask has priority 1 and an 8 KB stack; if loop() needs more stack, add SET_LOOP_TASK_STACK_SIZE(16 * 1024); at the top of the sketch.
This default split is exactly why a sketch that does a lot of blocking work inside loop() can still receive WiFi packets and serve HTTP requests reasonably well -- that traffic is largely being handled on the other core the whole time, mostly independent of whatever loop() is doing.
Creating Your Own Task on a Specific Core
Once a sketch needs to do two genuinely independent things at once -- read a sensor continuously while also running a slower, blocking display update, for instance -- creating a second FreeRTOS task explicitly, and pinning it to a chosen core, is the right tool.
TaskHandle_t sensorTaskHandle;
void sensorTask(void *parameter) {
for (;;) {
int value = analogRead(34);
Serial.printf("Core %d reading: %d\n", xPortGetCoreID(), value);
vTaskDelay(pdMS_TO_TICKS(200)); // yields to other tasks for 200 ms
}
}
void setup() {
Serial.begin(115200);
xTaskCreatePinnedToCore(
sensorTask, // function to run
"SensorTask", // name, for debugging
4096, // stack size in BYTES on the ESP32 (not words)
NULL, // parameter to pass in
1, // priority (0 = lowest)
&sensorTaskHandle, // task handle, for later reference
0 // core to pin to -- Core 0 here
);
}
void loop() {
// The default loopTask keeps running on Core 1, independent of sensorTask
Serial.println("Core 1 doing its own thing");
delay(500);
}With this running, the sensor reads happen on core 0 on their own schedule, independent of whatever loop() is doing on core 1. (A light, regularly yielding task like this is fine on core 0; heavy work belongs on core 1, next to loop().)
Inside an Arduino-ESP32 task, delay() is also safe: it calls vTaskDelay() internally. What you must avoid is a loop that never waits at all.
Priorities, Stack Size and the Watchdog
Priority. Higher numbers run first. A higher-priority task that's ready to run always pre-empts a lower one on the same core, and tasks of equal priority take turns. The Arduino loopTask is priority 1. Most sensor, display and logging tasks belong at 1–3; save higher priorities for short, time-critical work that then sleeps.
Stack size is in bytes on the ESP32 (vanilla FreeRTOS uses words; ESP-IDF changed this). 2048 bytes is enough only for very small tasks; anything that calls Serial.printf(), formats strings or uses a library needs 4096 or more. Measure instead of guessing: inside the task, print how much stack has never been used:
Serial.printf("%s stack free: %u bytes\n", pcTaskGetName(NULL), uxTaskGetStackHighWaterMark(NULL));If the number gets close to zero, increase the stack. If it stays in the thousands, you can shrink it and save RAM.
The task watchdog. ESP-IDF's task watchdog resets the chip if the idle task on core 0 doesn't get a chance to run for about 5 seconds. A task pinned to core 0 that loops without ever waiting starves that idle task and produces:
E (12345) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:
E (12345) task_wdt: - IDLE0 (CPU 0)In Arduino-ESP32 the core 1 idle task isn't watched by default, so the same tight loop on core 1 doesn't reset the board, but it still starves every lower-priority task on that core. The fix is the same either way: every task loop must wait somewhere, with vTaskDelay(), delay(), or a blocking queue or semaphore call.
Passing Data Between Cores Safely
A plain global variable can be read and written from tasks on different cores, but doing so without synchronization risks a race condition -- one core reading a value halfway through the other core writing it. A queue is the standard, safe way to move data between tasks, including across cores:
QueueHandle_t sensorQueue;
void producerTask(void *parameter) {
int reading;
for (;;) {
reading = analogRead(34);
xQueueSend(sensorQueue, &reading, portMAX_DELAY);
vTaskDelay(pdMS_TO_TICKS(200));
}
}
void consumerTask(void *parameter) {
int received;
for (;;) {
if (xQueueReceive(sensorQueue, &received, portMAX_DELAY)) {
Serial.printf("Got reading: %d on core %d\n", received, xPortGetCoreID());
}
}
}
void setup() {
Serial.begin(115200);
sensorQueue = xQueueCreate(10, sizeof(int));
xTaskCreatePinnedToCore(producerTask, "Producer", 4096, NULL, 1, NULL, 0);
xTaskCreatePinnedToCore(consumerTask, "Consumer", 4096, NULL, 1, NULL, 1);
}
void loop() {
vTaskDelete(NULL); // loopTask isn't needed here; both tasks run independently
}This pattern, a producer task on one core feeding a queue and a consumer on the other draining it, scales to most real designs, from sensor logging to keeping a display updated without stalling a network task. xQueueSend() copies the data into the queue, so the sender can reuse its variable straight away. Queues can carry whole structs, not just numbers:
struct Reading { float temperature; float humidity; uint32_t time; };
QueueHandle_t q = xQueueCreate(10, sizeof(Reading));Sharing a Resource Safely: Mutexes
Some things can only be used by one task at a time: the Serial port, an I2C bus, an SD card, a display. If two tasks use one at once, output gets garbled or a transaction fails. A mutex makes them take turns:
SemaphoreHandle_t i2cMutex;
void setup() {
i2cMutex = xSemaphoreCreateMutex();
// ... create the tasks
}
void readSensorSafely() {
if (xSemaphoreTake(i2cMutex, pdMS_TO_TICKS(100))) { // wait up to 100 ms for the bus
// ... Wire transactions here ...
xSemaphoreGive(i2cMutex);
} else {
Serial.println("I2C bus busy, skipped this reading");
}
}Always give the mutex back on every path, including errors, or the next task waits forever. Hold it for as short a time as possible. For a single variable such as a counter or a flag, a queue or a FreeRTOS task notification is usually simpler than a mutex.
A Complete Example: Responsive Button, Slow Network Task
A common real need: a button and LED must react instantly, while a slow network job (an HTTP upload, an MQTT publish) sometimes blocks for seconds. Give each its own task, and the button never waits for the network:
#include <WiFi.h>
const int BUTTON = 13, LED = 2;
QueueHandle_t events;
void buttonTask(void *) { // core 1: reacts within ~10 ms
bool last = HIGH;
for (;;) {
bool now = digitalRead(BUTTON);
if (last == HIGH && now == LOW) { // pressed
digitalWrite(LED, !digitalRead(LED));
uint32_t t = millis();
xQueueSend(events, &t, 0); // don't wait if the queue is full
}
last = now;
vTaskDelay(pdMS_TO_TICKS(10));
}
}
void networkTask(void *) { // core 0: may block for seconds
uint32_t t;
for (;;) {
if (xQueueReceive(events, &t, portMAX_DELAY)) {
Serial.printf("Uploading press at %lu ms...\n", (unsigned long)t);
delay(2000); // stands in for a slow HTTP or MQTT call
Serial.println("Upload done");
}
}
}
void setup() {
Serial.begin(115200);
pinMode(BUTTON, INPUT_PULLUP);
pinMode(LED, OUTPUT);
events = xQueueCreate(20, sizeof(uint32_t));
xTaskCreatePinnedToCore(buttonTask, "button", 3072, NULL, 2, NULL, 1);
xTaskCreatePinnedToCore(networkTask, "network", 8192, NULL, 1, NULL, 0);
}
void loop() { vTaskDelay(portMAX_DELAY); } // nothing left for loop() to doPress the button several times quickly: the LED toggles at once every time, while the uploads queue up and complete one by one. Network code that uses the Wi-Fi stack can run on either core; putting the waiting on core 0 keeps core 1 free for your own timing-sensitive work.
Single-Core Chips: ESP32-S2, C3, C6 and H2
| Chip | Cores | How to create a task |
|---|---|---|
| ESP32, ESP32-S3 | 2 | xTaskCreatePinnedToCore(..., 0 or 1) or xTaskCreate() |
| ESP32-S2, C3, C6, H2 | 1 | xTaskCreate(), or pin to core 0 |
Tasks, queues and mutexes work exactly the same on single-core chips; they simply take turns on one CPU, so yielding with vTaskDelay() matters even more. Pinning to core 1 on a single-core chip fails, so write portable code like this:
#if CONFIG_FREERTOS_UNICORE
xTaskCreate(sensorTask, "sensor", 4096, NULL, 1, NULL);
#else
xTaskCreatePinnedToCore(sensorTask, "sensor", 4096, NULL, 1, NULL, 1);
#endifCommon Mistakes
Assuming loop() itself runs on both cores. It doesn't -- it's a single FreeRTOS task pinned to Core 1. Genuine parallelism requires creating additional tasks explicitly.
Sharing a variable across cores with no synchronization. Works most of the time in testing, then fails intermittently in the field -- exactly the signature of a race condition. Use a queue, a mutex, or at minimum a volatile qualifier plus careful reasoning about atomicity.
Writing an infinite task loop with no yield. Missing vTaskDelay() (or equivalent) trips the watchdog and reboots the chip, often reported as "random ESP32 crashes" without an obvious cause.
Over-prioritizing tasks. A high-priority task that rarely yields can starve lower-priority tasks on the same core, including system tasks -- default to low priorities unless a task genuinely needs to preempt others.
Putting heavy blocking work on Core 0 unnecessarily. Core 0 is already carrying the WiFi/Bluetooth stack; piling a slow, blocking task onto the same core can degrade network responsiveness even though your sketch code appears to be running elsewhere.
Reading ESP32 Crash Messages
| Message | Meaning | Fix |
|---|---|---|
task_wdt: Task watchdog got triggered ... IDLE0 (CPU 0) | A task on core 0 never yields | Add vTaskDelay() to its loop, or move it to core 1 |
***ERROR*** A stack overflow in task X has been detected | Task X's stack is too small | Double its stack size; check with uxTaskGetStackHighWaterMark() |
Guru Meditation Error: Core 1 panic'ed (LoadProhibited) | A null or invalid pointer, often an uncreated queue or handle | Create queues and mutexes in setup() before starting the tasks that use them |
assert failed: xQueueReceive ... (( pxQueue )) | Using a queue whose handle is NULL | Same as above; check xQueueCreate() returned non-NULL |
| Garbled Serial output | Two tasks printing at once | Protect Serial with a mutex, or print from one task only |
| Task never runs | Task function returned, or creation failed | Task functions must loop forever or end with vTaskDelete(NULL); check the xTaskCreate... return value (pdPASS) |
FAQ
Does loop() run on both cores? No. It's one task on core 1. Parallelism only comes from tasks you create.
Which core should I use for my task? Core 1 for heavy or timing-sensitive work, next to loop(). Core 0 is fine for light tasks that wait often, and for code that mostly waits on the network.
Is delay() safe inside a task? Yes on Arduino-ESP32: it yields like vTaskDelay(). Don't use busy-wait loops such as while (millis() - t < 100) {}.
How do I stop or delete a task? Call vTaskDelete(handle) from another task, or vTaskDelete(NULL) at the end of the task itself. Never just return from a task function.
Does using both cores use more power? Only while both are busy. Tasks waiting in vTaskDelay() or on a queue use no CPU. For battery projects, see the ESP32 deep sleep guide.
Can I use tasks in MicroPython? MicroPython runs on one core and uses asyncio for concurrency instead; see the asyncio reconnect example in ESP32 Wi-Fi with MicroPython.
Where to Go Next
- ESP32 Wi-Fi troubleshooting: a reconnect watchdog that fits naturally in its own task
- ESP32 HTTP GET and POST: slow network calls worth moving off your main loop
- ESP32 I2C pins and multiple devices: sharing one bus between tasks with a mutex
- ESP32 complete beginner's guide: the basics this builds on




