Understanding ESP32’s Multitasking and Watchdog Mechanism: A Deep Dive

Project Background

YuZou
2 min readMar 25, 2024

In the realm of modern IoT projects, the ESP32 stands out for its robust capabilities, dual-core processor, and extensive interfacing options. Capable of handling complex communication tasks while controlling a myriad of sensors and actuators, it’s a popular choice among developers. However, a challenging issue emerged in one of our projects involving ESP32: effectively managing long-running tasks without triggering the Watchdog Timer (WDT).

The Issue at Hand

In our project, the ESP32 was tasked with communicating with other devices via Bluetooth and controlling a vibration motor. However, when the vibration feature was activated continuously, the system would sometimes unexpectedly reset. The root cause of this issue was the prolonged control of the vibration consuming excessive CPU resources, preventing other tasks (including the watchdog feeding task) from executing in a timely manner. This monopolization of resources ultimately triggered the Watchdog Timer, which automatically resets the system when it detects inadequate system responsiveness, to prevent system hang-ups.

Underlying Principles

The built-in FreeRTOS operating system in ESP32 supports multitasking, allowing developers to define multiple tasks that run in parallel. Each CPU core has its own Watchdog Timer (WDT) to monitor if tasks are running too long. If a task occupies the CPU for an extended period without pausing or yielding execution, the WDT assumes the system is no longer functioning correctly and triggers a reset to protect the system.

Solution Strategy

To address this issue, we implemented several key steps:

  1. Task Allocation: We judiciously allocated tasks across the two cores of the ESP32, ensuring no single task would monopolize a CPU core for too long. This strategy effectively utilizes the advantages of the dual-core processor and balances the task load.
  2. Appropriate Delays: By inserting brief delays (e.g., 10ms) within the loop of long-running tasks (such as vibration control), we ensured the operating system had sufficient time to process other tasks, including the watchdog feeding task. This simple yet effective strategy prevented the WDT from triggering.
  3. Utilizing FreeRTOS APIs: Employing FreeRTOS provided APIs, such as vTaskDelay(), to create non-blocking delays not only improved task management efficiency but also ensured stable system operation.
  4. Monitoring and Tuning: Using ESP32’s system logs and debugging tools to monitor task execution and resource usage allowed us to further adjust task priorities and processing strategies for system stability and responsiveness.
  5. Through these measures, we successfully resolved the issue of triggering the WDT during continuous control of the vibration motor, ensuring stable system operation and enhanced task processing efficiency. This experience underscored the importance of understanding and effectively utilizing the operating system’s multitasking capabilities and watchdog mechanisms in embedded system development.

If you like you can follow me on Twitter: https://twitter.com/zouyu1121

--

--