Servo working while connected to computer, but not to external power

Hi Everyone!

I'm making a simple Obstacle Avoiding Car using an Arduino Uno and a DEVMO L293D Motor Drive Shield. My servo motor on the front for the Ultrasonic Sensor to look around is the MG996R Servo. I have two 18650 batteries in series for external power.

My servo motor works fine when connected to my Macbook. When connected to external power, it barely moves. Can anyone tell me why? My code is below (I am currently just using it to test the Servo).

Thank you so much!

#include <AFMotor.h>
#include <Servo.h>

const int pingPin = 10;
Servo myservo;
int MAX_SPEED = 90;
int turnSpeed = 60;
char direction = "";
int speedSet = 0;
int distance = 0;
bool atSpeed = false;
int turnDelay = 700;

AF_DCMotor motor1(1, MOTOR12_1KHZ); //BACK LEFT
AF_DCMotor motor2(2, MOTOR12_1KHZ); // BACK RIGHT
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);

void setup() {
  // put your setup code here, to run once:
  myservo.attach(9);  
  myservo.write(90);
  motorStop();
  delay(3000);
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(10);
  // distance = returnDistance();
  // run();
  lookAround();
  delay(5000);
}

void run(){
  //If returnDistance is greater than 900, move forward
  if(distance > 1000){
    motorForward();
  }
  //If you are close to something, stop, move backwards, stop, look around, move in direction of more space, then keep moving forward
  else{
    motorStop(); 
    delay(500);
    motorBackward();
    delay(500);
    motorStop();
    direction = lookAround();
    delay(200);
    if(direction == 'l'){
      motorLeft();
      delay(turnDelay);
      motorStop();
      delay(500);
    }
    else{
      motorRight();
      delay(turnDelay);
      motorStop();
      delay(500);
    }

  }
}

int returnDistance(){
  long duration;
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  return duration; 
}

char lookAround(){
  int l, r = 0; 
  delay(100); 
  myservo.write(160);
  delay(1000);
  l = returnDistance(); 
  delay(200); 
  myservo.write(20);
  delay(1000);
  r = returnDistance();
  delay(200);
  myservo.write(90);
  delay(1000);
  if(l > r){
    return 'l';
  }
  else{
    return 'r';
  }
}

void motorStop() {
  motor1.run(RELEASE); 
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
  atSpeed = false;
}

void motorForward(){
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
  if(atSpeed == false){
  for (speedSet = 0; speedSet < MAX_SPEED; speedSet++) {
    int speed = map(speedSet, 0, MAX_SPEED, 0, 255);
    motor1.setSpeed(speed);
    motor2.setSpeed(speed);
    motor3.setSpeed(speed);
    motor4.setSpeed(speed);
    delay(10);
  }
  atSpeed = true;
  }
}
void motorBackward(){
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
  if(atSpeed == false){
  for (speedSet = 0; speedSet < MAX_SPEED; speedSet++) {
    int speed = map(speedSet, 0, MAX_SPEED, 0, 255);
    motor1.setSpeed(speed);
    motor2.setSpeed(speed);
    motor3.setSpeed(speed);
    motor4.setSpeed(speed);
    delay(10);
  }
  atSpeed=true;
  }
}

void motorRight(){
  motor1.run(FORWARD);
  motor2.run(BACKWARD);
  motor3.run(BACKWARD);
  motor4.run(FORWARD);
  if(atSpeed == false){
  for (speedSet = 0; speedSet < turnSpeed; speedSet++) {
    int speed = map(speedSet, 0, turnSpeed, 0, 255);
    motor1.setSpeed(speed);
    motor2.setSpeed(speed);
    motor3.setSpeed(speed);
    motor4.setSpeed(speed);
    delay(10);
  }
  atSpeed = true;
}
}

void motorLeft(){
  motor1.run(BACKWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(BACKWARD);     
   if(atSpeed == false){
  for (speedSet = 0; speedSet < turnSpeed; speedSet++) {
    int speed = map(speedSet, 0, turnSpeed, 0, 255);
    motor1.setSpeed(speed);
    motor2.setSpeed(speed);
    motor3.setSpeed(speed);
    motor4.setSpeed(speed);
    delay(10);
  }
  atSpeed = true;
}
}

Do you have the required common ground? Please post a wiring diagram.

Example for one servo:

It's exactly this diagram, but instead of a 9V battery I have two 18650 batteries in Series, plus the MG996R Servo. I tried a lower power Servo on the same pins with external power, and got the same slow, twitchy motion. But works fine connected to my computer. I also tried connecting the servo to the other 5V, GND and A0 pins, but got the same result while on external. Seems like a power issue getting from the Motor Driver to the Arduino? I'm not sure.

Voltage drop thru voltage reg on board is causing it to overheat. Use a hard 5v for the CPU and the issue should clear.

That shield powers the servo through the 5volt regulator of the Arduino (bad design).
That regulator can safely provide about 300mA when powered from the 9volt of the shield.
Your servo draws about 2Amps (2000mA) at 5volt when it starts to move (stall current).
Overloading your laptop seems to work (fingers crossed).
A tiny SG-90 servo might work (for a while) on that setup, but not a bigger MG996R.
Leo..

You can use a 4xAA alkaline, or 5xNiMH battery pack to power one MG996R directly. The start/stall current is 2.5 Amperes, which the servo briefly draws, every time it starts moving.

I used 4xAA in Series to power the servo. Connected the Servo to the Signal, Ground and + pin from secondary external power. Turned on the car and got white smoke from the Motor Shield.... Not sure what I did wrong.. But seems this one is toast

What is "secondary external power"?

Please post a wiring diagram of the circuit that led to the smoke.