By default, Arduino does not support true multi-threading like operating systems such as Linux or Windows. Most Arduino boards run on microcontrollers, which typically have single-core processors and lack an operating system to manage threads.
However, you can simulate multi-threading in several ways:
Instead of using delay()
, the millis()
function allows you to track time and schedule tasks, creating the illusion of multitasking. This non-blocking approach enables multiple tasks to run based on time intervals.
Example Code: https://pastebin.com/SA0uScc6
Several libraries mimic threading by breaking down tasks into smaller pieces and executing them sequentially.
Example with TaskScheduler: https://pastebin.com/5t78h234
If you are using an ESP32, which has dual cores, you can leverage FreeRTOS (built into the ESP32 Arduino framework) to implement true multi-threading.
Example with FreeRTOS on ESP32: https://pastebin.com/h6DbQF5z
On ESP32, FreeRTOS enables real multi-threading, allowing tasks to run on separate cores.
While standard Arduino boards do not natively support multi-threading, you can simulate it using millis()
or task scheduling libraries. For real multi-threading, ESP32 with FreeRTOS is the best option.
Would any of these solutions fit your needs? Try them out at pleasedontcode.com!