Code
This page contains code examples and common patterns for using the watchdog. These are meant more for illustrative purposes and to adapt the code into your own projects, than for running as full self-contained programs. However, you can run them as-is if you like. You can also try removing or commenting out the wdt.feed() call to observe the watchdog bite.
This code demonstrates the RAISE mode watchdog. RAISE mode is supported on Nordic and Espressif ports.
# SPDX-FileCopyrightText: 2026 Tim Cocks for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
Demonstration of the RAISE mode watchdog. Supported on nordic, and espressif ports only.
Raises an exception if the watchdog is not fed at least once every timeout interval.
"""
import microcontroller
import watchdog
wdt = microcontroller.watchdog
# must feed the watchdog at least once every interval of this many seconds
wdt.timeout = 8
# activate the watchdog is raise mode
wdt.mode = watchdog.WatchDogMode.RAISE
while True:
wdt.feed()
# other tasks for you project...
This code demonstrates the RESET mode watchdog. RESET mode has wider support: Nordic, Espressif, Raspberry Pi, and SAMD51 devices can use RESET mode.
# SPDX-FileCopyrightText: 2026 Tim Cocks for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
Demonstration of RESET mode watchdog. Supported on:
raspberrypi, nordic, espressif, and samd51 devices.
Resets the device if the watchdog is not fed at least once per timeout interval.
On nordic port the device boots into safe mode after the reset.
"""
import microcontroller
import watchdog
print(microcontroller.cpu.reset_reason)
wdt = microcontroller.watchdog
# must feed the watchdog at least once every interval of this many seconds
wdt.timeout = 8
# activate the watchdog is reset mode
wdt.mode = watchdog.WatchDogMode.RESET
while True:
wdt.feed()
# other tasks for you project...
On Nordic port, the RESET mode watchdog will cause the device to boot into safe mode after the watchdog resets. The following scripts demonstrate the usage of safemode.py to recover from safe mode back into normal mode.
This is only relevant for the Nordic port.
# SPDX-FileCopyrightText: 2026 Tim Cocks for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
Demonstration of RESET mode watchdog on nordic, with safemode.py recovery file.
The watchdog resets the device into safemode if not fed at least once per
timeout interval. safemode.py resets again back into normal mode.
"""
import time
import microcontroller
import watchdog
# wait a few seconds before activating, to give time to cancel.
time.sleep(5)
print(microcontroller.cpu.reset_reason)
wdt = microcontroller.watchdog
# must feed the watchdog at least once every interval of this many seconds
wdt.timeout = 8
# activate the watchdog is reset mode
wdt.mode = watchdog.WatchDogMode.RESET
while True:
wdt.feed()
# other tasks for you project...
# SPDX-FileCopyrightText: 2026 Tim Cocks for Adafruit Industries # # SPDX-License-Identifier: MIT """ Reboot back into normal mode from safemode after a short delay. """ import time import microcontroller time.sleep(3) microcontroller.reset()
Another option for Nordic port is to use a RAISE mode watchdog, catch the Exception that it raises, and then trigger a reset from the except block. This adaptation allows the Nordic port to behave more similarly to the others with a normal mode reset instead of safe mode. However, this workaround means that the reset reason API will return SOFTWARE as the reason instead of WATCHDOG.
This code illustrates the technique.
# SPDX-FileCopyrightText: 2026 Tim Cocks for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
Demonstrates how to use the RAISE mode watchdog on Nordic to match the
non-safemode RESET behavior that non-Nordic ports have.
"""
import time
import microcontroller
import watchdog
# will indicate SOFTWARE, not WATCHDOG since the code manually resets
print(microcontroller.cpu.reset_reason)
wdt = microcontroller.watchdog
# must feed the watchdog at least once every interval of this many seconds
wdt.timeout = 8
# activate the watchdog is raise mode
wdt.mode = watchdog.WatchDogMode.RAISE
# wrap main loop in try/except to catch the WatchDogTimeout
try:
while True:
wdt.feed()
except watchdog.WatchDogTimeout:
print("Watchdog bit, doing normal mode reset in 3 seconds")
time.sleep(3)
# normal non-safemode reset
microcontroller.reset()
Page last edited September 04, 2026
Text editor powered by tinymce.