Nano V3 + servos, acting strange when fed only from Vin

Hello everyone,
I'm trying to make a basic "useless machine", using a Nano V3 clone and 2 "MG996" servos.

Here is the schematic:

And the sketch:

#define LEDPIN 13
#define BUTTONPIN 2
#define TRANSPIN 6
#include <Servo.h>

Servo doorServo; 
#define doorServoPin 3
#define doorServoUp 110     // lid open
#define doorServoDn 160      // lid closed

Servo gripServo; 
#define gripServoPin 4
#define gripServoUp 45      // push on button
#define gripServoDn 185     // on back position

void servoStatus(boolean newStat) {
  if (newStat) { digitalWrite(TRANSPIN, HIGH); delay(50); 
                 doorServo.attach(doorServoPin); 
                 gripServo.attach(gripServoPin); }
          else { delay(50); doorServo.detach();             
                 gripServo.detach();  
                 digitalWrite(TRANSPIN, LOW); delay(50); }
}

void doorSweepTo(byte targetPos) { doorServo.write(targetPos); delay(500); }

void gripSweepTo(byte targetPos) { gripServo.write(targetPos); delay(500);}

void closeButton() {
  delay(500); 
  servoStatus(HIGH); 
  doorSweepTo(doorServoUp); gripSweepTo(gripServoUp); 
  gripSweepTo(gripServoDn); doorSweepTo(doorServoDn); 
  servoStatus(LOW);
}

void setup() {
  pinMode(LEDPIN, OUTPUT); pinMode(BUTTONPIN, INPUT); pinMode(TRANSPIN, OUTPUT);
}

void loop() {
if (digitalRead(BUTTONPIN)) { closeButton(); }
}

It works perfect only while it is connected to PC via USB.
When disconnected and left on batteries, the servos are moving erratic - generally they're following the course, but with lots of random moves.

On batteries, Vin = ~7.2 V, and the 5V and 3V pins have the correct values (measured while the servos are moving).

Any ideas where am I wrong ?

Regards
Ciprian

Hi,

First you need a 1K resistor in the base lead of the BD139.

Place a 0.1uF cap between 5V and gnd on the Nano.

What value is R1?

I am not sure you need to attach and detach all the time, try attach in setup, and comment out all detach lines.

Switching power to the servos might be better if you switch positive not negative supply.
When you switch negative off, you still have a current flow from gnd in the arduino output to the servo positive supply.

A delay between turning the power on and then sending servo angle might help.

Tom... :slight_smile:

You forgot a base resistor for the BD139.

If these are really servo motors, it's not a good idea to turn their supply voltage on and off. Try without attach and detach and power switching first, until your code does what you want.

Or the battery can't supply the startup current the servo motors need and Vin drops below the threshold of the arduino internal regulator.