Making your projects more reliable

Arduino Internal Watchdog

The Arduino is a much simpler machine than a Raspberry Pi. However, it is actually easier to hang an Arduino than it is a Raspberry Pi, because all the code is single threaded on the Arduino. Single threading means that only one program runs at a time. An interesting project at [3] implements a degree of multithreading on the Arduino.

If you only have one thread running at a time, any hangup on that thread will stop the computer. Naturally, other problems can cause your code to crash and the Arduino to lock up: timeouts on peripherals, power issues, RFI, and so on. Bad code using the millis() function is a classic problem. You need to handle the rollover at 49.5 days if you aren't using a real-time clock [4].

Arduino Internal Watchdog

In this section, I will show you how to use the Arduino internal watchdog (if you can make it work). I'm not trying to be sarcastic; I just mean to say that many times something in the Arduino will keep the internal watchdog from working, so beware.

Assuming Jupiter is rising and Saturn is in ascension (now that IS sarcasm), here is a way to work with the internal Arduino WDT. First, the Wto of all the Arduino models is a maximum of eight seconds. Keep that in mind. Having a longer Wto covers a lot more sins in my opinion (Wto is 16 seconds on the internal Raspberry Pi watchdog, which still isn't long enough for my taste), so the Arduino Wto is a bit short. I often have serial processes that run longer than eight seconds in my design. Yes, you can incorporate patting into the code, but when you are using external libraries, that is a pain.

To experiment with your Arduino WDT, build a new sketch in the Arduino IDE (Listing 1); however, If you are using an ArduinoMega 2560 or similar device, you could "soft brick" your device (see the "Arduino Watchdog Problems" box). My Arduino Uno worked fine with this sketch.

Arduino Watchdog Problems

Use of the Arduino internal watchdog is problematic at best. The Arduino watchdog has a Wto of eight seconds, so if you are downloading a new sketch and the old sketch has the watchdog enabled, you can get into an infinite reboot sequence: The watchdog expires, the bootloader starts, the bootload works for a while, the watchdog expires, and so on, ad infinitum. This is called "soft bricking," and it is a problem I have run into several times. The Arduino is then worthless, but it is still running.

Some bootloaders now disable the watchdog appropriately, but beware: A lot of Arduinos out there (like the Mega 2560) still don't work. Although you can update the bootloader, it is not easy. The following are some of the problems you might encounter with the Arduino internal watchdog:

1. The internal watchdog does not power cycle the system. It reboots the Arduino via the Reset line, which means it does not restart in all conditions, especially in low-power/brownout conditions often experienced with solar-powered systems. I have seen this in Project Curacao.

2. Soft bricking (see text).

3. The internal watchdog is not completely independent of the Arduino, so if your code jumps to a piece of code that disables the WDT, you are finished. Try overwriting your stack to see what interesting things can happen to code in a small embedded system such as the Arduino.

4. A routine (e.g., serial communications) takes more than the maximum Wto of eight seconds. Figuring out all of the possibilities and putting wdt_reset() calls in the right spot is difficult or, with some serial routines, impossible.

Listing 1

Watchdog Timer Test

01 #include <avr/wdt.h>
04 void setup()
05 {
06   Serial.begin(57600);
07       Serial.println("");
08       Serial.println ("------->Arduino Rebooted");
09       Serial.println("");
10       wdt_enable(WDTO_8S);
13 }
16 void loop()
17 {
20   wdt_reset();
21 #endif
23   Serial.println("Arduino Running");
24   delay(1000);
26 }

Let the sketch run for 30 seconds or so: You should never see the Arduino Rebooted message after startup. Then, comment out the RESETWATCHDOG with:


Now when you run the sketch (if your watchdog is working), you should see the Arduino Rebooted every eight seconds or so in the serial monitor. The solution to all of the potential problems discussed here (but see the "RFI Interference" box) is to use an external WDT.

RFI Interference

The significant power system issues were not the only problems I encountered with the solar- and wind-powered Project Curacao. When the amateur radio folks powered up their worldwide radio contest on 28MHz in October, I experienced significant RFI problems as well. My box is connected to a radio tower in Curacao and a 15-meter line that just happens to be about three wavelengths of 28MHz, making it a very good antenna. Things looked good up until the radio contest!

As I was exploring this set of problems with Project Curacao, I decided to build my own external WDT. This gave me a fixed set of parameters not dependent on other software processes, in the case of the Raspberry Pi, or on which model I used, in the case of the Arduino. I decided to build an external dual watchdog board so I didn't have to rely on the internal WDTs (Project Curacao has a Raspberry Pi and Arduino that monitors the power system). With the use of standard 555 timers, I got the flexibility I needed.

Buy this article as PDF

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content