How do I setup a hardware watch dog timer with an Arduino?
3 Answers
There are a number of dedicated hardware watchdog chips available, but it's easy enough to do using a regular 555 timer IC and a couple of supporting parts. With the 555 configured in astable mode with a very long "HIGH" period and a relatively short (perhaps 0.3 seconds) "LOW" period, the output can be connected to the Arduino's reset pin via a diode to make the watchdog an open-collector device. The Arduino can then periodically reset the watchdog using a digital output to turn on a transistor that discharges the timing capacitor attached to the 555, preventing it ever reaching a trigger point that will cause it to reset the Arduino.
Some basic information about a project to do this is available at http://www.practicalarduino.com/news/id/471
-
1\$\begingroup\$ phooky is absolutely correct, but the internal WDT has limitations: it can't run for more than about 6 seconds (depends on CPU model) and there's no way to do a clean "reboot" purely from software. You can tell the CPU to jump to position 0 and restart the bootloader, but then you may be left with dodgy stuff in RAM and things like interrupt flags still set in the CPU. Asserting RESET in hardware ensures the Arduino will come up with a clean environment. \$\endgroup\$jonoxer– jonoxer2009-11-17 03:41:09 +00:00Commented Nov 17, 2009 at 3:41
-
2\$\begingroup\$ letting the WDT time out will cause a "clean reset" as all registers will be guaranteed to start up in a defined state. Accomplishing it in software is as simple as disabling interrupts and endlessly looping. \$\endgroup\$Nick T– Nick T2011-03-22 17:22:59 +00:00Commented Mar 22, 2011 at 17:22
There's already a watchdog timer built into the atmega chip; you can use avr/wdt.h to manipulate it.
A quick example:
#include <avr/wdt.h>
void setup() {
wdt_enable(WDTO_500MS); // have the wdt reset the chip
// if 500ms passes without a reset
}
void loop() {
wdt_reset(); // reset the wdt
}
I have heard about some problems being reported with the WDT and the regular bootloader, so you may want to try another bootloader with it, for eg Adaboot.
Also, you can do a bunch more stuff with the WDT besides just a reset. I recommend http://www.atmel.com/dyn/resources/prod_documents/doc2551.pdf