I'm just a tinkerer learning Arduino with my son and I was surprised that when I plugged the USB cord back in to the Arduino the program was still running and I thought to myself, "it's a good thing I didn't just blow anything up."
So, what's the normal way people call it a day with these things? Do you usually load up the bare-bones do nothing sketch or just unplug everything?
-
4What do you mean by "when I plugged the USB cord back in to the Arduino the program was still running", what behavior did you observe and what behavior did you expect? Was anything connected to the Arduino?jippie– jippie2014年02月26日 18:35:57 +00:00Commented Feb 26, 2014 at 18:35
-
@jippie yeah, I had a bunch of LED's plugged in and they all lit up. For some reason I thought the programs were loaded into memory and would be gone on reboot, but since that doesn't exist....Peter Turner– Peter Turner2014年02月26日 18:42:28 +00:00Commented Feb 26, 2014 at 18:42
-
7The programs are loaded into flash memory, not RAM, so they are kept even after powerdown.Ricardo– Ricardo2014年02月26日 20:05:59 +00:00Commented Feb 26, 2014 at 20:05
6 Answers 6
When booted, the Arduino will run whatever program was put on it last. Solution: Don't turn it on if you don't want it to run.
When out of the box, the Arduino usually runs the Blink program. So you can just load that and call it a day if you want to "reset" it.
Or have a loop program with a sleep command.
If you've been messing with the USB bootloader via the icsp pins, this isn't enough though, you'll need to reinstall the bootloader. But if you've been uploading programs via usb or directly using a programmer, there is no issue with uploading Blink or some other dummy program to reset it.
-
is it possible to have it run a self-destructive program with nothing plugged in to the voltage terminals?Peter Turner– Peter Turner2014年02月26日 20:38:59 +00:00Commented Feb 26, 2014 at 20:38
-
@PeterTurner when there's no power to it? No. But you can add a program that misuses the pins, and if you leave it on some damage can be called (not much unless the pins are connected)Manishearth– Manishearth2014年02月26日 20:44:16 +00:00Commented Feb 26, 2014 at 20:44
If safety is a major concern, then you should almost always load a stop* sketch onto the board before calling it a day. This would be a good idea, for example, when your board is semi-permanently a part of a larger system. You could do so by:
- Upload the Bare Minimum sketch found in File -> Examples -> Basic in the arduino-ide
- Upload a sketch with a large delay in loop.
#include <limits.h>
void setup()
{}
void loop()
{
delay(ULONG_MAX);
}
- Upload a sketch with a call to
exit()
fromsetup()
(Thanks @Peter!)
void setup()
{
exit(0);
}
void loop()
{}
*Stop is simply figurative. The board doesnt really stop running.
-
2Could you get a similar (and more permanent) effect by calling
exit()
from withinsetup()
orloop()
?Peter Bloomfield– Peter Bloomfield2014年03月02日 15:52:02 +00:00Commented Mar 2, 2014 at 15:52 -
@PeterR.Bloomfield Can't believe I forgot about that!asheeshr– asheeshr2014年03月02日 15:57:49 +00:00Commented Mar 2, 2014 at 15:57
-
Why bother with either the exit or the delay? Just have
void setup () { } void loop () { }
- in any case internally the processor is just looping around "doing nothing useful".2015年08月18日 21:44:54 +00:00Commented Aug 18, 2015 at 21:44
The major thing that can go wrong is if you put your Arduino aside for a couple of weeks, forget what sketch (code) is loaded onto it, then get back to work on it, plug in hardware (like a switch, LED or motor) and have it unexpectedly light up / turn on, because the previous code addressed the pin it was connected to.
Solution 1. Load the new code first.
A simple and safe technique is to load the new code that you are working on today before you plug any hardware in. Now you know what the code does, what pins it uses, and you then plug the hardware in afterwards (preferably turning the Arduino off first by unplugging it).
Then when you reconnect power, it runs the sketch you uploaded, you have your hardware connected and all should be well.
Solution 2. Load a "do nothing" sketch.
You can upload this short sketch:
int main () { }
It "does nothing". All pins will remain as inputs, so you can safely then attach your hardware, and then start coding your new project.
Safety first
A good rule is to not plug or unplug hardware (eg. LEDs, switches, motors) with the power applied. If you get distracted and plug into the wrong socket on the board you might immediately damage either what you are connected, or the Arduino, or both.
With the power off, you can then do a last-minute check that everything is connected to the right place, you have your polarities correct, and so on.
Also make sure you don't have static electricity on your body, particularly if you are wearing a jumper or something fluffy, or are sitting on carpet.
Personally I always reach over and touch some metal part nearby to "ground" myself, before reaching for the Arduino. An example is the metal case of a PC, a headphone socket, or something like that.
If, after connecting power to the Arduino (eg. by the USB cable) you do not immediately see the power LED come on, disconnect the power immediately! You may have shorted something out, and the less time you do that for, the better.
In the Arduino IDE, go to examples, select Eeprom, select eeprom clear and then upload the sketch.
When it is the LED on pin 13 will turn on, you are done.
NB. This may take some time depending on the size of the EEPROM in the Arduino.
-
So what is the point? Running something that does not touch I/O (but clears the EEPROM at every power on)?Peter Mortensen– Peter Mortensen2017年04月17日 02:18:48 +00:00Commented Apr 17, 2017 at 2:18
-
Have you actually tried this? Pin 13 does not appear to be set up as output (Arduino IDE 1.6.5)Peter Mortensen– Peter Mortensen2017年04月17日 02:20:39 +00:00Commented Apr 17, 2017 at 2:20
My biggest concern when this happens is I've usually wired the Arduino for my new project and I'm plugging in the USB for the initial download of the new sketch. I worry, having no idea what the previous program is going to try, being connected to totally different hardware.
So far nothing bad has happened and I don't think I'll adopt burning BLINK each time I put one of my Arduinos back in the bin. It's probably just a matter of time though.
If the arduino board is connected to any other device then unplug everything and just connect the arduino with the computer using the usb cable and try to upload new sketch i hope it will help.
-
Welcome to Arduino SE! Is there any reason this answer is better than the other provided answers? Thanks!Anonymous Penguin– Anonymous Penguin2015年08月18日 20:55:31 +00:00Commented Aug 18, 2015 at 20:55