Servo motor control

Hello, I am trying to control a servomotor with arduino uno, but i have a big problem. When I tried to control it using servo library for arduino, I have noticed that it has a problem when it generates the PWM. When I use it and fix a position for my servo (ej 60º) the servo goes to 60º but when the steady states has reached, i don't know why but it shake the position a few degree. When I saw the signal with the osciloscope i can see how the PWM signal change her period by themself, without any order. more or less it increase by 5 us the period of the PWM signal, enoght to be noticed by the ear. I don't know how to solve it, anybody could help me? Thanks

This is a common problem and a search in the forum search will return many posts that deal with the problem. Are you running the servo from the Arduino 5V? If not, what is your servo power supply?

I wonder what the "guaranteed" fluctuation in the output is: what does the datasheet say?

It's ages since I 'scoped the output of a servo line, but I don't recall seeing it move much. I'll dig my scope out and have a look.

The range of a servo is from 1000 to 2000 us for 0 to 180 degrees. That's 1000/180 = 5us/degree. So if your signal's changing by 5us as shown on your scope, how can that possibly mean "shake the position a few degree"; it will at most be 1 degree of movement, unless of course the servo's badly made.

As a matter of interest, and here I'm wondering if it's a power related thing, does the scope show the same fluctuation with the servo completely out of the circuit, including power and ground?

groundFungus: This is a common problem

Except the OP maintains that the output signal itself is changing, which is not the usual description of servo jitters.

What happens to the output signal when the power supply voltage is bouncing around? Servo pwm may not be affected by power supply.

It's happens even if i don't connect the motor, only with the generation of PWM. It's possible my Arduino could be broken? if I use PWM function it's don't happens, Probably it's a problem to generate the PWM with the registers configuration, due the small Duty cicle compared with the period. I am gonna crazy if i try to solve it....

groundFungus: What happens to the output signal when the power supply voltage is bouncing around? Servo pwm may not be affected by power supply.

Always good to eliminate things though. Seems he still gets the scope movement with the servos out of the picture.

It's happens even if i don't connect the motor

But even so, the variation on your scope will only account for a 1 degree wobble according to my calculations. If you get a "few" degrees jitter then that might be a power related thing, and I don't think you explained how the servos are powered.

I think that the problem isn't the power source of the motor, because I didn't connect it to Arduino to check the if it was a problem of the motor. But when I cheked the PWM output without the motor conneceted, I have noticed that there are a problem with the PWM generation. Also I have tested with a servo of 6V conected with a 7806 and a 5V servo conected to arduino. And the same errors...I think it's the function servo, but I am not sure.

Well my calc shows your scope variation would account for 1 degree of servo jitter, while you say you have a "few" whatever that means.

I don't have a sensor to mesure the position, but when i put closely my ear I can listening some shake, like a heart beat, and when i use the osciloscope I can see the variation of the duty cicle of the PWM period. I think that 1 degree isn't imporant, but I need that the servo stays in the same position to control the position of something, if it moves i can check correctly the thing that i am observing. Thanks for help!


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

Can you post your code please?

Please use code tags. Please read the first post in any forum entitled how to use this forum.,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.

Thanks.. Tom.. :)

Hello, I have tested without any motor, only with the PWM pin conected to osciloscope, also I have to say that the “vibration” is more close to 2,5 us. Isn’t to much but it is enogh to feel it.

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object

void loop() {  
  myservo.write(60);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there

Hi, Thanks for your code. Can you post your circuit diagram please.

Tom.... :)

Here it is!


The Servo library uses timer interrupts to control its outputs, not hardware PWM, if other interrupts are happening (like the normal timer0 interrupts that drive millis()), there will be jitter due to interrupts interfering.

Other libraries exist which get round this problem, but none of the alternatives can drive 12 servos at once(*) which is Servo library's main strength.

(*) 48 servos on the Mega

Hi, Do you have the required 10uF and 0.1uF around the LM7806?

Thanks ..Tom.. :)

Hi Tom! Yes I have all that it's need. But how I said before, I thinks is a problem of the arduino. When I use the servo library does the same efect that if there was a motor, but without it. TO test it don't need the motor, only the library and the osciloscope. Thanks!

Have you tested the program without motor? Or somebody tries it?

I can feel the administrative silence XD

Hi, Have you put the oscilloscope on the 6V and the 5V pin on the controller to check for noise? With and without the servo plugged in.

Tom.. :)