# Why a servo doesnt move to angles properly

I've got to do a project which uses a servo to drive a turntable. How much I adjusted the angle, the servo seemed to shift every time it spinned. For instance, the 0 degree angle was getting a bit bigger. I think it is caused by the imprecise PWM control of the Arduino or the bad translation using, I think, the map() function.

Could you fix this issue in code, or the servo doesn't work properly. I observed that every servo (that I've tried) creates a buzzing noice for angles bigger than 170 degrees.

By the way, my code is:

#include

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

#define pos0 0
#define pos1 23
#define pos2 48
#define pos3 164

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

void loop() {
for (pos = pos0; pos  pos0; pos -= 1) {
myservo.write(pos);
delay(30);
}
Serial.print("pos: ");
Serial.println(pos);
delay(5000);
}


Edit: Should I try with a more powerful Arduino, like the Mega, which I think will be the perfect option having more GPIO than the UNO.

Every servo is different, they can have varying specifications:

• Angular speed
• Rotation range
• Stall Torque
• Size
• Gear material/quality

Servos will vary based on brand; in addition each servo is going to have some individual variance. So one has to characterize the servo, and discover what the proper control values are. Once the control values are established these are used to initialize the library so angular control will be accurate.

For instance, the 0 degree angle was getting a bit bigger

I have found that errors in repeatability, like this, are usually found in the mechanism the servo is driving, especially the connection between the servo and the driven mechanisms. Check the connection for slop, see if everything is tightened properly.

I observed that every servo (that I've tried) creates a buzzing noice for angles bigger than 170 degrees.

The typical maximum range of a servo is 160-190 degrees of rotation. The noise you are hearing is the servo trying to drive the motor to set the angle, but it is stalled as it can't turn that far.

Should I try with a more powerful Arduino, like the Mega, which I think will be the perfect option having more GPIO than the UNO.

IIRC the servo library can control 12 servos with a basic Arduino, and 48 with the MEGA. This difference is more than just pin count, the MEGA has 4x the hardware timers of the 328p; 12 x 4 = 48

If you need more features you can move to the MEGA, i.e. you need more PWM channels, and the servo library reduces PWM pins from 6 to 4 on the 328p. Another example could needing more timers.

Read the Arduino Servo examples for more information on usage of the servo library and control of servos, etc.

Try to use myservo.writeMicroseconds(x) function. For standard servos the value of parameter x is between 1000 to 2000 for 0 degree to 180 degree rotation. But you know all servos are not manufactured perfectly. Thats why servos can response to 700 to 2300. At first you have to verify the minimum and maximum responding values then use it. You can use map() for easy coding.

Not all Servos are perfectly built. One can get rid of the library itself; there is a way to make a servo turn by manually writing out the code.

for(x=0; x<150; x=x+1)
{
digitalWrite(Servomotor, HIGH);
delayMicroseconds(1500);          //Edit this delay to adjust the position
digitalWrite(Servomotor, LOW);
delayMicroseconds(18550);         //Don't touch this delay
}


By trying out different delays for the first delayMicroseconds()`, you can test out which delay you need for what position.

To make your code more clean, you could safe different angles in another void function, and when you need them just type out the name

Have a nice day ;)

• IIRC the servo library uses timers to generate the pulses so that you can still do other things while using the servo; with your method you will have to eventually re-implement the servo library yourself. – esoterik Jan 29 '19 at 23:36