Two Ways to Reset Arduino in Software

414,987

151

56

Posted in TechnologyArduino

Introduction: Two Ways to Reset Arduino in Software

If you want to RESET Arduino from the beginning without manually pressing the RESET button, there are a few ways. Here are two ways, using minimal wiring / circuitry. 

Step 1: Using 1 Wire Connected to the RESET Pin

1. electronically, using only 1 wire connecting an OUTPUT pin (12 in this example) to the RESET pin. (see the circuit)

In this example, pin 13, connected to the internal LED pin 13 is blinking. Pin 12 gets connected to the RESET pin by one wire.
-Typically, this would be a problem because when the application starts up, all pins get pulled LOW. This would therefore disable Arduino from every running. BUT, the trick is: in setup() function, the FIRST thing that happens is we write HIGH to the pin 12, which is called our reset pin (digitalWrite(resetPin, HIGH), thereby pulling the Arduino RESET pin HIGH. 

Step 2: Using Just Software

In this example, you do not need any extra wiring. We initialize the reset function, then call reset.

void(* resetFunc) (void) = 0;//declare reset function at address 0
...
resetFunc(); //call reset 

Share

    Recommendations

    • Microcontroller Contest

      Microcontroller Contest
    • Science of Cooking

      Science of Cooking
    • Pocket-Sized Contest

      Pocket-Sized Contest
    user

    We have a be nice policy.
    Please be positive and constructive.

    Tips

    Questions

    56 Comments

    resetFunc is a pointer to an invalid address! That's the reason the device 'resets', in fact it crashes. Don't rely on this malfunction method and maybe it can damage your device at the long run.

    Well.. #1 is not really a software solution, is it, if you have to wire pins.. Also, this is not an advised solution, according to what I've read before.

    #2 Is just a hack that anyone could come up with, but still doesn't provide a real watchdog functionality (you have to trigger it, and thus can only be used as a reset method, not a WD (which seems to be what sme of you guys need).

    Let me give some details on the built-in arduino WD (yes, really) that can be used both as WD or as a reset device.

    You can set it from 15ms to 8s range. You need to reset it in software or otherwise it will trigger a reset (reverse to #2 method).

    Here's how to use it;

    #include <avr/wdt.h> //should be in any adruino IDE

    void setup() {
    wdt_disable(); //always good to disable it, if it was left 'on' or you need init time
    //do some stuff here
    wdt_enable(WDTO_8S); //enable it, and set it to 8s
    }

    void loop() {
    //do some stuff here
    wdt_delay(5000) //instead of delay, i made my own delay to reset the WDT
    }

    //this makes sure the WDT is reset immediately when entering the
    //function, but we can still benefit from a real 'delay'.
    //upon leaving the function, we reset it again.
    //i realize timing will be loose, you can always do something with
    //millies() if you need strict timing.
    //you might also need to adjust the '1000' if you WDT is shorter

    void wdt_delay(unsigned long msec) {
    wdt_reset();

    while(msec > 1000) {
    wdt_reset();
    delay(1000);
    msec -= 1000;
    }
    delay(msec);
    wdt_reset();
    }

    //to reload the board, you only have to delay > WDT
    //another option is to set the WDT very short, and do a short delay

    void wdt_reload() {
    delay(8001);
    }

    11 replies

    It is not working...

    After few seconds, LED on pin13 is blinking...

    Please make sure the board (and the bootloader!) support watchdog.

    Thanks for the reply.

    Hi Steven,

    Above code is not working. Everytime reset is happens but if the watchdog overflows it is going to infinite reset loop(not once)

    Please make sure the board (and the bootloader!) support watchdog. This seems to happen with Arduino Pro Mini.

    I'm using above code in arduino Atmega2560

    This is interesting. Is the reset by the watchdog timer the same as if you would press the reset button?

    there are differences, can't remember exactly. I think it has to do with some pin state/config after the reboot.

    this has been discussed on the arduino playground. see https://playground.arduino.cc/Main/ArduinoReset

    I am sorry to say this, but many times the comments are much more useful than the article itself. Thank you.

    Thank you stevenk37 for an Arduino Watchdog. It is does the trick I need.

    It would be good if you would create a separate entry about it cause not all people read through comments :)

    http://www.atmel.com/webdoc/avrlibcreferencemanual/FAQ_1faq_softreset.html

    The second method crash the Arduino, the Leonardo (Pro Mini) at least. So I think that is an hack and not a real solution. Normally, a null pointer (aka adress 0) is an unusable invalid address, when call or use a null pointer to access things, strange things can happen. In fact a bad instruction that can cause a crash or a reboot. Don't use it, it is not reliable.

    Isn't a software reset a bit scary?

    Maybe you can answer this: if the loop simply called the reset function every time. then you could never stop it, or am I mistaken? Is there something in the arduino PC app that allows you to break into it?

    2 replies

    Yup its every bit scary. If you mistakenly put in some bad logic and burn it on the board and reset gets called in every loop your board will not remain programmable anymore!

    Well it depends on your software logic. Lets say you are controlling your arduino from smartphone via wifi/bluetooth. You send a command from your phone to change (lets say) the wifi ssid or password it is connecting to.... You get that command in arduino, store the new ssid/password in eeprom or sd card and RESET your arduino. Now when your arduino resets it will get the data from eeprom/sd card and connect with new wifi credentials.