What Is the Watchdog Timer? Wiring and Errors (2026)

What Is the Watchdog Timer? Wiring and Errors (2026)

A Watchdog Timer (WDT) is a hardware countdown timer inside the microcontroller that acts as a dead-man's switch. You "feed the dog" — reset the counter — periodically in your main loop. If your program hangs, the countdown hits zero and the WDT hardware-resets the chip, so the system recovers on its own.

Quick answer: The watchdog is a real hardware peripheral, not a software trick — it keeps working even when your code crashes into an infinite loop or a stuck sensor read. On Arduino Uno/Nano, arm it with wdt_enable(WDTO_2S) and call wdt_reset() every loop before timeout. It only helps with hangs, not a program that's wrong but still running.

Proof of work: the explanation and code below are Soldr's actual answer to "What is the Watchdog Timer and how do I use it?", asked through Compoden's AI build assistant on 12 August 2026. Published 12 August 2026 · Last updated 12 August 2026.

Soldr's answer explaining the Watchdog Timer: the dead-man's-switch mechanism, when it does and doesn't help, and a complete AVR arm-and-feed sketch
Soldr's build log answering "What is the Watchdog Timer and how do I use it?" — the explanation and a complete Arduino sketch generated in the same session.

What kinds of failures does a watchdog actually catch?

It's a real peripheral in the silicon, so it keeps running even when your code has crashed — an infinite loop, a stuck I2C read, a sensor call that blocks forever. Classic use cases are a robot rover that wedges against an obstacle, a remote sensor node that stops responding, or a display that freezes mid-update. The one caveat worth remembering: it only helps if the crash is a genuine hang. If the program is simply wrong but still executing, the watchdog just resets a buggy loop forever without fixing anything.

Watchdog behavior by platform
Platform How you use it
Arduino Uno/Nano (ATmega328P) You control it directly: wdt_enable()/wdt_reset()/wdt_disable()
ESP32/ESP8266 Arduino core already feeds the hardware WDT; use esp_task_wdt_init() for long tasks instead

How do I arm and feed the watchdog on an Arduino Uno?

On the ATmega328P it's dead simple. Include avr/wdt.h, arm it with wdt_enable(WDTO_2S) for a 2-second timeout, and call wdt_reset() inside loop() before that time elapses. To see it actually work, delete the wdt_reset() call — within about 2 seconds the board reboots on its own, visible as the boot message repeating on the serial monitor. Timeout options range from WDTO_15MS up to WDTO_8S.

// legend: [SOLDR] watchdog demo - [INIT] init - [WARN] fault - [FIX] likely remedy
#include <avr/wdt.h>

void setup() {
  Serial.begin(9600);
  // 2-second timeout - also resets the AVR on boot by design
  wdt_enable(WDTO_2S);
  Serial.println("[INIT] WDT armed: 2s timeout");
}

void loop() {
  wdt_reset(); // feed the dog - must run before 2s elapse
  Serial.println("[DATA] loop alive, dog fed");
  delay(1000);
}

Why does my Arduino reboot once right after I upload a sketch with the watchdog enabled?

wdt_enable() also fires a reset when the sketch starts — that's normal, it's how the AVR watchdog is designed, not a bug in your code. If you need to disable the watchdog later in a sketch, call wdt_disable(). On ESP32/ESP8266 boards, the behavior is different: you don't usually touch the hardware WDT directly, since the Arduino core already feeds it for you, and instead use esp_task_wdt_init() for long-running tasks or check for timeouts yourself.

What do you need to try this yourself?

An Arduino Uno R3 (CH340G) (Rs.230) is the cleanest board for learning the AVR watchdog directly — its ATmega328P is exactly what avr/wdt.h targets.

Frequently asked questions

Watchdog Timer kis kaam aata hai?

Watchdog Timer ek hardware timer hai jo microcontroller ke andar hota hai aur "dog ko khana khilate rehna" padta hai - matlab loop() mein baar-baar reset karna. Agar program kahin atak jaaye (jaise infinite loop ya block hua sensor read), toh watchdog khud hi chip ko reset kar deta hai, taki system apne aap recover ho jaaye.

Will the watchdog fix a bug in my code that keeps producing wrong output?

No - the watchdog only catches genuine hangs, where the program stops progressing entirely. If your code is running but producing wrong values due to a logic bug, the watchdog has nothing to detect, since the loop keeps completing and feeding the dog on schedule.

What is the price of an Arduino Uno for learning the watchdog timer in India?

The Arduino Uno R3 (CH340G) costs Rs.230 at Compoden, with cash on delivery available across India. Its ATmega328P chip has the AVR hardware watchdog built in, accessible through the avr/wdt.h library shown above. Prices checked 12 August 2026.

Do I need a watchdog timer if my project is always connected to power and I can reset it manually?

For a bench project you can walk over to, a manual reset is fine - the watchdog earns its place in unattended deployments, like a remote sensor node, a rover, or a display kiosk, where a single stuck sensor read could otherwise brick the device until someone physically intervenes.

Back to blog