Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey 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
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

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

By Nikhil Agnihotri January 19, 2023

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: Python, Tutorials
Tagged With: ESP32, esp8266, MicroPython, pyboard, timer, watchdogtimer, wdt, wipy
 

Next Article

← Previous Article
Next Article →

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.

EE TECH TOOLBOX

“ee
Tech Toolbox: 5G Technology
This Tech Toolbox covers the basics of 5G technology plus a story about how engineers designed and built a prototype DSL router mostly from old cellphone parts. Download this first 5G/wired/wireless communications Tech Toolbox to learn more!

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

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!


RSS EDABOARD.com Discussions

  • Phase Shift Full Bridge suffers spurious FET turn_ON
  • optimum spacing between feed and sub reflector
  • Equipment to see if household should buy battery/solar/inverter?
  • 'HERIC' pairs of IGBTs essential for Mains inverters
  • reverse polarity circuit protection between to power sources

RSS Electro-Tech-Online.com Discussions

  • Adhesive Defibrillator Pad Cable
  • Epson crystal oscillators
  • Simple LED Analog Clock Idea
  • Fun with AI and swordfish basic
  • Microinverters and storeage batteries?

Featured – LoRa/LoRaWan Series

  • What is the LoRaWAN network and how does it work?
  • Understanding LoRa architecture: nodes, gateways, and servers
  • Revolutionizing RF: LoRa applications and advantages
  • How to build a LoRa gateway using Raspberry Pi
  • How LoRa enables long-range communication
  • How communication works between two LoRa end-node devices

Recent Articles

  • How to design a heart-rate pulse sensor BPM meter
  • Three-phase EMI filter with neutral provides >1 MOhm insulation resistance
  • Infineon adds 60 V and 150 V power MOSFETs with 30 krad(Si) TID and AEC-Q101 qualification
  • Microchip releases DSC families with 78 ps PWM resolution and 40 Msps ADCs
  • Silanna Semiconductor releases laser driver IC with 86% efficiency

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 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

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey 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
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe