mg995 servo x 2 behaves badly!!

I have my clg project on dual axis solar tracking
i have attach two mg995 servo (metal gear) as per schematic
but it behaves badly sometimes and sometimes it works perfectly (rarely)
what will be the reason ??
is my code having a bad execution or any hardware issue ?
Please help

My code :

#include <Servo.h>

// 180 horizontal MAX
Servo horizontal; // horizontal servo

int servoh = 180; // 90; // stand horizontal servo

int servohLimitHigh = 180;
int servohLimitLow = 0;

// 65 degrees MAX
Servo vertical; // vertical servo

int servov = 45; // 90; // stand vertical servo

int servovLimitHigh = 80;
int servovLimitLow = 15;

// LDR pin connections
// name = analogpin;

int ldrlt = 0; //LDR top left - BOTTOM LEFT <--- BDG

int ldrrt = 1; //LDR top rigt - BOTTOM RIGHT

int ldrld = 2; //LDR down left - TOP LEFT

int ldrrd = 3; //ldr down rigt - TOP RIGHT

void setup()
{ Serial.begin(9600);

// servo connections

// name.attacht(pin);

horizontal.attach(9);

vertical.attach(10);

horizontal.write(180);

vertical.write(45);

delay(3000);

}

void loop() 
{ int lt = analogRead(ldrlt); // top left

int rt = analogRead(ldrrt); // top right

int ld = analogRead(ldrld); // down left

int rd = analogRead(ldrrd); // down right

// int dtime = analogRead(4)/20; // read potentiometers

// int tol = analogRead(5)/4;

int dtime = 10; int tol = 50;

int avt = (lt + rt) / 2; // average value top

int avd = (ld + rd) / 2; // average value down

int avl = (lt + ld) / 2; // average value left

int avr = (rt + rd) / 2; // average value right

int dvert = avt - avd; // check the diffirence of up and down

int dhoriz = avl - avr;// check the diffirence og left and rigt

/*Serial.print("Bottom Left");

Serial.print(" ");
Serial.print(lt);
Serial.print(" ");

Serial.print("Bottom Right ");

Serial.print(" ");
Serial.print(rt);
Serial.print(" ");

Serial.print("Top Left");

Serial.print(" ");
Serial.print(ld);
Serial.print(" ");
Serial.print("Top Right ");

Serial.print(" ");
Serial.print(rd);
Serial.print(" ");

Serial.println();
*/

if (-1*tol > dvert || dvert > tol) // check if the diffirence is in the tolerance else change vertical angle

{

if (avt > avd)

{

servov = ++servov;

if (servov > servovLimitHigh)

{

servov = servovLimitHigh;

}

}

else if (avt < avd)

{

servov= --servov;

if (servov < servovLimitLow)

{

servov = servovLimitLow;

}

}

vertical.write(servov);

}

if (-1*tol > dhoriz || dhoriz > tol) // check if the diffirence is in the tolerance else change horizontal angle

{

if (avl > avr)

{

servoh = --servoh;

if (servoh < servohLimitLow)

{

servoh = servohLimitLow;

}

}

else if (avl < avr)

{

servoh = ++servoh;

if (servoh > servohLimitHigh)

{

servoh = servohLimitHigh;

}

}

else if (avl = avr)

{

// nothing

}

horizontal.write(servoh);

}

delay(dtime);

}

.................................................................
Schematic :
..................................................................
About mg995 :

Dimension: 40.7×19.7×42.9mm

Operating voltage range: 4.8 V to 7.2 V

Stall torque: 9.4kg/cm (4.8v); 11kg/cm (6v)

Operating speed: 0.2 s/60º (4.8 V), 0.16 s/60º (6 V)

Rotational degree: 180º

Dead band width: 5 μs

Operating temperature range: 0ºC to +55ºC

Current draw at idle: 10mA

No load operating current draw: 170mA

Current at maximum load: 1200mA

...................................................
Sorry But i dont have knowledge about voltage and power supply
But i read something like servo require external power supply
{i tried with mobile charger ( 5.0 v and 1 A)}

please suggest me if any changes required in code or in any hardware part

Thank You!!!!!!!!!!!!!!!!!!

Note : SG90 non-metal gear servos also behaves badly sometime with same schematic

Almost all servo issues reported here are power related - yours is no different. Your servos require 4.8 to 7.2 volts and each of them can draw up to 1.2 Amps. The power supply you tried isn't really sufficient for one of them. Ideally, you need something that's six volts and can supply three amps or more.

Please give some details of "behaves badly". What exactly do the servos do that isn't what you wanted and when?

I don't know if this is the only problem but it certainly sounds as though you have too little power. MG995s work best at 6V and need at least 1.5A each, preferably more. Try a separate 6V 3A or 4A power supply and see if that improves things.

Steve

wildbill:
Almost all servo issues reported here are power related - yours is no different. Your servos require 4.8 to 7.2 volts and each of them can draw up to 1.2 Amps. The power supply you tried isn't really sufficient for one of them. Ideally, you need something that's six volts and can supply three amps or more.

u mean each single servo requires 6 v and 1.5 A current
so i have to connect each servo to each battery seperate
right ?

slipstick:
Please give some details of "behaves badly". What exactly do the servos do that isn't what you wanted and when?

I don't know if this is the only problem but it certainly sounds as though you have too little power. MG995s work best at 6V and need at least 1.5A each, preferably more. Try a separate 6V 3A or 4A power supply and see if that improves things.

Steve

behaves badly means

  1. sometimes it produces long beep like noise
    2.sometime it rotate too slow
    3.sometime it fluctuate its rotator
    4.sometime it vibrate from inside but shaft doesnt rotate

i want to know that ,from my code ,which type of pwm is i am using (soft/hardware)
please let me know !!!

so i have to connect each servo to each battery seperate

No. One power supply for both servos, 6V, 3A minimum. Don't forget to connect all the grounds.

Simple servo test code you might use to troubleshoot your servo issues using the serial monitor.

// zoomkat 7-30-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.attach(9);
}

void loop() {

  while (Serial.available()) {

    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
      delay(3);
    } 
  }

  if (readString.length() >0) {
    Serial.println(readString);
    int n = readString.toInt();
    Serial.println(n);
    myservo.writeMicroseconds(n);
    //myservo.write(n);
    readString="";
  } 
}

zoomkat:
Simple servo test code you might use to troubleshoot your servo issues using the serial monitor.

// zoomkat 7-30-10 serial servo test

// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// Powering a servo from the arduino usually DOES NOT WORK.

String readString;
#include <Servo.h>
Servo myservo;  // create servo object to control a servo

void setup() {
  Serial.begin(9600);
  myservo.attach(9);
}

void loop() {

while (Serial.available()) {

if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
      delay(3);
    }
  }

if (readString.length() >0) {
    Serial.println(readString);
    int n = readString.toInt();
    Serial.println(n);
    myservo.writeMicroseconds(n);
    //myservo.write(n);
    readString="";
  }
}

Thank You!!!

jremington:
No. One power supply for both servos, 6V, 3A minimum.

Than You !!!!

'' Don't forget to connect all the grounds.''

yes and also with Uno's ground !!!!!!!

i just google for 6v 3 amp battery but it showing

battery with 3.3 AH !

means it will supply 3.3 A current for 1 hr only
if current in Amp reduces then it will last for more time
i am newbee in electricity , somewhere i read this info about volts amps and AH
Hoping my answer is correct !!!

shivamgokarankar:
Than You !!!!

i just google for 6v 3 amp battery but it showing

battery with 3.3 AH !

means it will supply 3.3 A current for 1 hr only

Maybe. Knowing the Amp hours of the battery is useful, but you also need to know the maximum current it can provide to decide whether it's adequate for your needs.

The power supply must be able to supply the servo stall current, but the servo running current is usually much less.

If you stall a servo for long, it will be likely be destroyed.

Please check the attackment
I have this battery
does this creates any problem to driving those 2 servos??

That close-up picture of a label tells us nothing about what type of battery it is or what current it can provide. A datasheet would be more use but even a picture of the whole battery might give some idea.

I'm guessing it's some sort of lead-acid battery but there loads of different types with different characteristics.

Steve

Extremely Sorry but motor which i have only have this info on that

And i tried this by connecting to my circuit
motor are working fine

Thank You to all who helped me !!!!