Short answer: Yes—sometimes. “Arduino” (the IDE + sketches) doesn’t include an RTOS by default, but many Arduino-compatible boards either ship with one or can use one easily. ![Getting-Started-with-FreeRTOS](https://hackmd.io/_uploads/S1mqQc1Rle.jpg) **Native Arduino "OS" - The Reality** Default [Arduino](https://www.ampheo.com/c/development-board-arduino) doesn't have a true RTOS. It runs: * Simple superloop architecture (setup() + loop()) * Cooperative multitasking (you manually manage task switching) * Interrupt-driven for time-critical events * No preemption, no task priorities, no memory protection ``` cpp // Default Arduino structure - NO RTOS void setup() { // Initialization } void loop() { // Your main code runs sequentially task1(); task2(); task3(); // You must manually manage timing and task switching } ``` **Popular RTOS Options for Arduino** **1. FreeRTOS - Most Popular Choice** **Supported Boards:** * [Arduino Due](https://www.ampheo.com/product/a000062-25542566), Zero, MKR series (ARM Cortex-M) * ESP32, ESP8266 (built-in FreeRTOS) * [SAMD21](https://www.onzuu.com/search/SAMD21)/[SAMD51](https://www.ampheo.com/search/SAMD51) boards **Installation:** ``` cpp // Arduino IDE: Sketch → Include Library → Manage Libraries // Search for "FreeRTOS" by Richard Barry // PlatformIO: Add to platformio.ini lib_deps = freertos Basic Example: cpp #include <Arduino_FreeRTOS.h> #include <task.h> // Task function prototypes void TaskBlink(void *pvParameters); void TaskAnalogRead(void *pvParameters); void setup() { Serial.begin(9600); // Create tasks xTaskCreate(TaskBlink, "Blink", 128, NULL, 1, NULL); xTaskCreate(TaskAnalogRead, "AnalogRead", 128, NULL, 1, NULL); // Start scheduler - NEVER RETURNS! vTaskStartScheduler(); } void loop() { // Empty - scheduler takes over } void TaskBlink(void *pvParameters) { pinMode(LED_BUILTIN, OUTPUT); while(1) { digitalWrite(LED_BUILTIN, HIGH); vTaskDelay(1000 / portTICK_PERIOD_MS); // Non-blocking delay digitalWrite(LED_BUILTIN, LOW); vTaskDelay(1000 / portTICK_PERIOD_MS); } } void TaskAnalogRead(void *pvParameters) { while(1) { int sensorValue = analogRead(A0); Serial.println(sensorValue); vTaskDelay(100 / portTICK_PERIOD_MS); } } ``` **2. ChibiOS - High Performance** Best for: SAM-based boards (Due, Zero) Features: Hard real-time capabilities, lower latency **3. mbed OS (for ARM boards)** * [Arduino Nano 33 BLE](https://www.ampheo.com/product/abx00030-25542527), [Portenta H7](https://www.ampheo.com/product/abx00042-25542693), [Nicla Vision](https://www.ampheo.com/product/abx00051-25542502) * Full-featured RTOS with networking, security **4. ESP32's Built-in FreeRTOS** ``` cpp // ESP32 has FreeRTOS built-in - no library needed #include <freertos/FreeRTOS.h> #include <freertos/task.h> void anotherTask(void *parameter) { while(1) { Serial.println("This is another task"); vTaskDelay(1000 / portTICK_PERIOD_MS); } } void setup() { Serial.begin(115200); xTaskCreate(anotherTask, "another Task", 10000, NULL, 1, NULL); } void loop() { Serial.println("This is Arduino loop"); delay(1000); } ``` **RTOS Benefits for Arduino Projects** **Concurrent Task Management** ``` cpp // Without RTOS - manual cooperative multitasking unsigned long previousMillis1 = 0; unsigned long previousMillis2 = 0; void loop() { unsigned long currentMillis = millis(); // Task 1 every 1000ms if (currentMillis - previousMillis1 >= 1000) { previousMillis1 = currentMillis; task1(); } // Task 2 every 500ms if (currentMillis - previousMillis2 >= 500) { previousMillis2 = currentMillis; task2(); } } // With RTOS - automatic preemptive scheduling xTaskCreate(task1, "Task1", 1000, NULL, 1, NULL); xTaskCreate(task2, "Task2", 1000, NULL, 2, NULL); // Higher priority ``` **RTOS Features Available:** ![企业微信截图_20251017174618](https://hackmd.io/_uploads/SklKg5yClx.png) **When to Use RTOS on Arduino** **Use RTOS when you need:** 1. Multiple time-critical tasks running simultaneously 2. Complex timing requirements 3. Responsive systems that can't be blocked by long operations 4. Network stack + application logic concurrently 5. Professional embedded development practices **Stick with default loop() when:** * Simple projects with 1-2 tasks * Basic [sensor](https://www.ampheo.com/c/sensors) reading + LED control * Beginners learning Arduino basics * Memory-constrained devices (Uno, Nano) **Practical Implementation Examples** **Example 1: Data Logger with RTOS** ``` cpp #include <Arduino_FreeRTOS.h> #include <semphr.h> SemaphoreHandle_t xSerialSemaphore; void TaskSensorRead(void *pvParameters) { while(1) { // Read sensor int value = analogRead(A0); // Safely print with semaphore if (xSemaphoreTake(xSerialSemaphore, portMAX_DELAY) == pdTRUE) { Serial.print("Sensor: "); Serial.println(value); xSemaphoreGive(xSerialSemaphore); } vTaskDelay(100 / portTICK_PERIOD_MS); } } void TaskDataSave(void *pvParameters) { while(1) { // Save data to SD card vTaskDelay(1000 / portTICK_PERIOD_MS); } } void setup() { Serial.begin(9600); xSerialSemaphore = xSemaphoreCreateMutex(); xTaskCreate(TaskSensorRead, "Sensor", 256, NULL, 2, NULL); xTaskCreate(TaskDataSave, "Save", 256, NULL, 1, NULL); vTaskStartScheduler(); } ``` **Example 2: Robot Controller** ``` cpp // Separate tasks for different robot functions xTaskCreate(TaskMotorControl, "Motors", 512, NULL, 3, NULL); // High priority xTaskCreate(TaskSensorFusion, "Sensors", 1024, NULL, 2, NULL); // Medium priority xTaskCreate(TaskCommunication, "Comm", 512, NULL, 1, NULL); // Low priority ``` **Board Compatibility Guide** ![企业微信截图_20251017174757](https://hackmd.io/_uploads/SyXJ-5JAgg.png) **Getting Started Tutorial** **Step 1: Install FreeRTOS Library** 1. Arduino IDE → Tools → Manage Libraries 2. Search for "FreeRTOS by Richard Barry" 3. Click Install **Step 2: Basic Task Creation** ``` cpp #include <Arduino_FreeRTOS.h> void setup() { xTaskCreate(MyTask, "MyTask", 128, NULL, 1, NULL); vTaskStartScheduler(); } void MyTask(void *pvParameters) { pinMode(LED_BUILTIN, OUTPUT); while(1) { digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); vTaskDelay(500 / portTICK_PERIOD_MS); } } void loop() {} // Unused ``` **Step 3: Monitor Task Status** ``` cpp // Add to setup() before scheduler starts Serial.begin(115200); configUSE_TRACE_FACILITY = 1; // Enable task stats // Check task states periodically void TaskMonitor(void *pvParameters) { while(1) { Serial.printf("Free heap: %d\n", xPortGetFreeHeapSize()); vTaskDelay(5000 / portTICK_PERIOD_MS); } } ``` **Conclusion** Yes, [Arduino](https://www.ampheoelec.de/c/development-board-arduino) can run RTOS, but you need to: 1. Choose compatible hardware (ESP32, ARM-based boards recommended) 2. Install appropriate RTOS library (FreeRTOS most common) 3. Understand RTOS concepts (tasks, semaphores, queues) 4. Manage limited resources on smaller boards For new projects where RTOS functionality is needed, ESP32 is highly recommended due to its built-in FreeRTOS support and ample resources.