How to using FreeRTOS on Arduino?

 Using FreeRTOS on Arduino lets you run multiple tasks (functions) independently, improving structure and responsiveness in your embedded project. Here’s a beginner-friendly guide:




Getting Started with FreeRTOS on Arduino

 Step 1: Install FreeRTOS Library

  1. Open the Arduino IDE.

  2. Go to Library Manager (Sketch > Include Library > Manage Libraries…).

  3. Search for "FreeRTOS".

  4. Install “Arduino FreeRTOS Library” by Richard Barry or similar.


 Step 2: Basic Example – Two Blinking LEDs

This example runs two tasks to blink two LEDs at different intervals.

cpp

#include <Arduino_FreeRTOS.h> void TaskBlink1(void *pvParameters); void TaskBlink2(void *pvParameters); void setup() { pinMode(8, OUTPUT); // LED 1 pinMode(9, OUTPUT); // LED 2 // Create two tasks xTaskCreate(TaskBlink1, "Blink1", 128, NULL, 1, NULL); xTaskCreate(TaskBlink2, "Blink2", 128, NULL, 1, NULL); } void loop() { // Empty - FreeRTOS takes over } void TaskBlink1(void *pvParameters) { (void) pvParameters; for (;;) { digitalWrite(8, HIGH); vTaskDelay(500 / portTICK_PERIOD_MS); // 500ms delay digitalWrite(8, LOW); vTaskDelay(500 / portTICK_PERIOD_MS); } } void TaskBlink2(void *pvParameters) { (void) pvParameters; for (;;) { digitalWrite(9, HIGH); vTaskDelay(1000 / portTICK_PERIOD_MS); // 1s delay digitalWrite(9, LOW); vTaskDelay(1000 / portTICK_PERIOD_MS); } }

 What’s Happening?

  • xTaskCreate(): Creates tasks that run in parallel.

  • vTaskDelay(): Non-blocking delay — lets other tasks run.

  • loop() is unused because FreeRTOS schedules tasks instead.


 Notes:

  • Works best on boards with more RAM like Arduino Mega or ESP32.

  • Each task needs its own stack size (e.g., 128 words = ~512 bytes).

  • Avoid using delay() inside FreeRTOS tasks — use vTaskDelay() instead.

评论

此博客中的热门博文

How To Connect Stm32 To PC?

What are the common HDL languages used in FPGA design?

How do you set up ADC (Analog-to-Digital Converter) in STM32?