Microcontrollers

Add a CircuitPython Watchdog So Your Build Reboots Itself

Add a CircuitPython Watchdog So Your Build Reboots Itself

Your line-following robot ran clean for six laps, then froze on the seventh and sat there blinking while the judges walked to the next table. Nobody was close enough to hit reset. The fix is a weekend-sized one: about ten lines of CircuitPython that let the chip reboot itself when your code stops responding.

What Adafruit just published

Adafruit shipped a new Learn Guide for the CircuitPython watchdog module, the binding that exposes the hardware watchdog already sitting inside most modern microcontrollers. The concept is old and stubbornly reliable. You tell the silicon “if I go quiet for longer than N seconds, assume I have hung and restart me.” Your main loop then has to check in on schedule. The guide calls this feeding the dog, and letting the timeout elapse without feeding means the dog bites and the board resets into a clean state.

This matters most where nobody can reach the hardware: a weather node bolted to a rooftop, a competition robot mid-run, a data logger left in a stockroom over semestral break.

The code, and the parts you already own

Cost of this build: zero pesos. If you have an RP2040 Pico, an ESP32-S3 Feather, or a SAMD51 Metro on the breadboard already, the module is baked into the firmware. No library to copy into lib/, no extra pin, no soldering.

import microcontroller
from watchdog import WatchDogMode

w = microcontroller.watchdog
w.timeout = 2.5
w.mode = WatchDogMode.RESET

while True:
    read_sensors()
    w.feed()

Two knobs, and both matter. timeout is a float in seconds, and the ceiling is set by the chip rather than by CircuitPython. The RP2040 tops out near 8.3 seconds, so a 2.5 second budget leaves comfortable headroom. mode is either RESET, which hard-resets the board, or RAISE, which throws a WatchDogTimeout exception inside your code so you can flush a log or park a servo before going down. Not every port implements RAISE, so read the support table in the guide before you design around it.

The gotcha that catches everyone: any blocking call longer than your timeout will trigger a boot loop. A slow I2C sensor read, a Wi-Fi reconnect, or a long SD card write can each blow past 2.5 seconds. Feed the dog inside those routines, or raise the timeout to match your worst-case path.

Spend Sunday testing it properly

Adding the watchdog takes ten minutes. Proving it works takes the rest of the afternoon, and that is the part worth doing. Set the mode to RESET, then deliberately break something: yank the SDA line off your I2C sensor mid-run, or drop a while True: pass into a branch you can trigger. If the board comes back on its own within your timeout window, the safety net is real. Full guide and the per-chip support table are at Adafruit’s announcement, with the API reference in the CircuitPython docs.

Frequently Asked Questions

Does the watchdog timer work on every CircuitPython board?

No. It depends on the hardware watchdog in the chip itself, so support varies by port. RP2040, ESP32-S3, SAMD51 and nRF52840 boards generally expose it, and the maximum timeout differs between them; the RP2040 caps out near 8.3 seconds. Check the support table in the Learn Guide before committing to a timeout value.

What is the difference between RESET mode and RAISE mode?

RESET performs a hard reset of the board the moment the timeout elapses, which is what you want for unattended hardware. RAISE instead throws a WatchDogTimeout exception inside your Python code, so you can log the fault, close a file, or move a servo to a safe position before restarting. RAISE is not implemented on every port.

What will I learn if I build this?

You will pick up fault tolerance and timing-budget thinking, which is the difference between a demo that survives a full defense and one that needs a hand on the reset button. You will also learn to measure the worst-case duration of blocking calls like I2C reads and Wi-Fi reconnects, and how to debug a boot loop you caused yourself. Both skills carry straight into industrial embedded work.

This article was inspired by reporting from Adafruit. Find the parts and modules to build it at Circuitrocks.

// written by Ann Arandia

Ann Arandia covers community projects and maker events for the Circuitrocks blog. She writes about local workshops, kid-friendly electronics, and the Philippine maker scene — the people, the meet-ups, the projects that come out of them.