Does Arduino Support Multi-Threading?

Alternatives & Solutions

February 21, 2025 by Alessandro Colucci
Multithreading Image

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:

1. Using millis() for Multitasking

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

2. Using Task Scheduling Libraries

Several libraries mimic threading by breaking down tasks into smaller pieces and executing them sequentially.

    • Scheduler library (available for some platforms like Arduino Due).
    • TaskScheduler: A widely used third-party library for scheduling tasks.

Example with TaskScheduler: https://pastebin.com/5t78h234

3. ESP32 and Real Multi-Threading with FreeRTOS

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.

Conclusion

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!

Chat with us on WhatsApp