The Arduino Hang Guardian - Arduino Watchdog Timer Tutorial

About: Part software developer, part maker.

Hi Everyone,

It happens to all of us. You build a project, enthusiastically connect all of the sensors, and all of a sudden, the Arduino hangs and no input is processed.

“What is going on?”, you’ll ask and start digging through your code, only to realize that you’ve got stuck in an infinite loop. Thank God the Arduino was on your bench and not in a remote location.

Today, we’ll look at how we can use the watchdog timer on the Arduino to prevent this from happening.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: What Is This Watchdog Timer?

The Arduino watchdog timer as the name implies is a timer that runs separately from the main CPU on the board. This timer can be used to periodically check the state of the board and in circumstances where the board got stuck in a software loop or got stuck because of hardware failure, the watchdog timer can reset the Arduino and have it start all over again.

By default when not in use this timer is disabled on all projects and only when we enable it, we need to make sure to periodically tell it to not reset our board if it is still operating as expected.

To be able to use the watchdog timer, we first need to include the avr/wdt.h file in our Arduino sketch. This is the standard library that holds the watchdog actions.

Step 2: How to Enable the Watchdog Timer?

To enable the timer, we use the function “wdt_enable” where we need to pass in the threshold interval at which the board will be reset. Depending on our use case, this can be anywhere from 15 milliseconds to up to 8 seconds in pre-defined settings that are contained in the watchdog library.

THRESHOLD - CONSTANT NAME

15 ms WDTO_15MS
30 ms WDTO_30MS
60 ms WDTO_60MS
120 ms WDTO_120MS
250 ms WDTO_250MS
500 ms WDTO_500MS
1s WDTO_1S
2s WDTO_2S
4s WDTO_4S
8s WDTO_8S

Step 3: Keep Your Program Running

Now, with the timer enabled, to prevent it from resetting our Arduino we need to periodically call the “wdt_reset” function to reset the watchdog timer before the threshold interval expires.

When choosing a reset interval, it is crucial that we take into consideration any lengthy operation like reading or sending data or connecting to external sensors. The reset threshold should be at least one and a half times greater than these times to prevent any accidental resets.

Step 4: Example Usage

In the example program that you can download here, we first set up the watchdog timer with a reset interval of 4 seconds. Then to know that we are in the setup function, we flash an LED for 3 times quickly and then in the main loop we first reset the timer, light up an LED with progressively longer intervals and then we turn it off. The next loop resets the timer again until the light-up time is longer than 4 seconds.

When this happens, the watchdog timer resets the board and the setup is executed again.

Step 5: Watchdog Timer Issues

One possible issue with the watchdog timer, depending on the bootloader of your Arduino is that if the watchdog timer value is too low and the bootloader does not reset the timer when uploading new code, you may end up damaging your Arduino board in a way that it will always be stuck in the boot phase. The bootloader will try to start, but the timer will keep resetting the board, never allowing it to properly start. To prevent issues like this, make sure to always use threshold intervals of 2 seconds or more.

Step 6: Enjoy

If you have an example of where you have used the watchdog timer in a real project, let me know down in the comments, make sure to like the video and don’t forget to subscribe.

Cheers and thanks for reading/watching!

Be the First to Share

    Recommendations

    • CNC Contest

      CNC Contest
    • Make it Move

      Make it Move
    • Teacher Contest

      Teacher Contest

    2 Discussions

    0
    None
    WilkoL

    7 weeks ago

    Good explanation. I do have one question though, you say that there is a risk when the wd-time-out is set low because it may interfere with the bootloader. But after a reset the wd-timer isn't activated until after the bootloader has finished and your code is running, isn't it? (Except when you activate it with the fuses of course.)
    And even if you had set it to 8 seconds, a reset could come at the last few milliseconds before the time-out...
    I have to admit, I haven't tested it, I prefer to use the atmega328 without the bootloader and program it with a AVR-ISP or AVR_ICE debugger.

    1 reply
    0
    None
    taste_the_codeWilkoL

    Reply 5 weeks ago

    It is a problem generally with the older Mega 2560 boards where the old bootloader does not reset the timer when uploading code so the timer expires, bootloader starts, bootload works for a while, the timer expires, etc., etc. etc, where in practice the board is 'soft bricked'. It can be recovered but it is a complicated process.