How to Reset Arduino from the Code

Sometimes we want to reset Arduino just to make sure all functions running properly. Or if Arduino in a ‘hangs’ state because of something wrong, of course we have to reset it. We can’t reset Arduino manually especially if the Arduino-based control system that we have built is mounted somewhere far away. The only way is by creating auto reset system or by detecting if the Arduino in hangs state.

For this tutorial we will discuss about how to reset the Arduino from the code or script not by pressing the hardware reset button manually. Reset Arduino periodically is very important to do, mainly on the stand alone Arduino that works independently as a 24 hours-control system. This will prevent Arduino be hangs and always make it 'fresh' back.



To reset Arduino from the code, you can define a function at address ‘0’ in Arduino sketch. So when you call this function anywhere within your main script, then basically you're going to address ‘0’ in your Arduino. It will produce same effect when you press reset button on Arduino board (hardware reset). For more details, following is example of 'my_reset' function to reset Arduino from the code

void (* my_reset) (void) = 0;

void setup () {
  Serial.begin (9600);
  Serial.println ( "in the Arduino setup block");
  Serial.println ( "http://www.arduinogeek.com");
  delay (200);
}

void loop ()
{
  Serial.println ( "looping block Arduino");
  delay (1000);
  Serial.println ( "ready to reset Arduino");
  Serial.println ();
  delay (1000);
  my_reset ();
  Serial.println ( "never reach here because I've been reset ');
}

Notice how to define 'my_reset' function in order to be at address ‘0’ in Arduino (row [1]) and the way to calling the function to reset Arduino (row [15]). For more details, please open Serial Monitor window through Tools-Serial Monitor menu. Note that line [16] above will never be executed (won’t displayed in Serial Monitor window) because Arduino has been reset first (row [15])

You can combine my_reset() function above with Time.h/TimeAlarm.h library to define auto reset Arduino system periodically within a specified period, such as once a week or once a month to refresh your Arduino system back.

At the beginning of this tutorial, I had mentioned about Arduino detection system if in a hangs state. Arduino must be able to detect itself while not working properly and how to handle it. One of many ways is by utilizing WDT (Watch Dog Timer), which is a kind of timer that will triggers an interrupt which will reset the processor (in this case is microcontroller AVR ATMega the Arduino board) when overflow. So Arduino with WDT reset technique is different from the way of ‘my_reset’ function above. If using WDT, a reset system is triggered by a overflowing timer, but if using ‘my_reset’ function, reset will occur when we call the function. So, WDT is a pe-reset mechanism in Arduino when hang was happened (post incident), while the ‘my_reset’ function is a reset mechanism in Arduino before the hang (preventive/pre-incident). How/What the WDT is and when we use it will be discussed more detail in next tutorial. Stay tune on my blog J.
Previous
Next Post »