Here is my tool-set;

  • Visual Studio with plugin called "Visual Micro"
  • Flashing the HEX file using Atmel Studio.
  • via AVRISP mkII
  • on ATmega 2560 MCU

And my simple trial code (I have also some others). I couldn't make it working for the purpose of failure recovery by resetting the uC.

#include<avr/wdt.h> /* Header for watchdog timers in AVR */

void setup() {
    Serial.begin(9600); /* Define baud rate for serial communication */
    Serial.println("Watchdog Demo Starting");
    pinMode(13, OUTPUT);
    wdt_disable();  /* Disable the watchdog and wait for more than 2 seconds */
    delay(3000);  /* Done so that the Arduino doesn't keep resetting infinitely in case of wrong configuration */
    wdt_enable(WDTO_2S);  /* Enable the watchdog with a timeout of 2 seconds */

void loop() {
    for (int i = 0; i<20; i++) /* Blink LED for some time */
        digitalWrite(13, HIGH);
        digitalWrite(13, LOW);
        wdt_reset();  /* Reset the watchdog */
    while (1); /* Infinite loop. Will cause watchdog timeout and system reset. */

All I get, a rapidly flashing LED at the same pace at all times. Btw, I never noticed 3000 ms delay in Setup().

And Terminal reading;

Opening port
Port open
Watchdog Demo StartWatchdog Demo StartWatchdog Demo StartWatchdog Demo StartWatchdog Demo StartWatchdog 
Demo StartWatchdog Demo StartWatchdog Demo StartWatchdog Demo StartWatchdog Demo Start
Port closed

Would you please give me some heads-up what would be wrong here?

  • Be carefull with the watch dog and Arduino's Bootloader. Bootloader never resets WDT and if you set it too short, it'll never get into the main program after the WDT reset (WDT stays enabled). So it will start only by resetting it by reset signal. – KIIV Dec 13 '18 at 17:13
  • I know about some bootloader where ATmega2560 uses has an issue with WDT. But, I don't think there is bootloader here? I am directly flashing HEX. – Sener Dec 13 '18 at 17:20
  • Right, I missed that mkII part of AVRISP. It can be flashed by bootloader too and it doesn't erase bootloader. But flashing it over ICSP will erase the chip completely. Maybe I'd try to use WDT ISR and toggle some pin (without reseting the cpu) – KIIV Dec 13 '18 at 17:29
  • Your suggestion about WDT ISR sounds interesting. But, I didn't understand the idea toggling some pins without resetting the uC. How can I use that when something went wrong and uC hanged if it is not resetting? – Sener Dec 13 '18 at 18:17
  • Well, that is for the testing purposes, if it's even working. I was using it for low power operations as periodic wake up from deep sleep mode. In both cases is the reset undesirable. – KIIV Dec 13 '18 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.