Try Embedded

Watchdog that rescues a hung sketch

Feed a watchdog from the main loop, hang the sketch with a button, and count the resets it recovers from in EEPROM.

Firmware · Intermediate

In a real product: Remote weather station (Agriculture): A watchdog restarts the station if it ever hangs.

Firmware hangs: a sensor that never answers, a loop that never ends, a bus stuck low. A watchdog is a timer that resets the chip unless the firmware proves it is alive by restarting the timer in time. It turns a hang into a reboot, and a reboot you can count.

What you'll learn

  • Enabling the AVR watchdog and feeding it from one place: the main loop
  • Telling a watchdog reset from a power-on reset with MCUSR
  • Keeping a count across resets in EEPROM, and treating an erased cell (0xFF) as "never written"

How it works

wdt_enable(WDTO_1S) starts the watchdog with a one-second timeout. Each pass of loop() calls wdt_reset(), so while the sketch runs normally the timer never runs out, and the green LED blinks as a heartbeat.

Press Hang and the sketch enters a while (true) it never leaves, as a driver waiting for a reply that never comes would. Nothing feeds the watchdog now, and a second later it resets the chip. On the way back up, setup() reads MCUSR: its WDRF bit says the reset came from the watchdog. The sketch adds one to a counter in EEPROM, which survives the reset, and blinks the red LED once for every reset so far.

The first lines of setup() matter: after a watchdog reset the watchdog is still running, so the sketch clears MCUSR and disables it before doing anything slow.

Try this

  • Press Hang a few times: the count goes up each time.
  • Move wdt_reset() into the blink delay's loop: the watchdog no longer notices the hang. Feed it only where the whole loop has done its work.
  • Change the timeout to WDTO_250MS: the 500 ms blink now trips it on its own. Pick the timeout from the longest normal pass of the loop.

sketch.ino

// A watchdog fed from loop(): the Hang button (pin 2) makes the sketch hang, the watchdog resets it after 1 s, and
// an EEPROM counter remembers how many times it has had to. Green LED (7): heartbeat. Red LED (8): one blink per reset.
#include <avr/wdt.h>
#include <EEPROM.h>

const int HANG = 2, HEARTBEAT = 7, RESETS_LED = 8;
const int COUNT_ADDR = 0;

void setup() {
  byte cause = MCUSR;  // why the chip reset
  MCUSR = 0;
  wdt_disable();       // still running after a watchdog reset

  Serial.begin(9600);
  pinMode(HANG, INPUT_PULLUP);
  pinMode(HEARTBEAT, OUTPUT);
  pinMode(RESETS_LED, OUTPUT);

  byte resets = EEPROM.read(COUNT_ADDR);
  if (resets == 0xFF) resets = 0;  // an erased cell: never written
  if (cause & _BV(WDRF)) {
    resets++;
    EEPROM.update(COUNT_ADDR, resets);
    Serial.print("Recovered by the watchdog. Resets so far: ");
  } else {
    Serial.print("Power-on start. Watchdog resets so far: ");
  }
  Serial.println(resets);
  for (int i = 0; i < resets; i++) {
    digitalWrite(RESETS_LED, HIGH);
    delay(150);
    digitalWrite(RESETS_LED, LOW);
    delay(150);
  }
  wdt_enable(WDTO_1S);
}

void loop() {
  wdt_reset();  // the one place the watchdog is fed
  if (digitalRead(HANG) == LOW) {
    Serial.println("Hanging...");
    while (true) {}  // a driver waiting for a reply that never comes
  }
  digitalWrite(HEARTBEAT, !digitalRead(HEARTBEAT));
  delay(250);
}

Lessons behind it