1

I'm trying to add a watchdog on an Arduino Nano, for my code, and I'm following the code in this manual exactly, but after the amount of time for not resetting the watchdog, it resets but does not reboot.

In the last paragraph it says that:

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).

So I've tried for 8 s, but still same result.

What is wrong?

#include <avr/wdt.h>
int k=1;

void setup() {
  Serial.begin(9600);
  wdt_enable(WDTO_8S);
}

void loop() {
  //do stuff here
  k++;
  wdt_reset();
  Serial.println(1000 * k);
  delay(1000 * k);
}
4
  • Please describe "resets but does not reboot". What happens? What does not? Try section 11.8 of the Atmega328p datasheet which describes the operation modes of the watchdog timer. You may need some additional setup to get the result you want.
    – JRobert
    Commented Nov 9, 2018 at 22:45
  • Please post the actual test code you are using. The linked page only shows code which should not reset unless "do stuff here" takes too long. (Edit your question to do this).
    – Nick Gammon
    Commented Nov 10, 2018 at 1:51
  • @NickGammon added
    – guyd
    Commented Nov 10, 2018 at 9:57
  • As @JRobert said, what do you mean by "it resets but does not reboot"? Do you mean the watchdog timer resets, or the processor resets? If the processor resets then it would reboot.
    – Nick Gammon
    Commented Nov 11, 2018 at 6:33

5 Answers 5

3

the "old" Nano bootloader doesn't clear the Watchdog flag and while the bootloader waits for the upload, the board resets.

flash the "new" bootloader to Nano

In IDE 1.8.6 you can select version of Nano in Tools menu. It is the 'new' bootloader if you select "Atmega328p" and 'old' if you select "Atmega328p (Old bootloader)". If you invoke "Burn bootloader" it will use the bootloader for the selected option.

5
  • Can you please refer ?
    – guyd
    Commented Nov 10, 2018 at 10:05
  • how to burn bootloader? you need a programmer or other Arduino. arduino.cc/en/Tutorial/ArduinoISP
    – Juraj
    Commented Nov 10, 2018 at 10:09
  • where to get this "NEW" bootloader ?
    – guyd
    Commented Nov 10, 2018 at 10:10
  • I enhanced the answer
    – Juraj
    Commented Nov 10, 2018 at 10:18
  • The complete process of burning a bootloader is described here: docs.arduino.cc/built-in-examples/arduino-isp/ArduinoISP . The answer above is a summary and assumes that a programmer is a,ready connected to the target "Nano".
    – 6v6gt
    Commented May 25, 2023 at 10:17
1

This works on an Uno R3.

The sketch restarts every 4 seconds when wdt_reset() is commented out.

Please copy the code and try it.

  #include <avr/wdt.h>

  void setup() {
    pinMode(13,OUTPUT);
    digitalWrite(13,LOW);

    delay(50);
    digitalWrite(13,HIGH);    // flicker the LED to identify when setup() is running
    delay(50);
    digitalWrite(13,LOW);
    delay(50);
    digitalWrite(13,HIGH);
    delay(50);
    digitalWrite(13,LOW);
    delay(50);
    digitalWrite(13,HIGH);
    delay(50);
    digitalWrite(13,LOW);
    delay(50);

    wdt_enable(WDTO_4S);

  }
  void loop() {
    digitalWrite(13,HIGH);
    delay(500);
    digitalWrite(13,LOW);
    delay(500);
    //wdt_reset();
   }
4
  • hi- same affect... perhaps Uno and Nano behave not the same ?
    – guyd
    Commented Nov 10, 2018 at 10:01
  • @Guy.D: We still need to know what you see happening and what you expected would happen instead.
    – JRobert
    Commented Nov 11, 2018 at 13:20
  • @JRobert I’ll describe what I see: code run ok until time that wdt does not get its reset ( which I do on purpose). After that leds on board start to flicker non stop, and data stop posting on serial monitor ( this part I call- reset ). Only after a power off - board is booting back up. If I wouldn’t power off- that flickering state would keep forever.
    – guyd
    Commented Nov 11, 2018 at 14:40
  • 1
    this makes no sense .... the code that i posted flickers the LEDs during setup(), but there is no serial I/O code ..... it appears that you are not loading the new sketch into the Arduino, just rerunning the old sketch
    – jsotola
    Commented Nov 11, 2018 at 18:05
1

I got the WDT working in my Nano with the old bootloader using this code from an old post on Arduino Forum.

The code is added after the wdt_reset();

  //reset watchdog
  wdt_reset();
  //set up WDT interrupt
  WDTCSR = (1 << WDCE) | (1 << WDE);
  //Start watchdog timer with 4s prescaller
  WDTCSR = (1 << WDIE) | (1 << WDE) | (1 << WDP3) | (1 << WDP0);
1
  • 1
    the true problem are wdt intervals shorter than the time bootloader waits for upload
    – Juraj
    Commented May 25, 2023 at 5:16
0

After that leds on board start to flicker non stop, and data stop posting on serial monitor

Definitely have a read of the datasheet for the '328p, section 11.8. There are several modes of operation - it can interrupt; reset; or both. Even if it resets, it doesn't reload your initial RAM data, so, for instance, line 2 int k=1; looks like an executable statement but is an initializer and has no effect at reset, and k retains its current value. There are probably other, similar, anomalies that happen when the WDT fires.

You probably don't need to understand the WDT down to its last control-bit but definitely make sure you're using it in the mode you want to be in. It will probably be handy during your testing to have a tiny sketch that just disables the watchdog so you can quickly get your Arduino back for the next test.

0

The problem may be that the 328 defaults to a Watchdog time of 16mS after a reset- WDP3 = 0, WDP2 = 0, WDP1 = 0, WDP0 = 0 so when it resets and starts your program from the beginning of your code, the program never gets very far. I made a very short recovery program that just invokes noInterrupts(); and load it straight after a power off / on. Then you can load a new program that doesn't use the watchdog.

Not the answer you're looking for? Browse other questions tagged or ask your own question.