Servo jitter, how do you fix it?

I've been searching high and low for 3 days non-stop to try and fix this issue I've been having. Using the standard servo library with Arduino I have really bad servo jittering. The exact same as this YouTube has here: Arduino + Servo + jitter :((((.mp4 - YouTube

I have not been able to find a single definitive solution. What am I missing? Do most people just deal with this problem? What other servo libraries are people using? Why is the default libraries producing such noisy results for controlling a servo?

Please help, how do I fix this!

Are you using cheap servos?
Is the controller sending a constant output?
Is your external servo power supply steady?

When I plug the servos directly into the RC reciever they have absolutely no jitter what so ever. I verified all my outputs to servo.write in serial debug and the values are all consistently the exact same, no noise coming in from the reciever. I have all the servos wires to +5v external battery, and arduino is powered from USB, a ground wire connects the arduino to the external +5v battery also.

Any ideas?

:sleeping: time.

Show us a good image of your wiring.

.

I confess I did not see any jitter in the video :slight_smile:

As well as making a drawing of your wiring connections and posting a photo of the drawing (NOT a photo of the hardware) you also need to post the program you are using.

And please use the code button </>so your code looks like thisand is easy to copy to a text editor

...R

The servo output from the Arduino (e.g. UNO) does jitter and I have also found this unacceptable. The problem is that the Arduino uses a timer, interrupts and software to terminate the pulse so if the processor is busy servicing another interrupt then the pulse gets extended by up to 10us. Modern servos, even the cheap ones, respond to this.

If you only need to drive 2 servos and are using an UNO then the ServoTimer1 library can drive one servo on pin 9 and one on pin 10.

As the library is quite old you will need to make some tweaks to make it compile under the later IDE's. Details can be found on this forum.

Saw a bit of jitter. Also saw a sloooow 9600 print monitor baudrate in the video. Not much else to report due to lack of visible code.

You haven't posted your code. Are you using other interrupts?

Are you needing millis()/micros()/delay() to work ? If not try TIMSK0=0; in setup()

TIMSK0=0; in setup() works quite well but you will still get jitter if you use the Serial class (e.g. Serial.print etc) so you loose that capability as well as the millis() etc >:(

The ServoTimer1 library produces pulses for 2 servos using a hardware only timer and thus has virtually no jitter. :slight_smile: However this solution will only be of use if you only need to drive 2 servos.

Based on the analysis in Reply #5 I suspect it is only an occasional pulse that would be the wrong width. Does the servo not average-out that sort of thing?

It would be interesting to see how the servo behaves with a program that deliberately includes an occasional slightly longer pulse.

...R

I have modified PWMServo library for arduino Mega2560 to get 9 true PWM output with OBSOLUTELY no jitter

it use 16bit timer 3 , 4 and 5

I dont asked the original developer yet if i could share my work , so for now i will only share if you PM me.

1 Like

As per this article - If the Servo Misbehaves | Arduino Lesson 14. Servo Motors | Adafruit Learning System , adding a 470uF capacitor between the power going to the servo fixed the problem for me.

Gavin1966:
As per this article

That Article shows the servo being powered from the Arduino 5v pin.

The general advice on this Forum is not to do that as the 5v pin cannot provide enough current. The large capacitor is just a band-aid. The servo should have its own power supply with the servo GND connected to the Arduino GND.

...R

1 Like

My robot project uses two continuous rotation servo motors to drive two wheels. I am using two 5V, 3A UBEC's. One UBEC is for the Arduino. The other UBEC is for the servo motors.

After doing some reading and trying to fix my two servo jitter problem, I read some more, mostly from this forum. I was going to post a question on how to fix the jitter problem with my two servos using an Arduino Uno compatible mcu.

There was some talk of using the Adafruit servo library. It helped when I wrote a test program for the new library. The program just ran a loop and here is the relevant code called from the loop:

void run_motors_2directions()
{
  for(int count = 30; count < 150; count++)
  {
    servo_right.write(180-count);
    servo_left.write(count);
    Serial.print("left wheel, count = ");
    Serial.println(count);
    Serial.print("right wheel, 180 - count = ");
    Serial.println(180 - count);
    delay(500);
  }
}

The motors stopped approximately when they were supposed to, around the 90 mark.
Then I wrote an even simpler test with the following code:

void test_motor()
{
  servo_right.write(90); // ==> 120 rotates the right motor forward (looking at it from its
  servo_left.write(120);
  delay(500);
}

This is where I ran into problems again. I read about a number of things; twisting wires together, adding a 470uF capacitor across the power, using a servo shield, etc. I tried the a 220uF cap but that did not work.

Then it occurred to me that the purpose of the capacitor is to smooth out any power surges and supply extra current to top up when the power dips below a level. My two servos always would stop jittering when I removed one signal lead from the Arduino. The servo that was left would work fine. It was the same when I removed the other signal line to operate the other servo. The thought that this brought to mind is that maybe the 3A supply from the one UBEC was not enough. Either that, or the UBEC is not actually supplying 3A and the two servos need it.

Another UBEC added to the project fixed the problem. The battery pack and batteries can handle the extra current draw. Both functions used to test the servo motors work as expected. Now there are three UBEC's, one for the Arduino and one for each servo motor. I suspect that I will no longer have problems of this sort when I work through the rest of the program.

I could try measuring the current used by the servo motors and be sure of how much current is needed but that is for another day.

I hope this helps.

tfoutfou:
I have modified PWMServo library for arduino Mega2560 to get 9 true PWM output with OBSOLUTELY no jitter

You mean much less jitter. If you had created something with zero jitter you'd have created something
physically impossible in a finite amount of time, due to Heisenberg's uncertainty principle.

I'm having the same problem when using the Adafruit_PWMServoDriver library (did not try the direct connection from arduino to servo, only through the PWM shield).

As a workaroud I'm using this approach:

  • I'm having a function which moves a certain servo to a degrees position by transforming the degrees parameter into pulseLenght
  • Depending on the speed of the servo I'm calculating the time needed to finish the move and then I'm setting the pulseLenght to 0, which acts like a power-off for the servo and the jitters stops immediately:
pwm.setPWM(servoNum, 0, 0);

This works great, although I'm not sure if it's a correct approach. The only issue is that by waiting until the move is finished, the code is not async anymore and, for example, another servo can't move in the same time. A possible solution would be to registers a sort of async timer when the move starts and call the "turn-off" logic once the time ends.

No idea yet if and how this would be possible :slight_smile:

I’m extremely new to the world of Arduino and programming in general. I had the same problem and fixed it by simply changing the pinMode for the servo to input when not required. When an action occurred that required the servo to move, the first instruction would be to change the pinMode back to output - do the servo movement - then change pinMode back to input. Mine was a very basic circuit and this was an easy work around but I image wouldn’t be a good solution for more complex sketches.

A possible Solution here :

This is out of my experience using from (1) to (10) Sg90 Motors on one of my Arduino Mini Pro - for Robotics.
I was using (2) of the 18650 Rechargeable Lithuim Batteries for this with a 5V VReg.

I did find the same problem listed above, more than (1) Servo caused Jitter - the more the merrier.
-> What I realized is that MANY Multiple Motors were still Draining off of the same Battery Voltage supply,

  • whether I altered their position or not via Arduino.

What this means is,

  • First that the Pulse may deviate (received by the Motor) slightly in its 1ms-2ms Pulse Size causing a random 'Jitter'.
  • Second, if multiple Motors are driving/adjusting position at the same time,
    then (1) or more Motor may Stop Short / miss its intended Position
    (because multiple motors Load may drag the Battery Voltage down more).
    Meaning, is that it will 'Jitter' around trying to find its Final Center New Position.

The Solution that I found is:

  • Enable / Attach() only (1) Motor (at at time) to position it - delay say between 1/10 - 1/2sec between Delay(100) to Delay(500) - then disable / Detach() it.
  • Then, Enable / Attach() the next Motor and position it.
  • Etc - through Each of the Motors.
    I would roll through a loop a few times - through All the Motors, to make sure that All the Motors had centered to their new position.

This got rid of my Jitter Problem.

This also Reduced the Load on the Battery - and so Extending the Battery's AmpHours.
-> Because this was Driving only (1) Motor at a time, instead of Powering ALL of them while they just sit around Jittering and Centering - and wasting Power.
Note: Even if the Motors seem to be in correct Position, they may waste Power just 'Jittering' if left enabled / Attach().

1 Like