(SOLVED) Servos jittering

So, I've run into another problem with my SpotMicro robot dog. The mg996r servos I'm using are jittering wildly.
Here's my code:

#include <Servo.h>

int xAxisPin = 30;
int yAxisPin = 31;
int zAxisPin = 32;
int elevationPin = 33;
int speedPin = 34;
int modePin = 35;
 
int xAxisPwmValue = 0;
int yAxisPwmValue = 0;
int zAxisPwmValue = 0;
int elevationPwmValue = 0;
int speedPwmValue = 0;
int modePwmValue = 0;


int xAxisRotationValue = 0;
int yAxisRotationValue = 0;
int zAxisRotationValue = 0;
int elevationValue = 0;
int speedValue = 0;
int mode = 1;

int legAngle1 = 0;
int legAngle2 = 0;
int legAngle3 = 0;

Servo shoulderLB;
Servo kneeLB;

void setup() {
  // put your setup code here, to run once:
  pinMode(xAxisPin, INPUT);
  pinMode(yAxisPin, INPUT);
  pinMode(zAxisPin, INPUT);
  pinMode(elevationPin, INPUT);
  pinMode(speedPin, INPUT);
  pinMode(modePin, INPUT);
  shoulderLB.attach(2);
  kneeLB.attach(3);
}

void loop() {
  // put your main code here, to run repeatedly:
  elevationPwmValue = pulseIn(elevationPin, HIGH);

  legAngle1 = 0;
  legAngle2 = map(elevationValue,0,100,99.3,33.8);
  legAngle3 = map(elevationValue,0,100,23.1,117.7);
  shoulderLB.write(90+legAngle2);
  kneeLB.write(45+legAngle3);
  delay(10);
}

The setup is fairly simple. I'm using an Arduino Mega, with the PWM input from an RC receiver going into pin 33, and I have a 100uF capacitor across the servos power supply. I am only using two servos right now, but I will need to run up to 12.

The servos are on a separate 6v power supply, with its ground connected to the Arduino's ground. The servo data lines are connected directly to their respective pins, 2 and 3. The RC receiver is powered by the Arduino's 5v pin.

Connecting the two grounds helped somewhat, but there is still a lot of jitter.

1 Like

You are updating the servos at 100 Hz, perhaps a bit too frequently.

Have you examined the return value of your call to pukseIn()? Or the mapped values as fed to the servos? Imma bet you will see these values fluctuating, resulting in jitter which really isn’t jitter because the servos are only doing what they are being told to do…

Try printing those values to see and for now increase the loop delay to 333 or whatever so you can see what’s jitter and what’s just proper response.

HTH

a7

1 Like

Servo's on separate power supply is able to provide how much current?

Running the servo power through a breadboard?

1 Like

Servo jittering is usually due to an inadequate power supply.

For the MG996R, the supply needs to be able to provide 2.5 Amperes per servo.

1 Like

For both of you, the power supply is a set of four AA batteries.
I'm not sure how to measure the current capability.
And the dog will have 12 mg996r servos, I need 30 amps?
I am using a breadboard's power rails to connect all of the servos in parallel.

Four fresh AA batteries are good for one or maybe two MG996R servos.

You need a 20 to 30 A power supply for 12 of them.

How would I make the dog portable? Would a LiPo be sufficient?

Look for rechargeable RC battery packs, capable of 4.8-6 V and 20-30A discharge.

1 Like

I found a battery that is 7.4v, 100C, 1500mAh. Would this work?

Nevermind, I found a 6v one. Strangely enough, none of them have a current specification.

Are your servos rated for 7.4V?

1500 mAh capacity will power the robot for a few minutes.

1 Like

My servos are rated 6v

RC battery packs are rated by voltage, C rating and mAh (capacity).

100C, 1500 mAh (1.5 Ah) means that the battery can be discharged at a rate of 100*1.5 or 150 Amperes absolute maximum (for a second or two).

Battery lifetime in hours is very roughly given by Ah/(average current draw in Amperes)

Would 2400 mAh work better for what I want to do? About how long would it last?

Divide 2.4 Ah by the average current draw of your robot in Amperes.

Judging by the 20A current for 12 servos, I would get about 12 minutes. How could I raise my time even longer? (More batteries, bigger batteries, etc.) My arduino is powered by a separate battery, and it lasts for hours of continuous use anyways.

Breadboards are not able to supply the current required for 2 servo's, much less one.

What would happen if I used a breadboard for more than that?

And I mean just a power rail, not the breadboard power supply.

You would burn out the internal breadboard connections, nothing would work and the breadboard would be useless.

Could I branch out the thicker wire from the PSU (I'm just using a wired supply right now) to individual breadboard wires? Or would those burn out?