CORE 1setupkeyspriority 2-z zslot 0slot 1slot 2loggerpriority 1-z z1q = xQueueCreate(3, sizeof(char));2xQueueReceive(q, &c, portMAX_DELAY);3xQueueSend(q, &c, 0)4xQueueSendFromISR(q, &c, &woken);5portYIELD_FROM_ISR(woken);onButtonGPIO 4 · ISR?7890B
Back: inside the ESP32 (core 1, the keys task, the queue's three slots, the logger task)Front: the queue calls and the button whose ISR sends into the queueTokens: the characters copied in and out of the queueA FreeRTOS queue of 3 one-byte slots between two tasks on an ESP32: the keys task (priority 2) sends the characters you type, the logger task (priority 1) receives and prints them. 7, 8 and 9 fill the queue in order and 0 is lost because the queue is full; the logger then takes 7, 8 and 9 oldest first. A button's interrupt routine sends B with xQueueSendFromISR and, because it woke a higher-priority task, returns straight into the logger.

A queue between tasks

  1. Two jobs need to pass data safely. setup() calls xQueueCreate(3, sizeof(char)): the kernel sets aside a queue of 3 slots, each 1 byte. A queue is first in, first out (FIFO): items leave in the order they came.

Sending from a task

Sending from an ISR

What happens

;; step 1/8 · create
queue    a FIFO box of fixed slots, owned by the kernel
line 1   q = xQueueCreate(3, sizeof(char));
         3 slots x 1 byte
sender   copies an item in at the back
reader   copies the oldest item out at the front

// ESP32 DevKitC, Arduino core. Serial Monitor at 115200, "No line ending".
// Type keys to send them; press the button on GPIO 4 (to GND) to send B.
QueueHandle_t q;
const int BUTTON = 4;

void keysTask(void *) {                    // priority 2
  for (;;) {
    while (Serial.available()) {
      char c = Serial.read();
      if (xQueueSend(q, &c, 0) != pdPASS)   // wait 0 ticks for space
        Serial.println("queue full, key lost");
    }
    vTaskDelay(pdMS_TO_TICKS(20));         // Blocked for 20 ms
  }
}

void loggerTask(void *) {                  // priority 1
  char c;
  for (;;) {
    xQueueReceive(q, &c, portMAX_DELAY);   // Blocked until an item comes
    Serial.printf("got %c\n", c);
  }
}

void ARDUINO_ISR_ATTR onButton() {
  char c = 'B';
  BaseType_t woken = pdFALSE;
  xQueueSendFromISR(q, &c, &woken);        // never waits
  portYIELD_FROM_ISR(woken);               // switch now if a higher task woke
}

void setup() {
  Serial.begin(115200);
  q = xQueueCreate(3, sizeof(char));
  pinMode(BUTTON, INPUT_PULLUP);
  attachInterrupt(BUTTON, onButton, FALLING);
  xTaskCreatePinnedToCore(keysTask, "keys", 4096, NULL, 2, NULL, 1);
  xTaskCreatePinnedToCore(loggerTask, "logger", 4096, NULL, 1, NULL, 1);
}

void loop() { vTaskDelay(portMAX_DELAY); }  // loop() has nothing to do

The card shows the five queue calls only. The whole sketch, under the steps, wraps them in the keys task, the logger task, the button ISR and setup(). xQueueSend returns pdPASS when the item went in. The ESP32 has two cores and FreeRTOS runs an idle task on each. The sketch pins both tasks to core 1 so one core tells the story; the button interrupt and Wi-Fi details of the second core are left out.