Soft WDT reset on NodeMCU 1.0 LoLin V3 using delayMicroseconds()

2 min read 04-10-2024
Soft WDT reset on NodeMCU 1.0 LoLin V3 using delayMicroseconds()


If you've been programming the NodeMCU 1.0 LoLin V3, you may have encountered the dreaded "Soft WDT Reset" error. This can be quite perplexing, especially for those new to the platform. In this article, we will explain what a Soft WDT reset is, provide an example code scenario that triggers this issue, and demonstrate how to effectively utilize delayMicroseconds() to mitigate the problem.

What is a Soft WDT Reset?

A "Soft WDT Reset" occurs when the NodeMCU's watchdog timer (WDT) detects that the microcontroller is stuck in a loop or taking too long to execute a task. The WDT is designed to ensure that your code doesn’t run endlessly, and if it doesn’t receive a "refresh" signal within a specified timeframe, it will reset the microcontroller.

Example Scenario

Imagine you are working on a project that requires reading data from a sensor and then performing a series of operations based on that data. If these operations take too long, the watchdog timer may trigger a reset. Here's a simplified version of code that might cause this issue:

void setup() {
  Serial.begin(115200);
}

void loop() {
  // Simulate a long-running task
  for (int i = 0; i < 1000; i++) {
    // Perform some computations
    Serial.println(i);
    // The long delay here can cause a Soft WDT reset
    delay(100);
  }
}

In this example, the delay(100) function holds the execution of the loop for 100 milliseconds, which may be enough to trigger the WDT.

Utilizing delayMicroseconds() to Prevent WDT Reset

To prevent the WDT reset, you can use delayMicroseconds() instead. This function allows you to introduce shorter delays that are less likely to cause the watchdog to time out. Here’s how you can modify the code:

void setup() {
  Serial.begin(115200);
}

void loop() {
  // Simulate a long-running task
  for (int i = 0; i < 1000; i++) {
    // Perform some computations
    Serial.println(i);
    // Use delayMicroseconds for shorter pauses
    delayMicroseconds(100);
  }
  yield();  // Yield to the WDT
}

In this updated code, using delayMicroseconds(100) keeps the delay minimal. Additionally, calling yield() provides a chance for the WDT to reset during long operations, ensuring that the system remains responsive.

Additional Tips and Best Practices

  1. Check Your Loops: Ensure that any loops you implement contain breaks or yield methods, especially if they are going to run for an extended period.

  2. Use Non-blocking Code: Consider implementing state machines or non-blocking code patterns to prevent long execution times.

  3. Monitor Performance: Regularly check the execution time of your functions to ensure they run smoothly within the WDT limits.

  4. Use ESP.wdtFeed(): You can also manually feed the watchdog timer by calling ESP.wdtFeed() at critical points in your code.

Conclusion

Dealing with a Soft WDT reset on NodeMCU 1.0 LoLin V3 can be a common challenge for developers. By replacing lengthy delays with delayMicroseconds() and incorporating other best practices, you can significantly enhance the reliability of your applications.

References

By applying the insights from this article, you'll be better prepared to tackle the Soft WDT reset issues in your NodeMCU projects. Happy coding!