Watchdog in Arduino Library - or at least support by bootloader

I tried testing my simple sketch on page 1 of this thread. It works up to a point, but I must have the "bad" bootloader. Does anyone have a link to one that definitely fixes the watchdog issue? (For the Mega 2560).

drjiohnsmith: to answer an earlier comment, I like the mega, and I have the official IDE that comes from the arduino web site, as in here

I think you are not seeing or understanding the need for exactness.

Whether or not the Watchdog reset can be used with your board depends on what you actually have. More precisely what AVR chip is on your board and what bootloader is installed in that chip. And if what you have doesn't work, depending on what you have (board, IDE, and ISP programmer), you can use the IDE or tools that come with the IDE to modify/correct (update) a bootloader that won't work with watchdog reset to a new bootloader that will.

The information you provided above still does not answer the basic questions of what you actually have with respect to either s/w or h/w. Ok, so you "like" the "mega", but is that what you have? And if so, which "mega"? There is an Arduino "mega" that uses 1280 and one that uses a 2560. Or do you have some other AVR based Arduino?

And the s/w link you provided, actually has 27 different versions of the IDE that can be downloaded from that page. While the latest AVR based s/w version is at the top, that version changes through time so depending on when you downloaded the "latest" IDE it can be different versions. The reason that all this is very important is that in the past year there have been some pretty big changes to the IDE, and the AVR based bootloaders that ship with the Arduino s/w.

Some of these changes affect whether or not watchdog reset works and how to update a bootloader that doesn't work with one that does.

--- bill

would be nice if there was an example I could use, same as we have examples for things like lcd’s in the ide.

The exact code I posted on page 1 of this thread works, and does pretty-much what you ask. Here it is again:

#include <avr/wdt.h>

void setup ()
  Serial.begin (115200);
  Serial.println ("Restarted.");
  wdt_enable (WDTO_1S);  // reset after one second, if no "pat the dog" received
 }  // end of setup

void loop ()
  Serial.println ("Entered loop ...");
  wdt_reset ();  // give me another second to do stuff (pat the dog)
  while (true) ;   // oops, went into a loop
}  // end of loop

Tested on the Mega2560 board once I replaced the bootloader with this working one:


Entered loop ...
Entered loop ...
Entered loop ...

Here is an example sketch that shows how to use watchdog reset as a way to
intentionally reset the board.
While having a way to reset the board under software control can be useful, this example
unlike Nick’s example, is not a good example of what watchdog reset is often/normally used for.
When using watchdog in the normal way - as in Nick’s example,
best programming practice is NOT to do the wdt_reset() in an ISR but in a main loop
as his example shows, since doing it in an ISR will prevent a WDT reset from happening
when the foreground code is stuck in an an unintended loop - which is the entire point of using WDT reset.

— bill

 * Demonstration of AVR processor reset under software control 
 * since the Arduino core library does not provide a function to do this.
 * While this resets the AVR chip and all its internal registers,
 * it won't reset any of the attached hardware that is external to the AVR chip.
 * Note:
 * This will not work with some of the older versions of the Arduino bootloader
 * as they didn't properly initiliaze the AVR chip for watch dog reset & timeouts.
 * With some older bootloaders it may cause the bootloader to get stuck in a 
 * constant reboot until the chip is power cycled.
 * 2012-17-06  bperrybap - added flush for 1.x arduino to ensure message is sent
 * May 2011 -

void setup()
void loop()
	Serial.println("Top of loop()");
	for(int x = 3; x ; x--)
	Serial.println("(Reseting Arduino)");
#if ARDUINO > 100
	// force out the serial data as silly 1.x ALWAYS buffers xmits
	// with no way to disable buffering.
	resetArduino(); // reset myself

 * Function to cause the AVR to reset
#include <avr/wdt.h>
void resetArduino()
	while(1); // wait to die and be reborn....

thank you guys,

I uploaded these examples in my arduino mega 2560 and was a fight to get remove them.

FernandoGarcia: I uploaded these examples in my arduino mega 2560 and was a fight to get remove them.

So you got to see first hand what happens when a bootloader doesn't properly initialize the WDT registers after a watchdog reset. Given it is such a simple/easy fix to the bootloader, I don't understand why the Arduino team doesn't ship an updated bootloader to fix this.

--- bill

I’ve provided a link above to the fixed one. Replace the file in your current installation, and do an “burn bootloader”.

I’m not sure if the fixed one is the one that ships with the IDE, it should be, one would think.

I don’t support your words.
If the delay() is used, the watchdog will reset all time.
Reset button don’t work. I have to power off.

After much searching the web I finally found a optiboot version for the mega1280 board. Tested on two different mega1280 boards a seeeduino and a arduino mega1280. First the the new boards.txt entry to support the new bootloader:

############################################################## Mega1280 Optiboot

And the optiboot hex file optiboot_atmega1280.hex
I was able to burn this bootloader using the arduinoISP sketch from IDE 1.0.3, but not using my USBtiny hardware programmer, as it does not work with flash sizes >64KB in size.


And finally a sketch from a poster here on this forum (forgot name, sorry whoever) to test the ability to handle a very short 15 millisec WDT interrupt timeout. Works with Uno but
will ‘brick’ mega boards with ‘stock’ bootloaders.

// Test sketch to see if WDT interrupts are handled properly by the bootloader

 Warning Warning Warning this is a semi-destructive test in that
 if your bootloader does not reset WDT interrupts upon starting 
 it will be forced into a tight loop of bootloader starts/WDT resets
 chip/bootloader starts again/lather rinse repeat. One can only
 recover to normal operation by reburning the bootloader with
 a ICSP programmer. If this sketch runs properly on a chip with a WDT aware bootloader,
 you will see continuous serial output on the serial monitor.

Note that current arduino mega boards will fail this test and brick


// Code from arduino forum poster (sorry name not remembered) 1/11/13

#include <avr/wdt.h>

void setup(){
  Serial.println("Hello world");

void loop(){
  Serial.println("I am going to not get stuck..");
  for(int x=0; x<100; x++) {
  Serial.println("I am going to get stuck now..");
  for(int x=0; 1; x++) {


stevemeng: I don't support your words. If the delay() is used, the watchdog will reset all time. Reset button don't work. I have to power off.

It's a bug in the bootloader, not the sketch. If you change the fuse to load the sketch (and bypass the bootloader) it will work correctly. Or, better, get a bootloader that handles the WDT correctly.

Regarding " Tested on the Mega2560 board once I replaced the bootloader with this working one: "

I found another bootloader (which seems to be the one installed with the Arduino IDE) under bootloaders/stk500v2/stk500boot_v2_mega2560.hex

It has the same file name as "this working one", but its size is 103kB versus 21kB for the other. Obviously, these are different bootloaders, and should have a different version, means e.g. stk500boot_v3_mega2560.hex for the working one. Besides of this, what are the differences between the two, other than watchdog support?

Thanks Nick to have solved the problem for the Mega2560 (now only the correctly working bootloader should be delivered with new Mega 2560 and new versions of the IDE).

Meanwhile, I have ordered the new Arduino Due. Has anyone yet successfully used watchdog with this one? Means, does the standard Due bootloader support watchdog, and what are the equivalent lines of code for the SAM3XE8 in the Arduino IDE?

I don't know, I suggest you post this question in the Due part of the forum.

Obviously, these are different bootloaders, and should have a different version, means e.g. stk500boot_v3_mega2560.hex for the working one.

"Stk500v2" is the name of the protocol supported by this bootloader, the v2 is NOT the version of bootloader itself. I don't think that there is a separate version number for the bootloader. Although that WOULD be a good idea. (Hmm. It does have the date that the code was compiled:

Bootloader>? CPU stats
Arduino explorer stk500V2 by MLS
Compiled on = Jan 28 2013


but its size is 103kB versus 21kB for the other.

Um? Not that I could see. shows as "514 lines (513 sloc) 22.989 kb" While the newer code at shows as "file 469 lines (468 sloc) 20.964 kb " Perhaps you were comparing HTML pretty-printed web page against 'raw' file size?

Embed: It has the same file name as "this working one", but its size is 103kB versus 21kB for the other.

We have to take that with a grain of salt, as the maximum size of the bootloader on the Mega2560 is 8K bytes (see datasheet, page 330).

The idea that a chip with 256 Kb of program memory would have a 103 Kb bootloader is, if I may say, laughable.

Well, size of the .hex file will always be somewhat more than twice the size of the actual code... 20k is a reasonable .hex file size for an 8k bootloader.

hello, I need the strech by bluetooth shit, and I get it, but I must be attentive to press the reset arduino-one.

when bleutooch connects, there squeezed reset and this loads the strech.

I tried to use the wacthdog, when this is connected to a reset,
but I can not load the strech, someone has done something similar to what

#include <avr/wdt.h>

int led = 9;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by
void setup ()
  Serial.begin (115200);
    pinMode(led, OUTPUT);

//  Serial.println ("Restarted.");
wdt_reset ();   

 }  // end of setup

void loop ()

analogWrite(led, brightness);    

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade: 
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ; 
  // wait for 30 milliseconds to see the dimming effect    

}  // end of loop

Nick, re: Regarding " Tested on the Mega2560 board once I replaced the bootloader with this working one:"

Do you know if this is the bootloader in the Arduino 1.04 IDE release?

I've been battling dropped ethernet connection on my webserver application for weeks. It occurs anywhere from hours to days. The application continues to run, but over time, a client will not be able to connect. I have a Mega2560 and want to implement watchdog timer based on your example. The code verifies, but I haven't uploaded it yet to test based on the comments on this post (the need to replace the bootloader)

Is your recommendation of the bootloader you specified still valid? Any help is appreciated.

Thanks, Rich

There seem to be two booloader files shipping with 1.04 namely:

// File =  Mega2560-prod-firmware-2011-06-29.hex
// Loader start: 3E000, length: 8192
// MD5 sum = 1E 35 14 08 1F 65 7F 8C 96 50 69 9F 19 1E 3D F0 

// File =  stk500boot_v2_mega2560.hex
// Loader start: 3E000, length: 8192
// MD5 sum = D9 E6 6B 4E D1 A6 11 2C 61 8F 9B D5 5D 24 E2 13

However neither has the MD5 sum of the one that I found to work namely:

// File =  stk500boot_v2_mega2560_fixes_watchdog_problem.hex
// Loader start: 3E000, length: 8192
// MD5 sum = 8A F4 7A 29 43 A0 D8 7C DB ED 09 A3 8F 40 24 1E

I would still recommend the "fixed" one from: