Arduino programming guide series
A real-time OS for the Arduino
A real-time operating system is optimized so that processing is completed within tight time bounds, and execution is consistent and predictable. It is the preferred operating system for critical applications. And a version of it works on the Arduino Uno.
In a previous article in this series, I discussed a simple approach for implementing multitasking on the Arduino. This approach is based on the finite state machine paradigm. It is good enough if your program has relatively few states, for example controlling "simultaneously" two or three devices.
However, as an application becomes more complicated, the state machine approach becomes harder to handle and more prone to defects.
When that happens, it is time to turn your attention to something more robust. One such solution is FreeRTOS, or "Free Real-Time Operating System".
FreeRTOS is not a solution specific to the Arduino.
It is software that runs on many microcontrollers and microprocessors. It is a commercially developed product, with an open-source license that allows people to integrate it into their designs, free of charge, for open-source and even commercial closed source projects.
It is recognized as a high-quality, well tested, documented and supported real-time operating system.
Most people are used to general-purpose operating systems, like Windows and Mac OS. These OSs manage the resources of the host computer so that the various processes that are running on top of it get their "fair" share. The OS is responsible for allocating resources (like memory and CPU time). The OS will make a best-effort attempt to allocate resources to a process but will give no guaranty that it will.
A scheduler is a special service within the OS that makes such decisions based on various scheduling algorithms. In general-purpose operating systems, the scheduler tries to be fair to all processes but can make no guarantees.
A real-time OS, on the other hand, is designed so that each process will get the resources it needs in a predictable way.
Critical applications, such as medical, avionics, and industrial automation, depend on predictable patterns in the way that the scheduler allocates resources to a process. Therefore, a general-purpose OS is not suitable, and a real-time OS takes over.
A real-time OS is not meant to work on the type of processors that we find in a personal computer, but usually on microcontrollers used in embedded applications.
Unlike in general-purpose computing, the applications in which a microcontroller is used are very specific: they are only designed to do a particular set of functions, reliably, and predictably. The real-time OS ensures the scheduling, inter-process communication, and synchronization of the processes that make up the firmware running on these microcontrollers.
You can use FreeRTOS on the Arduino, of course!
There is a FreeRTOS library that you must install first (just search for "FreeRTOS" in the Library Manager), and a respectable learning curve to get through. A good place to get started is Maniacbug's Github repository, which contains examples of Arduino sketches that use FreeRTOS, and the FreeRTOS Quick Start Guide page on freertos.org
Done with the basics? Looking for more advanced topics?
Arduino Step by Step Getting Serious is our comprehensive Arduino course for people ready to go to the next level.
Learn about Wifi, BLE and radio, motors (servo, DC and stepper motors with various controllers), LCD, OLED and TFT screens with buttons and touch interfaces, control large loads like relays and lights, and much much MUCH more.
Jump to another article
3. Focus on the type parameter in "println()"
4. "0" or "A0" when used with analogRead()?
5. What is the "_t" in "uint8_t"?
6. Save SRAM with the F() macro
7. What is the gibberish in the Telnet output?
9. Confusing keywords? follow the source code trail
10. The interrupt service routine and volatile variables
11. The problem with delay() and how to fix it
12. How to deal with the millis rollover
13. Can you use delay() inside Interrupt Service Routine?
15. A closer look at line feeds and carriage returns
16. Understanding references and pointers
17. Simple multitasking on the Arduino
19. Concurrency with the Scheduler library on the Arduino Due and Zero
20. Bitshift and bitwise OR operators
21. What is a "static" variable and how to use it
22. Understanding the volatile modifier
23. Optiboot, a free upgrade for your Arduino