Saturday, November 22, 2014

Arduino and watchdog timer

by / Saturday, 16 June 2012 / Published in Arduino

    arduino-wdt Have you ever encountered situations when your Arduino-based device hangs?

    There are many reasons for this - from millis() overflow in 49 days to moisture in outdoor sensors (causing them not to respond)...

    But what if your device is not easily accessible and\or works for long periods without human supervision? Such situations should be prevented in this case. You don't want your plant watering system that you left to look after your plants and flowers during your vacation to hang on the second day, leaving your loved plants without water, really?

    Most MCUs, including those used in Arduino, has a special feature, called Watchdog timer. Once it is configured, it checks for 'heartbeat' signals (or commands) that your MCU should issue. As soon as the time since the last pulse exceeds configured treshold, watchdog initiates reset sequence. This is really convenient, and ensures that your system will be in hanged state for no more than watchdog threshold.

    I once encountered sudden hangs of my standalone arduino-based device. I run all possible tests, double-checked all circuitry, replaced MCU - with no effect. Finally I examined wires to external OneWire DS18B20 temperature sensor. The resistance between Vcc and GND was 600 Ohm! Then I looked through the window - it was foggy and raining... The insulation of the sensor was rather old and had a leak. As soon as I disconnected the wires leading from the sensor all problems were gone.
    Semyon Tushev

    I don't know, why there's not even a word about this in Arduino docs. Such things save a lot of trouble.

    Quoting ATMega Datasheet:

    ATmega48/88/168/328 has an Enhanced Watchdog Timer (WDT). The WDT is a timer counting
    cycles of a separate on-chip 128 kHz oscillator. The WDT gives an interrupt or a system reset
    when the counter reaches a given time-out value. In normal operation mode, it is required that
    the system uses the WDR - Watchdog Timer Reset - instruction to restart the counter before the
    time-out value is reached. If the system doesn't restart the counter, an interrupt or system reset
    will be issued.

    Using Watchdog timer in Arduino projects

    First of all, you need to include corresponding header file:

    #include <avr/wdt.h>

    In the next step we should enable WDT as well as configure it:

    wdt_enable(WDTO_2S);

    In most cases you'll have to put that line to init(){} section, however, Watchdog timer may be enabled at any moment.

    WDTO_2S is a constant that means threshold interval of 2 seconds. (It is defined inside header file). If Watchdog receive no "pat" signal for this period, it will "bite" back - reboot the MCU.

    Threshold values are pre-programmed (you cannot set any value you wish).

    Threshold valueConstant nameSupported on
    15 msWDTO_15MSATMega 8, 168, 328, 1280, 2560
    30 msWDTO_30MSATMega 8, 168, 328, 1280, 2560
    60 msWDTO_60MSATMega 8, 168, 328, 1280, 2560
    120 msWDTO_120MSATMega 8, 168, 328, 1280, 2560
    250 msWDTO_250MSATMega 8, 168, 328, 1280, 2560
    500 msWDTO_500MSATMega 8, 168, 328, 1280, 2560
    1 sWDTO_1SATMega 8, 168, 328, 1280, 2560
    2 sWDTO_2SATMega 8, 168, 328, 1280, 2560
    4 sWDTO_4SATMega 168, 328, 1280, 2560
    8 sWDTO_8SATMega 168, 328, 1280, 2560

    Finally, you should put the following line that tells watchdog that everything's ok.

    wdt_reset();

    Sure, it should be called from app's cycle more often than threshold period you have set for watchdog ends (at least, during normal operation mode). The beginning of loop() cycle could be a good idea, however, this depends on your program.

    Also it's necessary to include it to every time-consuming operation. For instance, if you have data transmission cycle in your main loop, and this cycle lasts for a couple of seconds, you should put wdt_reset(); there as well, or your board may reset during transmission.

    When choosing a treshold, take all time- and timing-related values into account: delays, function tresholds, bus timeouts and speed. Threshold value should always be greater than the largest value - at least 1.5 times greater in most cases. However, this depend on your application.

    Another thing to take in mind is that it will reset the MCU, but not another hardware in your project, so you have to do it manually during Arduino initialization - if you have such hardware, sure.

    IMPORTANT THING:

    Quoting ATMega Datasheet:

    Note: If the Watchdog is accidentally enabled, for example by a runaway pointer or brown-out
    condition, the device will be reset and the Watchdog Timer will stay enabled. If the code is not
    set up to handle the Watchdog, this might lead to an eternal loop of time-out resets. To avoid this
    situation, the application software should always clear the Watchdog System Reset Flag
    (WDRF) and the WDE control bit in the initialisation routine, even if the Watchdog is not in use.

    In older bootloaders (for 168, 2K size), the code checks whether it is running after WDT reset or not, and skips waiting for new program in this case. (See this file, line 286).

    However, I could not find something similar in Optiboot bootloader (If I'm wrong, correct me please!), used by Arduino UNO and other new boards. It seems that they sacrificed this check for size. So, it is possible that watchdog timer will be enabled after reset, so -  if you set it's value too low, your board may never come out of bootloader stage. So, to be always on the safe side, set watchdog to longer periods (2S and more).

    I hope this article will help you to make your projects more safe and reliable ;)

    Read 5445 times Last modified on Sunday, 17 June 2012 11:53

    Site still under construction

    I will add more sections in the future

    • My works / Photogallery
    • More Joomla!-related articles
    • Personal blog
    • Arduino articles from the old site (done)

    Latest Posts

    Dealing with nginx “The plain HTTP request was sent to HTTPS port” error

    Dealing with nginx “The plain HTTP reque…

    In some cases you have to run multiple services on... Read more

    21-09-2014 in Linux/BSD

    Getting Argyll/dispcalGui working under Windows 8.1 64-bit

    Getting Argyll/dispcalGui working under …

    If you upgrade to windows 8.1 64-bit, you may find... Read more

    03-01-2014 in Blog

    How to transfer or mirror your Joomla! / Wordpress / Drupal website from one server to another in one click

    How to transfer or mirror your Joomla! /…

    The following script may help you to transfer or mirror... Read more

    13-10-2013 in Linux/BSD

    Setting up your multi-user hosting VPS server (Apache): A secure way to run several Joomla/Worpress/Drupal sites

    Setting up your multi-user hosting VPS s…

    Keywords: Apache MPM ITK PHP MySQL VPS CentOS In this article... Read more

    13-10-2013 in Linux/BSD

    How it works: DS18B20 and Arduino

    How it works: DS18B20 and Arduino

    There are many Arduino sketches on the Web that provide... Read more

    05-08-2013 in Arduino

    Premium Joomla templates

    Premium Joomla templates

    Two years ago I discovered a site with tons of... Read more

    27-03-2013 in Recommendations

    Interfacing HH10D with Arduino

    Interfacing HH10D with Arduino

    HH10D is capacitive-type relative air humidity sensor. It is cheap... Read more

    17-06-2012 in Arduino

    Arduino and watchdog timer

    Arduino and watchdog timer

    Have you ever encountered situations when your Arduino-based device... Read more

    16-06-2012 in Arduino

    Measuring frequency with Arduino

    Measuring frequency with Arduino

    There are many signals that contain necessary information in frequency.... Read more

    02-03-2011 in Arduino

    Reading iButton with Arduino

    Reading iButton with Arduino

    We all are familiar with iButton devices - a small... Read more

    03-12-2010 in Arduino

    Prev Next


    Scroll to top