my servo is jittering

i made a dual axis solar tracker in which i use 2 servo motors(mg90s by towerpro)
everything is working fine except the motors are jittering
here is my code plz solve it asap

CODE:

#include <Servo.h> // include Servo library 

// 180 horizontal MAX
Servo horizontal; // horizontal servo
int servoh = 135;     // stand horizontal servo

int servohLimitHigh = 180;

int servohLimitLow=95;

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

int servov = 45;        // stand vertical servo

int servovLimitHigh = 85;
int servovLimitLow = 15;


// LDR pin connections

int ldrlt = 1; //LDR top left - BOTTOM LEFT    
int ldrrt = 0; //LDR top rigt - BOTTOM RIGHT 
int ldrld = 3; //LDR down left - TOP LEFT
int ldrrd = 2; //ldr down rigt - TOP RIGHT

void setup()
{
 Serial.begin(9600);
// servo connections
// name.attacht(pin);
 horizontal.attach(9); 
 vertical.attach(10);
 horizontal.write(135);
 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 rigt
//  

 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(avt);
 Serial.print(" ");
 Serial.print(avd);
 Serial.print(" ");
 Serial.print(avl);
 Serial.print(" ");
 Serial.print(avr);
 Serial.print("   ");
 Serial.print(dtime);
 Serial.print("   ");
 Serial.print(tol);
 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-5;
   
  
    if (servov < servovLimitLow) 
    { 
     servov = servovLimitLow;
    }
 }
 else if (avt < avd)
 {
   servov= servov+5;
  
   if (servov > servovLimitHigh)
 {
   servov = servovLimitHigh;
 }
 }
 vertical.write(servov);
  Serial.print("servov======");
   
  Serial.print(servov);
   Serial.println("   ");
 }
//  
 if (-1*tol > dhoriz || dhoriz > tol) // check if the diffirence is in the tolerance else change horizontal angle
 {
 if (avl > avr)
 {
   servoh = servoh-5;
   if (servoh < servohLimitLow)
   {
   servoh = servohLimitLow;
   }
 }
 else if (avl < avr)
 {
   servoh = 5+servoh;
    if (servoh > servohLimitHigh)
    {
    servoh = servohLimitHigh;
    }
 }
 else if (avl = avr)
 {
///    // nothing
 }
 horizontal.write(servoh);
  Serial.print("servoh======");
  Serial.print(servoh);
   Serial.println("   ");
 }
  delay(100);

}

Jittering servos can be power related, so how are the servos powered.
Two MG-90S servos draw 1.5-2Amp stall.
Forget about the <=500mA 5volt pin of an Arduino.
Leo..

Use a separate power supply for the servos (4xAA is OK for two small ones) and connect the grounds.

Servos will jitter if they are driven to a point where noise on the A/D from the feedback resistor causes it to constantly hunt.
There is nothing you can do about it except maybe get a better servo.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

im powering arduino externally by 7.5 volt 2 ampere battery and using a sensor shield which gives 5v to motor

7.5V is too high and will destroy a Tower Pro MG90S servo. The sensor shield 5V output cannot be used to power the motor.

Use 4xAA batteries to power the servos, and connect the grounds.

Motors voltage range is 4.8v to 6.6v
Im giving 7.5v to arduino and arduino gives 5v to sensor shield

Carefully study replies #1, #3, #7, #8 and #10.