Jerking servo motor

I’m trying to control servo with joystick everything works fine except servo. When I move joystick the servo works but at ideal condition when i dont move joystick then servo randomly start jettring or jerking please watch this 11second clip and look at servo Please watch this closely

Here’s mine code -

#include <Servo.h>
 
//Joystick controlled robot
Servo myServo1; // eyeblink right
Servo myServo2; // eyeblink left
Servo myServo3; // eye look right 
Servo myServo4; // eye look left
Servo myServo5; // eye look up down
int servo1 = 3;  
int servo2 = 5;  
int servo3 = 6; 
int servo4 = 9;  
int servo5 = 11;
int joyX = 0;   
int joyY = 1;    
int button1 = 2; 
int press1 = 90;

void setup(){
  myServo1.attach(servo1);
  myServo2.attach(servo2);
  myServo3.attach(servo3);
  myServo4.attach(servo4);
  myServo5.attach(servo5);
  pinMode(button1, INPUT);
  digitalWrite(2, HIGH); 
}

void loop(){
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    myServo1.write(180);
    myServo2.write(180);
    myServo3.write(180);
  }
  else {
    myServo1.write(0);
    myServo2.write(0);
    myServo3.write(0);
  }
  int valX = analogRead(joyX); 
  int valY = analogRead(joyY); 
 
  valX = map(valX, 0, 1023, 0, 180);
  valY = map(valY, 0, 1023, 0, 180); 
   
  myServo2.write(valX);
  myServo1.write(valX);
  myServo3.write(valY);   
  myServo4.write(valX);
  myServo5.write(valY);
 
  delay(15);
}

Look at image I attached for circuit diagram

What you need to do is build in a 'DEAD SPOT' on the joystick.

int JoySlack = 25; // Dead spot size
int center = 512; // Reading when joystick centered
if (abs((analogRead(A0)) - center) > JoySlack)
{
    Do stuff here
}

First you could put some Serial.printlns in to see if the problem is the joystick value jittering about when it is centered. It quite likely is in which case you already have the solution.

Otherwise we'll need to look further. Increasing that delay(15) might help.

Steve

missdrew:
What you need to do is build in a ‘DEAD SPOT’ on the joystick.

int JoySlack = 25; // Dead spot size

int center = 512; // Reading when joystick centered
if (abs((analogRead(A0)) - center) > JoySlack)
{
   Do stuff here
}

#include <Servo.h>
 
//Joystick controlled robot
Servo myServo1; // eyeblink right
Servo myServo2; // eyeblink left
Servo myServo3; // eye look right 
Servo myServo4; // eye look left
Servo myServo5; // eye look up down
int servo1 = 3;  
int servo2 = 5;  
int servo3 = 6; 
int servo4 = 9;  
int servo5 = 11;
int joyX = 0;   
int joyY = 1;    
int button1 = 2; 
int press1 = 90;
int JoySlack = 50; // Dead spot size
int center = 519; // Reading when joystick centered

void setup(){
  myServo1.attach(servo1);
  myServo2.attach(servo2);
  myServo3.attach(servo3);
  myServo4.attach(servo4);
  myServo5.attach(servo5);
  pinMode(button1, INPUT);
  digitalWrite(2, HIGH); 
}

void loop(){
  press1 = digitalRead(button1);
  if (abs((analogRead(A0)) - center) > JoySlack)
  if (press1 == LOW)
  {
    myServo1.write(0);
    myServo2.write(0);
    myServo3.write(0);
  }
  else {
    myServo1.write(180);
    myServo2.write(180);
    myServo3.write(180);
  }
  int valX = analogRead(joyX); 
  int valY = analogRead(joyY); 
 
  valX = map(valX, 0, 1023, 0, 180);
  valY = map(valY, 0, 1023, 20, 160); 
   
  myServo2.write(valX);
  myServo1.write(valX);
  myServo3.write(valY);   
  myServo4.write(valX);
  myServo5.write(valY);
 
  delay(200);
}

Is this correct code but still the servo jerking with little less noise…
Yes this problem solved but still i can hear micro jerking but thats not big one like previous

One more thing mine servo is moving tooooo…! fast when i move joystick is there anyway to smooth the servo rotation with linear movement of joystick???

Try

#include <Servo.h>

//Joystick controlled robot
Servo myServo1; // eyeblink right
Servo myServo2; // eyeblink left
Servo myServo3; // eye look right
Servo myServo4; // eye look left
Servo myServo5; // eye look up down
int servo1 = 3;
int servo2 = 5;
int servo3 = 6;
int servo4 = 9;
int servo5 = 11;
int joyX = 0;
int joyY = 1;
int button1 = 2;
int press1 = 90;
int JoySlack = 50; // Dead spot size
int center = 519; // Reading when joystick centered

void setup() {
  myServo1.attach(servo1);
  myServo2.attach(servo2);
  myServo3.attach(servo3);
  myServo4.attach(servo4);
  myServo5.attach(servo5);
  pinMode(button1, INPUT);
  digitalWrite(2, HIGH);
}

void loop() {
  press1 = digitalRead(button1);
    if (press1 == LOW)
    {
      myServo1.write(0);
      myServo2.write(0);
      myServo3.write(0);
    }
    else {
      myServo1.write(180);
      myServo2.write(180);
      myServo3.write(180);
    }
  int valX = analogRead(joyX);
  int valY = analogRead(joyY);
  if ((abs(valX - center) > JoySlack) || (abs(valY - center) > JoySlack))
  {
    valX = map(valX, 0, 1023, 0, 180);
    valY = map(valY, 0, 1023, 20, 160);

    myServo2.write(valX);
    myServo1.write(valX);
    myServo3.write(valY);
    myServo4.write(valX);
    myServo5.write(valY);
  }
  else
  {
    myServo2.write(90);
    myServo1.write(90);
    myServo3.write(90);
    myServo4.write(90);
    myServo5.write(90);
  }
  delay(200);
}

PROBLEM (1):- Still jerking problem happen :confused: :confused: I observed one thing, I measured voltage of buck convertor output and its constant 6.5volt then I measured voltage of arduino pwm and gnd and its also constant 0.37volts but when i connect all those things together including servo then when I measured voltage between servo's gnd and positive pin it always drops to approx 0.5volts at time of jerking-

For example when I measure voltage between brown and red wire of servo then its like 6.44volt but when servo jerks for approx 0.5second then at that instant time voltage drops to 6.15-6.02approx volts.. Jerking happen randomly no perticular time range..

Why this problem happen I mean I checked arduino and buck convertor and observed them for 1minute,no volt drop????????

I tried adding 25volt capacitor between servo gnd and +ve pin but no luck :(

PROBLEM (2):- The servo is still moving too fast when I move joystick - I want that When I move joystick at X(pos) then servo also rotate but with slower rate not like speed of jet:

Please note: This jerking happen to every servo that I connect and no matter how much between 5-7volt voltage I supplied there is no solution to this jerking & I even tested on both uno and mega there is no luck…

Did you ever put a serial print in to see if the joystick value is changing when the servo jitters?

One way to control the servo speed would be to change to VarSpeedServo.h instead of Servo.h. That has a speed parameter in the write() command.

Steve

therobobuilder: Please note: This jerking happen to every servo that I connect and no matter how much between 5-7volt voltage I supplied there is no solution to this jerking & I even tested on both uno and mega there is no luck..

Please what you mean by "jerking". Is it back and forth or is it in a single direction. Can you tell how often it jerks, in jerks per second, or ? Paul

Hii Thanks
What Kind of jerking??
Back and fourth at around 2-5degree when I’m not using joystick
Please watch this video to know better

#include <VarSpeedServo.h>

//Joystick controlled robot
VarSpeedServo myServo1; // eyeblink right
VarSpeedServo myServo2; // eyeblink left
VarSpeedServo myServo3; // eye look right
VarSpeedServo myServo4; // eye look left
VarSpeedServo myServo5; // eye look up down
int servo1 = 7;
int servo2 = 5;
int servo3 = 6;
int servo4 = 9;
int servo5 = 11;
int joyX = 0;
int joyY = 1;
int button1 = 2;
int press1 = 90;
int JoySlack = 25; // Dead spot size
int center = 519; // Reading when joystick centered

void setup() {
  Serial.begin(9600);
  myServo1.attach(servo1);
  myServo2.attach(servo2);
  myServo3.attach(servo3);
  myServo4.attach(servo4);
  myServo5.attach(servo5);
  pinMode(button1, INPUT);
  digitalWrite(2, HIGH);
}

void loop() {
  Serial.print("X-axis: ");
  Serial.print(analogRead(joyX));
  Serial.print(" | ");
  Serial.print("Y-axis: ");
  Serial.print(analogRead(joyY));
  Serial.println(" | ");
  delay(200);
  press1 = digitalRead(button1);
    if (press1 == LOW)
    {
      myServo1.write(0);
      myServo2.write(0);
      myServo3.write(0);
    }
    else {
      myServo1.write(180);
      myServo2.write(180);
      myServo3.write(180);
    }
  int valX = analogRead(joyX);
  int valY = analogRead(joyY);
  if ((abs(valX - center) > JoySlack) || (abs(valY - center) > JoySlack))
  {
    valX = map(valX, 50, 1023, 0, 180);
    valY = map(valY, 50, 1023, 0, 180);

    myServo2.write(valX);
    myServo1.write(valX);
    myServo3.write(valY);
    myServo4.write(valX);
    myServo5.write(valY);
  }
  else
  {
    myServo2.write(90);
    myServo1.write(90);
    myServo3.write(90);
    myServo4.write(90);
    myServo5.write(90);
  }
  delay(200);
}

WATCH THIS TO KNOW WHAT KIND OF JERK - JERKING PROBLEM

Hi IS THERE ANYONE WHO CAN LOOK AT THIS PROBLEM AND HELP ME OUT. DO I NEED TO GIVE MORE INFORMATION? AM I DOING SMETHING WRONG? IS THIS PROBLEM UNSOLVABLE?

NO NEED TO SHOUT. WE'RE TRYING TO HELP!

Most users of hobby servos don't need to worry about such minor twitching. You've tried most of the normal fixes for such problems. Though when you say you added a 25V capacitor, what was its value? It needs to be around 470-1000uF to be much use.

One interesting thing is the way the output of your DC-DC converter drops. I wonder if that converter is the CAUSE of the problem rather than an effect of it? Also 6.5V is greater than the specified voltage for most hobby servos.

Also your code seems to drive 5 servos, 4 of which don't exist. If you actually connect all 5 does it do the same thing? If you change the code so it doesn't try to write to non-existent servos is the problem still there?

If you try it using 4 x 1.2V NiMH AA batteries instead of the converter do you still get the same problem?

Steve

Hi,
Where is the button in your code?
How have you got it connected?

I cannot see it in your “circuit diagram” or in your video clip.
If it is not connected, then you have an open input pin that is neither HIGH nor LOW.
What is it for?

What is this bit of code supposed to do?

  press1 = digitalRead(button1);
    if (press1 == LOW)
    {
      myServo1.write(0);
      myServo2.write(0);
      myServo3.write(0);
    }
    else {
      myServo1.write(180);
      myServo2.write(180);
      myServo3.write(180);
    }

Comment that bit of code out of your sketch and see what happens.
You are making the servos go to 0 or 180 while trying to control them with your joystick.

That is probably where the jitter/twitching is coming from.
In fact I have commented it out for you, try this code;

#include <VarSpeedServo.h>


//Joystick controlled robot
VarSpeedServo myServo1; // eyeblink right
VarSpeedServo myServo2; // eyeblink left
VarSpeedServo myServo3; // eye look right
VarSpeedServo myServo4; // eye look left
VarSpeedServo myServo5; // eye look up down
int servo1 = 7;
int servo2 = 5;
int servo3 = 6;
int servo4 = 9;
int servo5 = 11;
int joyX = 0;
int joyY = 1;
int button1Pin = 2;
int press1 = 90;
int JoySlack = 25; // Dead spot size
int center = 519; // Reading when joystick centered


void setup() {
  Serial.begin(9600);
  myServo1.attach(servo1);
  myServo2.attach(servo2);
  myServo3.attach(servo3);
  myServo4.attach(servo4);
  myServo5.attach(servo5);
  pinMode(button1Pin, INPUT);
  digitalWrite(2, HIGH);
}


void loop() {
  Serial.print("X-axis: ");
  Serial.print(analogRead(joyX));
  Serial.print(" | ");
  Serial.print("Y-axis: ");
  Serial.print(analogRead(joyY));
  Serial.println(" | ");
  delay(200);
/*  press1 = digitalRead(button1Pin);
    if (press1 == LOW)
    {
      myServo1.write(0);
      myServo2.write(0);
      myServo3.write(0);
    }
    else {
      myServo1.write(180);
      myServo2.write(180);
      myServo3.write(180);
    }
    */
  int valX = analogRead(joyX);
  int valY = analogRead(joyY);
  if ((abs(valX - center) > JoySlack) || (abs(valY - center) > JoySlack))
  {
    valX = map(valX, 50, 1023, 0, 180);
    valY = map(valY, 50, 1023, 0, 180);


    myServo2.write(valX);
    myServo1.write(valX);
    myServo3.write(valY);
    myServo4.write(valX);
    myServo5.write(valY);
  }
  else
  {
    myServo2.write(90);
    myServo1.write(90);
    myServo3.write(90);
    myServo4.write(90);
    myServo5.write(90);
  }
  delay(200);
}

Tom… :slight_smile:

This is the culprit I reckon, as spotted by TomGeorge:

 press1 = digitalRead(button1);
    if (press1 == LOW)
    {
      myServo1.write(0);
      myServo2.write(0);
      myServo3.write(0);
    }
    else {
      myServo1.write(180);
      myServo2.write(180);
      myServo3.write(180);
    }

You need to use the technique in StateChangeDetection example to ensure these writes only happen when the button state changes. Currently you're reprogramming the servos in quick succession every time through loop() and its pot luck whether 0/180 or the joystick derived value is sent on the next serov control pulse.

slipstick: Though when you say you added a 25V capacitor, what was its value? It needs to be around 470-1000uF to be much use.

Hi steve sir Yes I used 25v 1000uf capacitor. Yes sir MG958 servo uses 6.6v for stall torque of 20kg-cm that why I used 6.5volt and I tried with different dcdc convertor but got same result. Yes I tried with all 5 servo but got same problem So finelly I removed all those and make video for one servo to show you the problem. Yes tried with Lipo battery but same result jerking.

TomGeorge:
Hi,
Where is the button in your code?
How have you got it connected?

I cannot see it in your “circuit diagram” or in your video clip.
If it is not connected, then you have an open input pin that is neither HIGH nor LOW.
What is it for?

What is this bit of code supposed to do?

  press1 = digitalRead(button1);

if (press1 == LOW)
   {
     myServo1.write(0);
     myServo2.write(0);
     myServo3.write(0);
   }
   else {
     myServo1.write(180);
     myServo2.write(180);
     myServo3.write(180);
   }



Comment that bit of code out of your sketch and see what happens.
You are making the servos go to 0 or 180 while trying to control them with your joystick.

That is probably where the jitter/twitching is coming from.
In fact I have commented it out for you, try this code;


#include <VarSpeedServo.h>

//Joystick controlled robot
VarSpeedServo myServo1; // eyeblink right
VarSpeedServo myServo2; // eyeblink left
VarSpeedServo myServo3; // eye look right
VarSpeedServo myServo4; // eye look left
VarSpeedServo myServo5; // eye look up down
int servo1 = 7;
int servo2 = 5;
int servo3 = 6;
int servo4 = 9;
int servo5 = 11;
int joyX = 0;
int joyY = 1;
int button1Pin = 2;
int press1 = 90;
int JoySlack = 25; // Dead spot size
int center = 519; // Reading when joystick centered

void setup() {
 Serial.begin(9600);
 myServo1.attach(servo1);
 myServo2.attach(servo2);
 myServo3.attach(servo3);
 myServo4.attach(servo4);
 myServo5.attach(servo5);
 pinMode(button1Pin, INPUT);
 digitalWrite(2, HIGH);
}

void loop() {
 Serial.print("X-axis: “);
 Serial.print(analogRead(joyX));
 Serial.print(” | ");
 Serial.print("Y-axis: “);
 Serial.print(analogRead(joyY));
 Serial.println(” | ");
 delay(200);
/*  press1 = digitalRead(button1Pin);
   if (press1 == LOW)
   {
     myServo1.write(0);
     myServo2.write(0);
     myServo3.write(0);
   }
   else {
     myServo1.write(180);
     myServo2.write(180);
     myServo3.write(180);
   }
   */
 int valX = analogRead(joyX);
 int valY = analogRead(joyY);
 if ((abs(valX - center) > JoySlack) || (abs(valY - center) > JoySlack))
 {
   valX = map(valX, 50, 1023, 0, 180);
   valY = map(valY, 50, 1023, 0, 180);

myServo2.write(valX);
   myServo1.write(valX);
   myServo3.write(valY);
   myServo4.write(valX);
   myServo5.write(valY);
 }
 else
 {
   myServo2.write(90);
   myServo1.write(90);
   myServo3.write(90);
   myServo4.write(90);
   myServo5.write(90);
 }
 delay(200);
}




Tom... :)

Hi Mr. TomGeorge sir Thankyou verymuch :smiley: :smiley: :smiley: the code is working I see no Jerking/twitching any more thankyou 8)

The tackle button I used to control the eyelid of my robot.

AND yes I’m dumb as hell I supposed to digital write only last two servos 4 & 5 as eyelid control not starting three’s(1,2,3)
BTW thankyou

You should make online course on arduino programming and electronic control on udemy.