watchdog-secours.ino

Arduino Nano Every · File : watchdog-secours.ino · Language : Arduino · Use case : Auto-reprise après blocage grâce au watchdog

In a sealed enclosure, a software crash must end in a reboot: the 4809 watchdog resets the board if the firmware forgets its refresh for one second. This sketch arms the dog and shows where to place wdt_reset in a long task.

Hardware

An Arduino Nano Every (ATmega4809 at 20 MHz, same footprint as the Nano, micro-B USB, separate UART chip, 5 V pins, drop-in compatibility with the classic Nano). Bare Nano Every; the watchdog is a hardware peripheral of the 4809.

Wiring

No wiring; test by uncommenting the blocking loop — the board self-resets every second.

How it works

wdt_enable(WDTO_1S) arms the hardware countdown; wdt_reset() reloads it. Without refresh, the 4809 issues a system reset — clean recovery into setup().

Running it

Install the Arduino megaAVR Boards package in the IDE, select Arduino Nano Every, upload. The USB Serial is separate from hardware Serial1 (RX/TX pins): classic serial code works as is.

Variants

Tune the delay (WDTO_250MS to WDTO_8S) to your longest task, or disarm the dog before serial programming.

watchdog-secours.ino

// Watchdog du 4809 : reinitialise la carte si le programme bloque.
// Le core megaAVR fournit l'API wdt identique a l'ancien core AVR.
#include <avr/wdt.h>

void setup() {
  // lire la cause du redemarrage AVANT d'armer le watchdog
  wdt_disable();
  Serial.begin(9600);
  Serial.println(F("demarrage"));

  wdt_enable(WDTO_1S); // si pas de rafle pendant 1 s -> reset
}

void loop() {
  // Tache longue : la boucle doit "nourrir" le chien regulierement
  for (int etape = 0; etape < 100; etape++) {
    travail();       // votre code potentiellement bloquant
    wdt_reset();     // rafle : le programme est vivant
  }
  // Volontairement bloque ? decommentez pour tester le reset :
  // while (true) {}
}

void travail() {
  static unsigned long t = 0;
  if (millis() - t > 1000) {
    t = millis();
    Serial.println(F("en vie"));
  }
}

← Back to the board