Prevent sketch form running when USB plugged in (i.e. sketch upload complete)?

Other than a physical switches, what can I do to prevent my sketch from running when plugged into USB? I want to load my sketch, but I don't want the sketch to start and pull amps from my computer. I just want to load my sketch, and then nothing happens until I unplug from the computer and hookup my battery or other power supply. All I can think is:

  1. Put a delay in setup() so I have time to unplug from USB.
  2. Do some kind of serial connection over USB in setup() and create a flag variable to wait for serial connection to be lost.
  3. Use physical switches in front of motors or whatever so they don't draw amps while I'm plugged into computer (but I'd have to remember to use the switch).

Or is there some standard way of doing this? I really don't want to put my laptop's usb bus at risk for drawing too much power, nor do I want the arduino board to even try to draw power over the usb for anything other than sketch uploads.

The Uno can only draw 500mA through its poly-fuse which is also typically how much your PC can deliver on the USB bus. You should not have motors and other high current devices powered directly from the Arduino so I'm not exactly sure what your concern is.

Probably one of the easiest things to do would be a delay in your setup() function and blink the onboard LED to indicate you are waiting. After the wait period expires, leave the LED on to indicate "active"

I have a nano on an expansion prototype shield with a battery on the Vin expansion header. It seems like all the V in the GVS pin headers have continuity with that header, but if there's no battery they draw from the board.

EDIT: I rechecked this and maybe I'm wrong, the Vin doesn't seem to have continuity with the V headers...well luckily this is just a small SG90 servo...

Maybe the above is incorrect wiring (still kind of a newb...how else can I have the battery power the arduino and the motor?), but either way, the point of the question is to protect myself from, well, myself :slight_smile:

I think your idea of adding a nice delay and warning flash to the setup() is a pragmatic solution.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Why don't you put a start button into you hardware and code?
This will then start your code when you are ready.
Put it as a while loop as the last function of the setup().

That way you will avoid HOT SWAP problems.

How many amps are we talking about, why don't you have a power switch on your batteries?

Are your motors using the same 5V as the USB port?

Tom... :slight_smile:

Would the reset button serve your need? Just press and hold down -- that kills the sketch. Or a jumper from the reset pin to GND (or would it be VCC?).

Thanks to all who replied, each informative.

After realizing my mistaken assumption about where my SG90 servo was drawing power, I rewired it. Granted, from what I understand it would be OK to just power it through the nano itself (and my laptop), I'd rather not. Now the servo has its own direct power from the battery and common ground with the arduino. So the laptop just powers the nano and the IR sensor, no biggie.

Sorry that the post skewed off topic from the usb issue to the circuit, but you all gave me food for thought!

You can slice open the outer insulation on the USB cable and cut the power line. That will allow you to upload to the board without powering it from the USB. Of course, you will need to have a separate power supply connected to power the board while you're doing that.