I am working on the temperature project using arduino to collect temperature. But sometimes arduino hang there so I have to unplug cord and plug it back in to restart the arduino. I do not when it is going to hang up again. So I decide to do some research on the internet but no luck for that. Here is the article I read about resetting arduino code with watchdog timer library.(unfortunately, it only work for arduino uno) If someone has any idea how to reset arduino mega 2560 code. Please help !!
-
2You can still use the watchdog without the library.– Ignacio Vazquez-AbramsJun 30, 2015 at 16:23
-
@IgnacioVazquez-Abrams Do I need to wire anything to my arduino or other like what they called bootloader (I do not know what is that) ? The watchdog is a library, right ? how to define it without the library ? I am guessing it is going to give me an error.– YangJun 30, 2015 at 16:39
-
The watchdog is a MCU peripheral. You enable it by setting the registers appropriately. See the ATmega2560 datasheet, "System Control and Reset" section, "Watchdog Timer" and "Register Description" subsections.– Ignacio Vazquez-AbramsJun 30, 2015 at 16:41
3 Answers
avr-libc already has the appropriate functions created for you. Just include <avr/wdt.h>
.
Sample usage
#include <avr/wdt.h>
void setup()
{
wdt_enable(WDTO_8S);
...
}
void loop()
{
wdt_reset();// make sure this gets called at least once every 8 seconds!
...
}
-
how to know the aruduino is resetting. I have it run for 10 mins. no sign see the display is resetting .– YangJun 30, 2015 at 18:18
-
1You can detect resets using the
MCUSR
register. But it's probably easier to add a Serial.print('startup') to the setup function and keep the serial console open. If 'startup' appears more than once a reset has occurred. Alternatively add a buzzer that beeps on startup(/reset).– GerbenJun 30, 2015 at 18:38 -
I add Serial.print('startup') at the setup. It did not reset the program .– YangJun 30, 2015 at 19:04
-
2@Yang: It won't reset unless the code fails to reset the watchdog. Jun 30, 2015 at 22:38
I tried Gerben's suggestion with a display:
#include <avr/wdt.h>
void setup ()
{
Serial.begin (115200);
Serial.println ();
Serial.println (F("Code starting ..."));
wdt_enable(WDTO_8S);
} // end of setup
void loop()
{
// wdt_reset();// make sure this gets called at least once every 8 seconds!
}
That does indeed print "Code starting ..." every 8 seconds or so.
But sometimes arduino hang there so I have to unplug cord and plug it back in to restart the arduino.
You are better off working out why. There is probably a bug. Fix the bug rather than just restarting the Arduino.
-
let me tried to run the code for several weeks to see if there temperature go off. By the way, i think the code has no problem but maybe somehow the temperature hit "0" or even crazier "99999", I still cannot figure that out is the hardware problem or something else. So that is why I tried to reset my code automatically.– YangJul 1, 2015 at 18:12
You could also use an external circuit to reset the arduino when 'heartbeat' pulses are missed. You can even detect when a reset has occured and report accordingly. See here for an example
-
What is the advantage of this circuit over the build-in watchdog timer?– GerbenJun 30, 2015 at 17:43
-
You can know when it's happened and record the event which is difficult if not impossible to do using the internal watchdog– Bra1nJun 30, 2015 at 18:40
-
1You can check the
MCUSR
register (The MCU Status Register provides information on which reset source caused an MCU reset.
).– GerbenJul 1, 2015 at 12:37 -
Yes (and I'm admittedly getting out of my depth here) doesn't the boot loader also use the internal watchdog so you wouldn't know if it was caused by the reset switch/power on or a watchdog event– Bra1nJul 1, 2015 at 13:09