Instructables
Picture of 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. 

 
Remove these adsRemove these ads by Signing Up

Step 1: Using 1 wire connected to the RESET pin

Picture of 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. 
subnet17 days ago

thanks, very helpful :-)

the method 2 seems to work for me even out of a for loop, if using it in a if statement, but only if resetFunc function is declared before setup.

NomanAhmed33 months ago

it really helped thanks

laptophead8 months ago

I tried to apply your sketch to reset the processor as soon as a number count was larger than 33. It did not work, I wonder if you can help.

Thanks a lot

Mitch

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

void loop()
{




for (int i=0; i <= 255; i+=3){
Serial.println(i);
delay(1);
}
resetFunc();

if (i>33) {
resetFunc();
}


}

you have to call the resetFunc() in side the "for" loop like.

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


void loop()

{


for (int i=0; i <= 255; i+=3){
Serial.println(i);

if (i>33) {
resetFunc();
}

delay(1);
}


}

Why should resetFunc() be called inside a loop? Why does it not work if it is called outside the "for" loop?

in the cord above ........ int "i" variable is declared inside the FOR loop, and infarct the variable that drives the FOR loop(till "i" reach 255)... And it is a local variable so it can not be accessed outside the loop... also in this case if some how we could access the "I", by the time FOR loop finish it would be 255 :)

Hwolfw laptophead8 months ago

The for loop doesn't include what you want it to do. The way you coded it, it will continue up to 255 then reset, never reaching the if statement.

void(* resetFunc) (void) = 0;//declare reset function at address 0
void loop()
{
for (int i=0; i <= 255; i+=3){
if (i>33) { //needs to be within the loop
resetFunc(); //to check i for greater than 33
}
Serial.println(i);
delay(1);
}
resetFunc();

//outside of loop, will never be looked at.
// if (i>33) {
// resetFunc();
// }
}

Ok, so teach me, why would I need to do this / want to do this? Why wouldn't this just loop, resetting over and over?

This post is EXACTLY what I needed! The software reset (Step 2 in this instructable) works perfectly for what I need to do. I am using an nRF24L01+ 2.4GHz transceiver, with the RF24 library by ManiacBug, and I used the above software reset code to make my program more robust. In certain circumstances (for example, a brown-out on the 3.3V power pin to the RF24L01+ module), the transceivers lose connection and stop working. I tried for many hours to get the 2.4GHz modules to reset themselves and reconnect, but I could not figure it out. Finally, I wrote a code that says to each device, "if you haven't received a successful packet in 100ms" (average time to do so is 5ms for my code), "then do an Arduino software reset." This works! Finally, I can get the modules to automatically reconnect if they have an anomaly that causes them to lose connection. So...to answer your question, this is where the software reset code is useful!

~Gabriel

electricrcaircraftguy.blogspot.com

PS. more info. on the module is here: http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo

You're right. In this example it will loop indefinitely.

It could have more useful applications in larger programs though. For example..

I once wrote a program when I was in geometry for a watch to extend its functionality. After every set of numbers I input I wanted it to give me an answer, wait 5 seconds and then reset itself so it was primed up for another set of numbers.

Admittedly this was a very lazy way for me do accomplish this but it got the job done.

Hope that helps.
linker21011 months ago
Is there a way reset millis to a previous time besides zero using this function?
retrolefty1 year ago
If you look in any Atmel AVR datasheet they will explicitly tell you that this in not an acceptable method to reset a chip. Upon reset all I/O pins are set to input mode so you lose the LOW output from pin 12 in this example. This doesn't allow the reset low pulse to met the minimum low pulse width it has to be for a valid reset process. So use at your own risk. There is a valid way to reset a AVR chip in software without even needing a wired added as here. Just enable the watch dog timer and let it time out which will generate a true reset. Then in the setup function disable the WDT.
Lefty.
BooRan2 years ago
I've used this technique before just to see if it worked and it seemed useful. One comment though is that i would recommend using a resistor in there. Sending a high signal straight to the Reset pin could cause a short circuit and fry your chip. Not sure what would work better though, a pull up resistor from the reset pin to high, and allowing the "resetPin" to pull it low, or just placing a resistor between the two points.
pdemetrios2 years ago
There are and the WDT method for all the micros with WDT .
Setup the WDT and then make an infinity loop

WDTCSR=(1< WDTCSR= (1< for(;;)
and in the 16ms the mcros will reset and the MCUSR variable will
have the value WDRF
With your software the MCUSR will have an Unspecified value.
sorry but the code has modified the correct is
WDTCSR=(1<<WDE) | (1<<WDCE) ;

    WDTCSR= (1<<WDE)

for(;;)
nice, never thought about this, thanks for posting!
billhorvath2 years ago
Neato! Thanks for the tips!