Arduino Watchdog
The watchdog timer monitors program execution and can intervene if the application remains stuck. This tutorial explains how to configure the AVR watchdog on ATmega328P-based Arduino Uno, Nano and Mini boards.
Difficulty: Intermediate
Created: June 2023
Technically reviewed: August 2026
Reference platform: AVR-based Arduino boards, with ATmega328P used in the examples
What is a watchdog for?
The watchdog timer, or WDT, is a hardware timer that operates partly independently of the main program flow. The program must regularly signal that it is still working correctly. If that signal does not arrive before the configured timeout, the watchdog can trigger an interrupt or reset the microcontroller, depending on its operating mode.
A watchdog does not correct the underlying program error and is not a replacement for proper error handling. It acts as a safety net that can restore operation after problems such as an infinite loop, an invalid state-machine state or an operation that never returns.
Platform-dependent behaviour
The examples below use the AVR Libc <avr/wdt.h> interface and therefore cannot be transferred directly to every Arduino board. They apply to the Uno R3, the classic Nano and other ATmega328P-based boards. The Uno R4, ESP32, SAMD and other architectures can use different watchdog APIs and behaviour.
Including the watchdog header
On AVR platforms, include the AVR Libc header to control the watchdog:
#include <avr/wdt.h>
Essential watchdog operations
Enabling watchdog supervision
wdt_enable(timeout) enables the watchdog with the selected timeout. In reset mode, the microcontroller restarts if the program does not reset the watchdog counter in time.
wdt_enable(WDTO_2S);
Resetting the watchdog counter
wdt_reset() signals that the program is still running correctly. Call it only at a point that healthy program execution genuinely reaches. Resetting the watchdog unconditionally inside every waiting loop can hide the exact fault you intended it to detect.
wdt_reset();
Disabling watchdog supervision
wdt_disable() disables the watchdog. Do this only when the remaining program execution no longer requires watchdog supervision.
wdt_disable();
Available timeout values
AVR Libc provides named constants for selecting the timeout. These values are approximate; the available settings and actual timing depend on the microcontroller, supply voltage and the watchdog’s own oscillator.
| Constant | Nominal timeout |
|---|---|
WDTO_15MS | 15 ms |
WDTO_30MS | 30 ms |
WDTO_60MS | 60 ms |
WDTO_120MS | 120 ms |
WDTO_250MS | 250 ms |
WDTO_500MS | 500 ms |
WDTO_1S | 1 s |
WDTO_2S | 2 s |
WDTO_4S | 4 s, where supported |
WDTO_8S | 8 s, where supported |
Choose a timeout that is safely longer than the maximum expected interval between two valid watchdog resets during normal operation.
A simple reset-watchdog example
The following ATmega328P example uses a two-second watchdog. After printing a message to the Serial Monitor, the program deliberately enters an infinite loop, causing the watchdog to reset the microcontroller.
#include <avr/wdt.h>
void setup()
{
// Allow enough time for the serial connection during startup.
wdt_disable();
Serial.begin(115200);
delay(500);
Serial.println("Program started");
wdt_enable(WDTO_2S);
}
void loop()
{
static unsigned long startedAt = millis();
// Service the watchdog normally for five seconds.
if (millis() - startedAt < 5000UL)
{
wdt_reset();
return;
}
Serial.println("Simulated program lockup");
Serial.flush();
// Execution cannot leave this loop, so the watchdog triggers a reset.
while (true)
{
}
}
How the program works
setup()enables the two-second watchdog.- For the first five seconds,
loop()regularly callswdt_reset(). - The program then enters an infinite loop and stops resetting the watchdog.
- After approximately two seconds, the microcontroller restarts and “Program started” appears again.
Watchdog and sleep modes
The watchdog on some AVR microcontrollers can continue operating during sleep and wake the processor with a watchdog interrupt. This is not the same configuration as the wdt_enable() reset example above. Interrupt-only, reset-only and combined modes require the appropriate watchdog-register configuration, and the procedure is MCU-specific.
For this reason, the simple sleep_mode() example from the old page is not carried over unchanged: enabling reset mode by itself does not demonstrate correct, generally reusable periodic wake-up behaviour.
Common mistakes and important notes
- Timeout too short: normal initialisation or a valid long-running operation can cause a reset.
- Unconditional watchdog resets: the watchdog remains satisfied even when the application is already in a faulty state.
- Blocking library call: if an operation can exceed the timeout, design a timeout or use non-blocking execution.
- Repeated reset loop: initialise outputs safely and inspect the reset cause where necessary.
- Mixing platforms: do not copy the AVR header and examples unchanged to an Uno R4, ESP32 or another architecture.
🧪 Try it!
- Upload the example to an ATmega328P-based board and open the Serial Monitor at 115200 baud.
- Observe how often the program restarts.
- Replace
WDTO_2Swith another timeout supported by your board. - Remove the infinite loop and confirm that the resets stop during normal operation.
Summary
A watchdog is a hardware supervision timer that can help restore operation when the program no longer reaches its designated health-check points. On AVR-based Arduino boards, it can be controlled with wdt_enable(), wdt_reset() and wdt_disable(). Reliable use requires a suitable timeout, carefully chosen reset points and a safe startup process.
