collapse

Author Topic: Arduino Software Reset  (Read 8866 times)

Killer Angel

  • Member
  • *
  • Posts: 62
Arduino Software Reset
« on: October 10, 2016, 08:13:42 AM »
The other day I was looking for a way to reset the Arduino by software.

Found several links that they all mentioned at most 3 ways:
  • connect an unused IO pin to the RESET pin and pull the pin LOW to reset (not recommended)
  • jump to the beginning of the code
  • using a watchdog interrupt

On my Arduino MEGA I tried using the 3rd method, but it got stuck in a bootloader loop. Apparently it needs a newer bootloader.
I settled to the 2nd method with a small timeout so that it can finish the current request (being used in a web server)


Methods in detail.

Method 1 (not recommended):
connect 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.
Code: [Select]
#define PINtoRESET XXX // whatever pin you like and have available
...
setup() {
  ...
  pinMode(PINtoRESET, INPUT);    // just to be clear, as default is INPUT
  digitalWrite(PINtoRESET, LOW); // set it to LOW, but does not actually set output
  ...                            // disables the 10K pull-up
}

and then whenever you need to reset you do the following:
Code: [Select]
  ...
  pinMode(PINtoRESET, OUTPUT);   // RESET!
  while(1);                      // never gets here. NOT NEEDED OF COURSE


Method 2 (not real reset, only starts the sketch from the beginning):
declare a function as:
Code: [Select]
void(* resetFunc) (void) = 0;  // declare reset fuction at address 0or as:
Code: [Select]
void resetFunc() {
  // Restarts program from beginning but
  // does not reset the peripherals and registers
  __asm__ __volatile__ ("jmp 0x0000");
}

and when needed call it like:
Code: [Select]
resetFunc(); // call reset function
while(1);    // never gets here. NOT NEEDED OF COURSE


Method 3 (real reset, but some bootloaders don't support it correctly):
first you have to include a header file:
Code: [Select]
#include <avr/wdt.h>
In that file you will find several WDTO_XXX variables: http://www.nongnu.org/avr-libc/user-manual/group__avr__watchdog.html

As soon as you enter the setup function you should reset the watchdog error code and interrupt:
Code: [Select]
setup() {
  uint8_t mcusr_state = MCUSR;  // store the initial state of the Status register
  MCUSR = 0;                    // clear all reset flags
  wdt_disable();                // watchdog may be configured with a 15 ms period so must disable it before going any further
  ...
}

and then:
Code: [Select]
...
wdt_enable(WDTO_15MS); // turn on the watchdog (use any time out you want).
for(;;) {
  // do nothing and wait for the reset
}

There is also a library for this method: https://github.com/WickedDevice/SoftReset
Killer Angel [:|]

bdk6

  • Guest
Re: Arduino Software Reset
« Reply #1 on: October 10, 2016, 09:58:50 AM »
"Method 1 (not recommended):"

Why not?  ( I suspect I know the answer you will give, but want to verify.)

MEgg

  • Sr. Member
  • *
  • Posts: 262
Re: Arduino Software Reset
« Reply #2 on: October 10, 2016, 08:15:14 PM »
Not a real software reset:
How long do the Arduinos have to be without power to make a complete reset?
I control a relay with the Spider card to switch on 12V  of the motor driver card later than the 5V of the motor driver card.
In the same way I could switch off power of the Spider card, thus rebooting the spider card.
Tried to find information about the timings but had no success.
1st project: Dagu 5 Rover + Dagu - 4 Channel DC Motor + Red Back Spider robot controller + Raspberry B+
Chassis + wheels: https://picload.org/image/dggroior/20150831_028.jpg
current: https://www.keepandshare.com/userpics/m/a/r/k/usegg/2016-04/sb/img_3480-79682018.jpg

bdk6

  • Guest
Re: Arduino Software Reset
« Reply #3 on: October 10, 2016, 09:05:30 PM »
"In the same way I could switch off power of the Spider card, thus rebooting the spider card."

well, maybe it's just me, but how you gonna turn it back on?

MEgg

  • Sr. Member
  • *
  • Posts: 262
Re: Arduino Software Reset
« Reply #4 on: October 11, 2016, 04:05:25 PM »
"In the same way I could switch off power of the Spider card, thus rebooting the spider card."

well, maybe it's just me, but how you gonna turn it back on?

I should add that there is also a Raspberry on my robot which could do the reboot of the Arduino,
but you are right: this is just moving the thing of reboot to another hardware part.
1st project: Dagu 5 Rover + Dagu - 4 Channel DC Motor + Red Back Spider robot controller + Raspberry B+
Chassis + wheels: https://picload.org/image/dggroior/20150831_028.jpg
current: https://www.keepandshare.com/userpics/m/a/r/k/usegg/2016-04/sb/img_3480-79682018.jpg

Killer Angel

  • Member
  • *
  • Posts: 62
Re: Arduino Software Reset
« Reply #5 on: October 12, 2016, 06:08:39 AM »
"Method 1 (not recommended):"

Why not?  ( I suspect I know the answer you will give, but want to verify.)

All I could find is:
Quote
The watchdog is the only method recommended by Atmel http://support.atmel.no/bin/customer?=&action=viewKbEntry&id=21

Even connecting reset to a digital pin is not reliable according to Atmel because the digital pin goes tristate and releases reset too soon. You need an external circuit that will hold reset low for sufficient time.
quoting from: https://forums.adafruit.com/viewtopic.php?f=8&t=15435#p75892
(link in the quoted message doesn't work)

but I couldn't find where Atmel does recommend the watchdog usage.
Killer Angel [:|]

bdk6

  • Guest
Re: Arduino Software Reset
« Reply #6 on: October 12, 2016, 06:23:56 AM »
I'm not certain about what Atmel recommends or doesn't recommend, although I suspect that is accurate.  However, the GPIO pin going tristate too soon is true, but easily fixed with a couple of components.  A capacitor and perhaps a resistor and/or a diode can be placed to hold the reset low long enough.  That is how we used to HAVE to do it.  Micros with internal reset circuits are a relatively new development.

Consider the simple solution of simply placing a cap to ground on the reset line.  When the power is first applied the cap will be low.  Assuming there is a pullup resistor (there should be!) the cap will be low to start, holding the chip in reset.  But it will charge through the pullup and at some later time become high, allowing the chip to run.  If a GPIO is now connected to reset along with the cap, it can be set low to discharge the cap.  It will discharge quite fast because of the current handling ability of the GPIO.  It will effectively be discharged before the GPIO goes tristate.  Now the cap is back to the initial power-on state of discharged, holding the chip in reset until the pullup can charge the cap.  Typically a cap of around 1 uF will work, but that varies.  An external pullup resistor may need to be added and, depending on what else is connected,  a diode to isolate the GPIO from other circuits connected to reset.

bdk6

  • Guest
Re: Arduino Software Reset
« Reply #7 on: October 12, 2016, 07:03:58 AM »
By the way, I am NOT recommending you use the method I mentioned.  I'm only listing it as an option.

Killer Angel

  • Member
  • *
  • Posts: 62
Re: Arduino Software Reset
« Reply #8 on: October 12, 2016, 09:58:51 AM »
Searching a bit around I found this Application Note from Atmel: http://www.atmel.com/Images/Atmel-2521-AVR-Hardware-Design-Considerations_ApplicationNote_AVR042.pdf

Look at Chapters 3 (page 6) and 3.1 (page 7) and related figures 3-1 and 3-2.

@bdk6: is this what you are suggesting after all?
Killer Angel [:|]

bdk6

  • Guest
Re: Arduino Software Reset
« Reply #9 on: October 12, 2016, 10:12:43 AM »
Not really.
The capacitor would be oriented the same way and the pullup also as shown.  However, the diode would be inline between the cap and the GPIO pin:

GPIO ------------|<|-----------|-------RESET
                       diode                          |
                                               ___
                                               ___
                                                 |
                                               GND
That connection allows the GPIO to discharge the cap, but not to charge it or fight with a high level controlled by another device (FTDI?)

MEgg

  • Sr. Member
  • *
  • Posts: 262
Re: Arduino Software Reset
« Reply #10 on: October 12, 2016, 05:20:15 PM »
1st project: Dagu 5 Rover + Dagu - 4 Channel DC Motor + Red Back Spider robot controller + Raspberry B+
Chassis + wheels: https://picload.org/image/dggroior/20150831_028.jpg
current: https://www.keepandshare.com/userpics/m/a/r/k/usegg/2016-04/sb/img_3480-79682018.jpg

bdk6

  • Guest
Re: Arduino Software Reset
« Reply #11 on: October 12, 2016, 05:54:20 PM »
Please never do that without warning.  I can never un-see that post.  I will have nightmares for a LONG time.

Tor

  • Member
  • *
  • T
  • Posts: 21
Re: Arduino Software Reset
« Reply #12 on: October 13, 2016, 06:46:33 AM »
Wouldn't it be better to combine the programmed reset with a little bit of hardware which produces a perfect reset signal? The DS1813 (or DS1818 for 3.3V). Great for push-button resets, but also handles power failures.  http://www.mouser.com/ds/2/256/DS1813-93440.pdf

bdk6

  • Guest
Re: Arduino Software Reset
« Reply #13 on: October 13, 2016, 07:27:38 AM »
I concur that one of those reset chips (or one of the many like them) is a MUCH better alternative than the el-cheapo cap and diode I outlined.  The main advantage to what I show is most people probably already have the parts.  Good for quick and dirty without having to order parts. 
Again, the reset IC is MUCH better!

I do, however, think using the watchdog is the best solution for the specific case the op needs.  But it requires (apparently) reflashing with a different bootloader to make it work.

MEgg

  • Sr. Member
  • *
  • Posts: 262
Re: Arduino Software Reset
« Reply #14 on: October 13, 2016, 01:40:05 PM »
I concur that one of those reset chips (or one of the many like them) is a MUCH better alternative than the el-cheapo cap and diode I outlined.  The main advantage to what I show is most people probably already have the parts.  Good for quick and dirty without having to order parts. 
Again, the reset IC is MUCH better!

I do, however, think using the watchdog is the best solution for the specific case the op needs.  But it requires (apparently) reflashing with a different bootloader to make it work.

Actually not all bootloaders seem to be affected.
But to prevent bdks nightmares ;-) here is the double-repetition of the warning ( 1 warning in posting of Killer
Angel @Method3, 2nd "warning" at the bottom of the page that I posted):
"Certain Arduino bootloaders do not deactivate the watchdog, which may send the
bootloader into a infinite loop. setup() will never be executed then."

I hope you can sleep better now, bdk.
 ;D

https://andreasrohner.at/posts/Electronics/How-to-make-the-Watchdog-Timer-work-on-an-Arduino-Pro-Mini-by-replacing-the-bootloader/
but I never tested that.
1st project: Dagu 5 Rover + Dagu - 4 Channel DC Motor + Red Back Spider robot controller + Raspberry B+
Chassis + wheels: https://picload.org/image/dggroior/20150831_028.jpg
current: https://www.keepandshare.com/userpics/m/a/r/k/usegg/2016-04/sb/img_3480-79682018.jpg

 

* Search


* Recent Topics

2D Side Scroller Cyberpunk themed by Killer Angel
[November 12, 2019, 07:56:15 AM]


The unnamed (yet) quatruped spider project by tinhead
[November 09, 2019, 05:58:16 AM]


Ebot SL by erco
[October 31, 2019, 04:27:36 PM]


Discord anyone? by tinhead
[October 20, 2019, 05:35:59 AM]


MKS Gen L 1.0 by lllloydo
[October 06, 2019, 01:24:26 AM]


Saturn pics anyone by viswesh
[September 28, 2019, 11:55:53 PM]


Hello Robot Rebels by Passixs
[September 15, 2019, 10:19:10 PM]


All Ya'll. And me. by James
[September 09, 2019, 10:35:19 PM]


MSD - Motorized solder dispenser (Work in progress) by James
[September 09, 2019, 08:45:29 PM]


It may be dangerous to work in the virtual realm by ZeroMax
[September 05, 2019, 05:55:16 PM]


"1984 Nixie Time" by Gareth
[August 25, 2019, 12:21:58 PM]


Just wanted to say Hi. by ErwinCanto
[August 18, 2019, 10:11:28 PM]


SDR (Software Defined Radio) by Gareth
[August 14, 2019, 01:12:21 PM]


PLX-DAQ by AmandaG
[August 06, 2019, 01:58:42 PM]


DIY Yellow Servo by AmandaG
[August 06, 2019, 01:08:09 PM]

* Recent Posts

Re: 2D Side Scroller Cyberpunk themed by Killer Angel
[November 12, 2019, 07:56:15 AM]


Re: The unnamed (yet) quatruped spider project by tinhead
[November 09, 2019, 05:58:16 AM]


Re: The unnamed (yet) quatruped spider project by Nemesis
[November 09, 2019, 03:29:02 AM]


Re: Ebot SL by erco
[October 31, 2019, 04:27:36 PM]


Re: 2D Side Scroller Cyberpunk themed by Killer Angel
[October 31, 2019, 09:57:48 AM]


Re: 2D Side Scroller Cyberpunk themed by Killer Angel
[October 31, 2019, 06:01:53 AM]


Re: 2D Side Scroller Cyberpunk themed by maelh
[October 30, 2019, 03:06:28 AM]


Discord anyone? by tinhead
[October 20, 2019, 05:35:59 AM]


The unnamed (yet) quatruped spider project by tinhead
[October 20, 2019, 05:28:14 AM]


Re: 2D Side Scroller Cyberpunk themed by Killer Angel
[October 17, 2019, 09:44:06 AM]


Re: 2D Side Scroller Cyberpunk themed by Killer Angel
[October 17, 2019, 08:16:11 AM]


Re: 2D Side Scroller Cyberpunk themed by Killer Angel
[October 14, 2019, 08:52:20 AM]


2D Side Scroller Cyberpunk themed by Killer Angel
[October 14, 2019, 03:32:40 AM]


Re: MKS Gen L 1.0 by lllloydo
[October 06, 2019, 01:24:26 AM]


Re: Saturn pics anyone by viswesh
[September 28, 2019, 11:55:53 PM]