Self Balancing sketch runs only when usb is connected

I have made a self balancing robot using an MPU6050 DMP, Arduino Motor Shield and PID written by Kas. I also have tuned the PID so that the robot can balance itself but when I unplug the usb cable no voltage will be sent to the motors. I power the Arduino from the voltage input of the motor shield. Both the motor shield and arduino recieves the power but the motors dont start turning.

The program is in the attachments

SelfBalancing2014.ino (9.73 KB)

That sounds more like a wiring problem than a coding problem.

I presume you have checked that the Arduino is actually working when the USB is disconnected?

Is it possible that the USB link is providing a ground connection for something?

Draw a diagram of how everything is actually wired up and post a photo of the drawing. Note that a photo of the hardware is not nearly as good as diagram.


What are you using to power it? Please don't say a regular 9V battery. This battery pack here would be perfect. You could also go to your local hobby shop and find something similar.

Powering motors and Arduino separately is always a better arrangement - motor supplies tend to drop to low voltage when the motor starts for a few milliseconds, unacceptable for the Arduino Vin.

I am using a 12 V adapter to power the motor shield. I think I need to buy Li-po batteries so that enough current can be supplied to the motors. Arduino and the motor shield seem to be working when hooked up. The LED's on both of them light up.

Wow, the soldering on those motor connections is awful !

UKHeliBob: Wow, the soldering on those motor connections is awful !

Are they soldered at all?