Sometimes it can be useful if the Arduino UNO could reboot itself without having to push the reset button on the board.
There are at least two ways to do the job.
Solution one. The dirty solution.
This solution moves the control to the beginning of the program using an assembly statement jump.
It is not a very complete solution because it doesn’t reset all the hardware connected to the Arduino board. Thus means that default configurations of the hardware will not be restored.
Here you are the function to call to reset the Arduino.
void software_Reset() // Restarts program from beginning but // does not reset the peripherals and registers { asm volatile (" jmp 0"); }
Solution Two. The Watchdog
I think this is the clean way. It uses the watchdog to reset the board, and the job of a watchdog is the reset of a board……
On Arduino Mega 2560, the bootloader could not support the watchdog; to use it, you shall install a bootloader supporting the watchdog.
google for “arduino mega watchdog bootloader”
You can have the same problem with other Arduino boards, so before using wdt_enable, please verify that the bootloader, used by your board, supports wdt_enable
The needed structure are in the avr/wdt.h file, to enable you have to call the function wdt_enable(); it accepts as parameter the time before the board will be reset if no watchdog reset will be issued.
You can choose between several predefined values :
15mS WDTO_15MS 30mS WDTO_30MS 60mS WDTO_60MS 120mS WDTO_120MS 250mS WDTO_250MS 500mS WDTO_500MS 1S WDTO_1S 2S WDTO_2S 4S WDTO_4S 8S WDTO_8S
Then to reset the board you have only to implement a short loop, when the timeout expires the system reboots (by francisco). In the following example I have used the minimum value for the timeout. Just call software_Reboot to reboot your board.
#include
void< software_Reboot() { wdt_enable(WDTO_15MS); while(1) { } }
Solution Three. One wire solution
If you are using the watchdog timer for other useful stuff, you can use a wire to connect, via a 1K resistor,a Digital Pin (for example pin12) to the Reset Pin. Then you can use the following code:
void swhwReset() { int pin=12; pinMode(pin, OUTPUT); // sets the digital pin as output digitalWrite(ledPin, LOW); // sets the LED off }
If you’ve found useful this post, please, make a visit to my linkedin profile gg1 to help me growing my ranking.
That’s all,
Gg1.
Hi,
IIUIC, the AVR datasheet specifically does not recommend the IO pin to reset method. During the reset the AVR sets all its IO to inputs or high Z, which means the reset pulse is not held long enough ? If the data sheet says don’t do it, its probably best not to ?
Also, IIUIC, the watchdog method isn’t that straight forward either ? you have to disable the watchdog immediately after reboot, as the watchdog setting survives the reboot ? and you'll be stuck in an endless reboot cycle ?
Regards,
Jon.
Jon,
To correctly use the watchdog reset, one should reburn his/her bootloader.
Many use that method to upload a new sketch via bluetooth.
Why this code frezzes my arduino mega r3… doesnt reset… only stop!
On an Arduino Micro with a Caterina bootloader the ‘proper’ procedure is:
#include “”
void reboot(void) {
#define BOOTKEY 0x7777
#define ADRBKEY *(unsigned short *)0x0800
cli();
ADRBKEY = BOOTKEY;
wdt_reset(WDTO_15MS);
while (1);
}
hi i upload the watchdog timer code. it works. but here is a problem. now every 15ms this arduino will be restart. so i can not upload another program. i think my arduino is totally dead. please help me
My arduino freezed. i used the third method. how to fix it ? :((
No i mean i used the second method.
Solution Two. The Watchdog
Bricked my Mega!!!!
void(* resetFunc) (void) = 0;
and then in the code: resetFunc();
You’re welcome 😀