FreeRTOS queue from a sensor task to an output task
One task samples a potentiometer and sends readings through a queue; another receives them and dims an LED.
RTOS · Intermediate
In a real product: Smart lighting controller (Smart home): A sensor task feeds an output task through a queue.
Tasks shouldn't share variables carelessly. The RTOS way to pass data from one task to another is a queue: the producer copies a value in, the consumer takes it out, and the queue handles the locking and the waiting. This project splits a dimmer into a sensor task and an output task joined by a queue.
What you'll learn
- Creating a queue with
xQueueCreate, and sending and receiving withxQueueSendandxQueueReceive - Blocking on a queue: a task that waits for data uses no CPU until the data arrives
- Decoupling the rate data is produced from the rate it is used
- Choosing what to do when a queue is full
How it works
sampler reads the potentiometer on A0 every 50 ms and sends each reading into readings, a queue that holds up to 4 int values. If the queue is full, it waits up to 10 ms and then drops the reading, so a slow consumer can't stall the sampler forever.
dimmer blocks in xQueueReceive with portMAX_DELAY: it sleeps until a reading arrives. Then it sets the LED's brightness with analogWrite on pin 9 and prints the value when it has changed noticeably.
The dimmer has the higher priority. When a reading arrives it runs at once, preempting the sampler, so the LED reacts within one tick of the sample.
Try this
- Watch the Tasks panel: each reading wakes
dimmerright aftersamplerruns, because the dimmer has the higher priority. Swap the priorities and compare the timeline. - Turn the knob while watching the Serial Monitor.
- Add
vTaskDelay(pdMS_TO_TICKS(300))at the end of the dimmer's loop so it falls behind, and count dropped readings in the sampler. - Make the queue one item long and use
xQueueOverwriteso the dimmer always gets the latest value. - Add a third task that receives from the same queue. Who gets each reading?
sketch.ino
// A sampler task reads A0 and sends readings through a queue; a dimmer task receives them and drives the LED.
#include <Arduino_FreeRTOS.h>
#include <queue.h>
const int KNOB = A0;
const int LED = 9;
QueueHandle_t readings;
volatile unsigned dropped = 0;
void sampler(void *) {
for (;;) {
int value = analogRead(KNOB);
// Wait at most 10 ms for room in the queue, then give up on this reading.
if (xQueueSend(readings, &value, pdMS_TO_TICKS(10)) != pdPASS) dropped++;
vTaskDelay(pdMS_TO_TICKS(50));
}
}
void dimmer(void *) {
pinMode(LED, OUTPUT);
int shown = -100;
int value;
for (;;) {
// Sleep until a reading arrives.
if (xQueueReceive(readings, &value, portMAX_DELAY) == pdTRUE) {
analogWrite(LED, value / 4);
if (abs(value - shown) > 8) {
shown = value;
Serial.print(F("knob "));
Serial.print(value);
Serial.print(F(" -> brightness "));
Serial.print(value / 4);
Serial.print(F(", dropped "));
Serial.println(dropped);
}
}
}
}
void setup() {
Serial.begin(9600);
readings = xQueueCreate(4, sizeof(int));
xTaskCreate(sampler, "sampler", 128, NULL, 1, NULL);
xTaskCreate(dimmer, "dimmer", 192, NULL, 2, NULL);
}
void loop() {}