0

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 !!

3
  • 2
    You can still use the watchdog without the library. Jun 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.
    – Yang
    Jun 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. Jun 30, 2015 at 16:41

3 Answers 3

3

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!
   ...
}
4
  • how to know the aruduino is resetting. I have it run for 10 mins. no sign see the display is resetting .
    – Yang
    Jun 30, 2015 at 18:18
  • 1
    You 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).
    – Gerben
    Jun 30, 2015 at 18:38
  • I add Serial.print('startup') at the setup. It did not reset the program .
    – Yang
    Jun 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
1

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.

1
  • 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.
    – Yang
    Jul 1, 2015 at 18:12
0

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

4
  • What is the advantage of this circuit over the build-in watchdog timer?
    – Gerben
    Jun 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
    – Bra1n
    Jun 30, 2015 at 18:40
  • 1
    You can check the MCUSR register (The MCU Status Register provides information on which reset source caused an MCU reset.).
    – Gerben
    Jul 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
    – Bra1n
    Jul 1, 2015 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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