22

Is it possible to reset an Arduino (i.e., to reboot it) from code (i.e from the sketch itself)? I know that is possible with a special circuit but is there a chance to make it just with code?

Below is my code and the comment //reset is where I want to force a reset.

#include <TrueRandom.h>

int i;
int randSeed;
long randNumber;

void setup(){
  Serial.begin(9600);
  Serial.println("20 pseudo Zufallszahlen:");
  for (i=1;i<=20;i++) Serial.print(random(10));
  Serial.println();
  Serial.println();
  //randomSeed(TrueRandom.random());
  randSeed = analogRead (A0);
  randomSeed(randSeed);
  Serial.print("Der 'seed' Wert: ");
  Serial.println(randSeed);
  Serial.println();
  Serial.println("20 Zufallszahlen mit analogem 'seed' Wert:");
  for (i=1;i<=20;i++) Serial.print(random(10));
  Serial.println();
  Serial.println("---------------------------");
  Serial.println();
  delay(500);
  //reset  
}

void loop() {
}

I want to reset the micro-controller at the end of the setup function to show the effect of random numbers with and without a seed.

2 Answers 2

25

There three ways to accomplish this. (last is my favorite)

1) Jumper an unused IO to the RESET pin. Leave it as INPUT for normal run, As it is externally pulled high. And when desired to reset set it as LOW and Output. (bang its rebooting).

setup() {
  ...
  pinMode(PINtoRESET, INPUT);    // Just to be clear, as default is INPUT. Not really needed.
  digitalWrite(PINtoRESET, LOW); // Prime it, but does not actually set output. 
  ...                            // Does disable 10K pull Up, but who cares.

then when desired...

...
  pinMode(PINtoRESET, OUTPUT);   // lights out. Assuming it is jumper-ed correctly.
  while(1);                      // never gets here.

2) Jump to beginning of the code.

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

But be careful, this does not perform a true reset, in that all the registers ARE NOT DEFAULTED. Rather they and the IO are left as is. Where somethings from the bootloader and then the heap will be initialized. And reset are not!

3) Use the watchdog. The SoftReset library makes it easy. Although it is not difficult to implement directly. Shown below..

#include <avr/wdt.h>
...
setup() {
  ...
  MCUSR = 0;  // clear out any flags of prior resets.
  ...

then when desired...

...
wdt_enable(WDTO_15MS); // turn on the WatchDog and don't stroke it.
for(;;) { 
  // do nothing and wait for the eventual...
} 
...
14
  • 2
    From everything I've read, the first option is not recommended. Options 2 and 3 are fine.
    – sachleen
    May 10, 2014 at 5:01
  • 2
    @sachleen: Please say more about that (opt-1).
    – JRobert
    May 19, 2014 at 14:14
  • 2
    Option 1. or a variant can be a clean enough way to do this as long as power on transient conditions are designed for. Feb 25, 2015 at 12:32
  • 1
    It should be noted that option 2 does not run the bootloader. Aug 13, 2016 at 19:57
  • 2
    Option 3 works only with bootloaders that have WDT enabled. It puts board into reset loop otherwise.
    – zgoda
    Mar 28, 2017 at 15:22
4

In case you have the original Arduino bootloader which you want to execute as a part of the reset, you can do a SW reset by jumping to the bootloader reset address (0x7800 on ATmega328p boards)

void reset() { asm volatile ("jmp 0x7800"); }

The watchdog reset approach will not work because of a bug in the bootloader. Here's a note from ATmega328P Datasheet page 45:

Note: If the watchdog is accidentally enabled, for example by a runaway pointer or brown-out condition, the device will be reset and the watchdog timer will stay enabled. If the code is not set up to handle the watchdog, this might lead to an eternal loop of time-out resets. To avoid this situation, the application software should always clear the watchdog system reset flag (WDRF) and the WDE control bit in the initialization routine, even if the watchdog is not in use.

This is exactly what will happen after a watchdog reset on a system with Arduino bootloader.

5
  • Yes, I have observed the eternal loop of timeouts, while answering the question, "I wonder if the boot loader resets the WDT" Why not just do "jump 0x0" ? May 18, 2021 at 18:44
  • @EricNelson Jumping to 0x0 will not run the bootloader, which is one of the two major use cases for a software reset. The second use case, resetting the system to a known state, is not possible without a bootloader with WD support or connecting the reset pin to a GPIO. May 19, 2021 at 11:16
  • @DmitryGrigoryev For some reason, Visual Studio thinks this is invalid syntax. It underlines the semicolon in red saying it expected a '('. The code will compile, however. I'm guessing it's some sort of discrepancy between Visual Studio and Visual Micro (the arduino plugin for VS). However, the arduino also gets reset if I leave out the volatile keyword (and that way, Intellisense doesn't complain). Are there any side effects to this?
    – Bas
    Aug 17, 2023 at 1:37
  • 1
    @Bas volatile prevents the compiler from over-optimizing your code. Technically, the compiler is allowed to eliminate the reset function entirely as it has no side effects visible to the C compiler. Of course, modern compilers are usually smart enough not to do this (e.g. volatile is not necessary with GCC which knows it's forbidden to optimize jmp), but strictly speaking the reset is not guaranteed to happen without volatile on a random compiler. Aug 17, 2023 at 8:41
  • 1
    Thanks @DmitryGrigoryev! Turns out that Intellisense thinking it was invalid syntax was indeed an older version of Intellisense in combination with VMicro. Updating VMicro resolved the issue. Either way, it now works with the volatile keyword as well.
    – Bas
    Aug 29, 2023 at 8:16

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