Servos Twitching Violently

Hello all,

Me and my friend are attempting to build a robotic arm. We have 4 main servos which when attached to the power source (6v-7.5 amp) twitch violently back and forth. We've messed with the code, and even tried attaching it to arduino power itself but they dont seem to work correctly. Code:

#include <Servo.h>

Servo myservo;
Servo myservo2;
Servo myservo3;
Servo myservo4;

int potpin = 1;
int potpin2 = 2;
int potpin3 = 3;
int potpin4 = 4;

int val = 0;
int val2 = 0;
int val3 = 0;
int val4 = 0;

void setup ()

{
myservo.attach(52);
myservo2.attach(47);
myservo3.attach(42);
myservo4.attach(37);

}

void loop()

{

val = analogRead(potpin);
val = map(val, 0, 1023, 0, 179);
myservo.write(val);
delay(5);

val2 = analogRead(potpin2);
val2 = map(val2, 0, 1023, 0, 179);
myservo2.write(val2);
delay(5);

val3 = analogRead(potpin3);
val3 = map(val3, 0, 1023, 0, 179);
myservo3.write(val3);
delay(5);

val4 = analogRead(potpin4);
val4 = map(val4, 0, 1023, 0, 179);
myservo4.write(val4);
delay(5);

}

Hopefully someone out there could help us out with debugging this thing, need answers asap since we're hard pressed for time.

It might be noise on the analog pins, try adding 10nF to 100nF from each analog
pin to ground - that should bypass all the noise but still allow fast enough response
to turning the pots.

Check the Arduino supply is stable 5V.

Make sure the wiring (supply and ground) to the pots is not shared with
any high-current wiring - for instance if you run a ground wire to both the
pots and the servo supply ground this could be superimposing IR drops on
the pot signals.

Good practice is to use entirely separate ground and supply wires between the
Arduino and sensitive analog sensors to the ground and supply wires used to
power the Arduino itself and any high current devices.

You can also use a test sketch that sets each servo to a constant value to see
if the twitching is due to the servo or due to the analog readings (narrow down
where the issue is).

Thanks for the reply, im pretty sure the problem would be that we had the pots hooked up to the major power supply (6v-7.5 amps) I will let my friend know about the problem and have him test the theory.

MarkT:
It might be noise on the analog pins, try adding 10nF to 100nF from each analog
pin to ground - that should bypass all the noise but still allow fast enough response
to turning the pots.

So, what do you mean by adding 10nF to 100nF sorry kind of new to this stuff.

Add the capacitor between the analog pin and ground. One lead of the capacitor to the pin,
one lead of the capacitor to ground (GND).

Inadequate grounding between the servos/power supply and the arduino may result in the servo erratic behavior.

Have you gotten the "Knob" example sketch to work with a single servo?

Thanks mark for clarifying that, never actually had to use capacitors before :stuck_out_tongue:

zoomkat We have been able to make servos move without sparatic movement and one servo can run just fine (from arduino power) im pretty sure the issue is the main power supply running the through the potentiometers. Also common ground is where all of the potentiometers, etc. Would run off of right?

im pretty sure the issue is the main power supply running the through the potentiometers.

Probably so. The control pot + is normally connected to the arduino +5v, the pot ground connected to the arduino ground, and the pot wiper to the arduino analog input pin.

Also common ground is where all of the potentiometers, etc. Would run off of right?

A common ground is between the arduino ground, the external power supply ground, and the servo grounds.

Below is some servo/pot test code from awhile back with an arm setup in mind. The code is setup so the servo command value is displayed in the serial monitor for debugging.

//zoomkat multi pot/servo test 3-23-13
//includes dead band for testing and limit servo hunting
//view output using the serial monitor

#include <Servo.h> 
Servo myservo1;  //declare servos
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

int potpin1 = 0;  //analog input pin A0
int potpin2 = 1;
int potpin3 = 2;
int potpin4 = 3;
int potpin5 = 4;

int newval1, oldval1;  //pot input values
int newval2, oldval2;
int newval3, oldval3;
int newval4, oldval4;
int newval5, oldval5;

void setup() 
{
  Serial.begin(9600);  
  myservo1.attach(2);  
  myservo2.attach(3);
  myservo3.attach(4);
  myservo4.attach(5);
  myservo5.attach(6);
  Serial.println("testing multi pot servo");  
}

void loop()
{ 
  newval1 = analogRead(potpin1);           
  newval1 = map(newval1, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){ //dead band 
    myservo1.write(newval1); //position the servo
    Serial.print("1- ");
    Serial.println(newval1); //print the new value for testing 
    oldval1=newval1; //set the current old value
  }

  newval2 = analogRead(potpin2);
  newval2 = map(newval2, 0, 1023, 0, 179);
  if (newval2 < (oldval2-2) || newval2 > (oldval2+2)){  
    myservo2.write(newval2);
    Serial.print("2- ");    
    Serial.println(newval2);
    oldval2=newval2;
  }

  newval3 = analogRead(potpin3);           
  newval3 = map(newval3, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval3 > (oldval3+2)){  
    myservo1.write(newval3);
    Serial.print("3- ");
    Serial.println(newval3);
    oldval3=newval3;
  }

  newval4 = analogRead(potpin4);           
  newval4 = map(newval4, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval4 > (oldval4+2)){  
    myservo1.write(newval4);
    Serial.print("4- ");
    Serial.println(newval4);
    oldval4=newval4;
  }

  newval5 = analogRead(potpin5);           
  newval5 = map(newval5, 0, 1023, 0, 179); 
  if (newval1 < (oldval5-2) || newval5 > (oldval5+2)){  
    myservo1.write(newval5);
    Serial.print("5- ");
    Serial.println(newval5);
    oldval5=newval5;
  } 
  delay(50);  //to slow loop for testing
}

Thanks really appreciate the help ill have to try the debugging code tomorrow! :smiley: