Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
    • Design Guides
      • WiFi & the IOT Design Guide
      • Microcontrollers Design Guide
      • State of the Art Inductors Design Guide
      • Power Electronics & Programmable Power
  • Women in Engineering

How to use MicroPython’s watchdog timer in ESP8266 and ESP32

By Nikhil Agnihotri

A watchdog timer is an internal or external timer that monitors a microcontroller’s program to ensure the application remains operative without failure. It serves as a safety feature in critical applications by monitoring the microcontroller’s output signal.

The watchdog can operate in two modes: 

  • Timeout mode – the timer establishes the microcontroller is not working properly if it inputs multiple signals (for example, if a double pulse is detected within a set period).
  • Window mode – the timer establishes the microcontroller is not working properly if it fails to receive a signal or receives too many in a set period. 

If either of these events occurs, the watchdog timer resets the microcontroller. 

The WDT class
MicroPython provides class WDT to configure and activate the watchdog timer in ESP8266, ESP32, WiPy, and pyboard. These are the only platforms for which WDT is available. 

A WDT object in a MicroPython script is used to restart the controller when an application crashes or is in a non-responsive or non-recoverable state. 

Once the watchdog timer starts, it cannot be reconfigured or stopped. It’s enabled as soon as an object of the WDT class is instantiated. Additionally, the script/program must feed the watchdog timer periodically to avoid expiring or resetting the controller automatically. 

The WDT class is imported in a MicroPython script using this statement:

from machine import WDT

The constructor method for WDT class has this prototype: 

class machine.WDT(id=0, timeout=5000)

The constructor method can take two keyword parameters, id and timeout. The “id” is that of the watchdog timer and should only be passed if there are multiple watchdog timers in a microcontroller.

The timeout parameter specifies the feed timeout and is required, depending on the MicroPython port. The timeout period is port-specific and specified in milliseconds. 

The watchdog timer is enabled immediately after an object of the WDT class is instantiated. The WDT class provides only one method. 

WDT.feed(): When this method is called, it feeds the watchdog timer periodically to prevent it from resetting the controller. This method must be called within or at the end of a script, so the watchdog timer is only fed when the main execution code is confirmed. If the script does not contain an infinite loop, it can be called at the end of the script. If the main execution script contains an infinite loop, it must be placed as the last statement of the loop. The method does not take any arguments. 

Watchdog timer in ESP8266
When using ESP8266, the timeout cannot be specified for the watchdog timer and is automatically determined by the underlying system. This board only has one watchdog timer, so there’s no need to pass its “id.” 

Here is a valid example of instantiating and configuring the watchdog timer in ESP8266:

from machine import WDT
wdt = WDT()
wdt.feed() 

A MicroPython script that uses the watchdog timer without an infinite loop (in ESP8266) should look like this: 

from machine import WDT
wdt = WDT()
….#Main execution code
wdt.feed() 

A MicroPython script that uses the watchdog timer with an infinite loop (in ESP8266) should look like this: 

from machine import WDT
wdt = WDT()
….# Non-repeating MicroPython code
while True :
# Code of infinite loop
wdt.feed() 

Watchdog timer in ESP32
In ESP32, one second is the minimum timeout that can be specified. ESP32 only has one watchdog timer, so it does not require specifying the “id.” 

This is a valid example of instantiating and configuring the watchdog timer in ESP32. 

from machine import WDT
wdt = WDT(timeout = 5000)
wdt.feed() 

A MicroPython script that uses the watchdog timer without an infinite loop (in ESP32) should look like this: 

from machine import WDT
wdt = WDT(timeout = 5000)
….#Main execution code
wdt.feed() 

A MicroPython script that uses the watchdog timer with an infinite loop (in ESP32) should look like this: 

from machine import WDT
wdt = WDT(timeout = 5000)
….# Non-repeating MicroPython code
while True :
# Code of infinite loop
wdt.feed() 

Conclusion
The watchdog timer is an excellent safety feature that ensures microcontrollers execute the correct application without failure. The watchdog detects any malfunctioning and resets the controller if it stops working or is in a non-recoverable state. 

MicroPython supports the watchdog timer’s functionality via the WDT class, but only for ESP32, ESP8266, WiPy, and pyboard. The watchdog timer must be carefully placed and fed within a MicroPython script so that it first ensures confirmation of the execution of the MicroPython’s main code. 

 


Filed Under: Tutorials
Tagged With: ESP32, esp8266, MicroPython, pyboard, timer, watchdogtimer, wdt, wipy
 

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


Featured Tutorials

  • What AI can bring to PCB manufacturing
  • battery types
    What are the different battery types for specific applications?
  • battery configurations
    Battery configurations (series and parallel) and their protections
  • Choosing Batteries for Robots
  • battery cycles
    How rechargeable batteries, charging, and discharging cycles work
  • battery selection low power design
    What are the battery-selection criteria for low-power design?

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • STMicroelectronics offers multi-connectivity development kit for asset tracking
  • Renesas launches new MPUs for advanced industrial Ethernet communication
  • STMicroelectronics releases TouchGFX 4.20 for advanced graphics
  • Infineon’s new HYPERRAM memory chip doubles bandwidth
  • Vishay receives R&D grant for WATT4FRED project

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr bluetooth dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics renesaselectronicscorporation Research samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • Equation of return loss specification?
  • transient vs. pss simulation
  • High Signal to noise ratio in low frequency
  • A 3-stage can work correctly but 4-stage ring vco can't work
  • Measuring a 1GHZ Signal

RSS Electro-Tech-Online.com Discussions

  • Audio measurements automated (Sone unit of loudness)
  • Digital Display Information
  • 2nd pcb design program?
  • Help identify a part
  • Need help for diy ups project
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
    • Design Guides
      • WiFi & the IOT Design Guide
      • Microcontrollers Design Guide
      • State of the Art Inductors Design Guide
      • Power Electronics & Programmable Power
  • Women in Engineering