# Motors controlling Arduino usb question

simulate this circuit – Schematic created using CircuitLab

I made a RC car with an Arduino board, using an H-bridge to operate the motors. I connect them through a capacitor and a diode to the +5 V pin of the Arduino board to avoid damages on the board. I wrote a simple program. I am using an ultrasonic sensor to avoid objects. The idea is when I don't have an object in front of me, then a command to move forward must be turned on. When there is an object, the motors must go in reverse, move for a while, then go forward and turn right. I use 2 DC motors, one for reverse and forward drive, and one for right and left (steering).

The problem is this: if I connect the board to the computer through the USB cable, and at the same time I turn on the car then the code is executed normally, but if I remove the USB cable and I turn on the car (using the ~ 5.4 V from BAT1 through D1) then the car doesn't move forward, it goes back, stays, goes forward, and turns right - this is repeated twice and after that the program starts to function normally.

Here is the code :

int forward = 11;
int reverse = 10;
int left = 9;
int right = 8;

#define ECHOPIN 3
#define TRIGPIN 2

void setup() {

Serial.begin(9600);

pinMode(forward, OUTPUT);
pinMode(reverse, OUTPUT);
pinMode(left, OUTPUT);
pinMode(right, OUTPUT);

pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);

}

void loop() {

digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW);
// Compute distance
float distance = pulseIn(ECHOPIN, HIGH);
distance= distance/58;
Serial.println(distance);

delay(200);

if ( distance < 12.50) {

analogWrite(forward,0);
delay(1000);

analogWrite(reverse,100);
delay(1000);

analogWrite(reverse,0);
delay(1000);

analogWrite(forward,100);
digitalWrite(right,HIGH);
delay(1500);
digitalWrite(right,LOW);
delay(500);

}

if ( distance > 12.50) {
analogWrite(forward,100);
delay(1000);
}

}

• I'm curious about the downvote... It would be nice to have the decency to stand up for your downvotes with a comment either explaining why, or better yet suggesting to new members what they should do differently. – Anindo Ghosh Mar 10 '13 at 12:54
• Not enough information is provided! – Leon Heller Mar 10 '13 at 13:15
• With the voltage already low to start with, the diode is probably not helping things. You may need to omit and and use some other means to prevent reversed polarity power connection. But that may not be enough anyway - your battery is likely only 6v when new for example. You may be able to modify the arduino to run at a lower voltage if you reduce the clock frequency. – Chris Stratton Mar 10 '13 at 19:00
• Hey , i almost 99 % sure , that the answer is somwhere hidden in the code , because there is no sense to work when i am connected with the usb to my pc , and not work when i am only on batteries . I Now i make some changes to the code and i think that i am in a process to solve the mystery , when i obtein some results i will post them here . – Димитър Цветков Mar 11 '13 at 10:03
• I have changed the sensor with this model(sparkfun.com/datasheets/Components/GP2Y0A21YK.pdf) and everything goes good now .The program is changed also. This is my resolution for now . – Димитър Цветков Mar 11 '13 at 17:06

From the very sketchy description of the problem as provided, one can only speculate that possibly the Arduino board or the motor drivers are not receiving sufficient power in the battery powered mode, to function normally.

The older 5 Volt Arduino boards are designed to operate with either a 5 Volt regulated supply on +5V pin, 5 Volts through the USB, or a DC voltage of between 7 and 12 Volts (or thereabouts) from an external supply. While most Arduinos will operate down to 6 Volts on the external supply, ~5.4 Volts leaves too little headroom for the on-board Low Drop Out regulator to output a stable 5 Volt.

Also, on turning on any of the motors, it is almost definite that the supply voltage will drop even further at least briefly, thus causing unpredictable behavior from the Arduino, including possible board resets.

If you must use the 5.4 Volt battery, better results may be obtained by using a buck-boost regulator (DC-DC step-up+step-down regulator, not a linear regulator) that is capable of producing a regulated 5 Volts from an input voltage all the way down to 4 Volts or so, and up to 6 or more Volts. That way, the Arduino is assured of a stable 5 Volt supply despite battery supply variations over a wide range.

For a more problem-specific resolution, it helps to provide a schematic and more detailed information, including perhaps relevant code.

• Ok thanks for your reply, i edited my post to obtein better information. – Димитър Цветков Mar 10 '13 at 14:37

I have changed the sensor with this model(sparkfun.com/datasheets/Components/GP2Y0A21YK.pdf) and everything goes good now .The program is changed also. This is my resolution for now .

the new code is this :

     void loop() {

if ( sensorValue >550) {

analogWrite(forward,0);
analogWrite(reverse,70);
delay(1000);

}

if ( sensorValue < 550) {
analogWrite(reverse,0);
analogWrite(forward,100);
delay(1000);
}


}