1

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.

How a servo works

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.

4 Answers 4

1

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.

1

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.

0

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 ;)

1
  • 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, 2019 at 23:36
0

An inexpensive servo tester, e.g....

https://www.amazon.com/DIYmall-Digital-Consistency-Controler-Checker/dp/B012LZKTDO/ref=sr_1_12?crid=3BLBBVDUPTN53&keywords=servo+tester&qid=1644245707&sprefix=servo+tester%2Caps%2C93&sr=8-12

...is useful for determining the exact mechanical limits of a specific servo quickly and easily. If the servo will only physically move 170 degrees and the connected mechanism requires 180 degrees, a different servo is needed.

It should be possible to use an oscilloscope connected to the servo control lead to determine a) what the tester output duty cycle is at specific angular positions, and b) what the actual Arduino output duty cycle is when running a program.

Use the servo tester to assure that the range of motion of your servo is adequate to correctly operate the mechanism.

Note the tester output at desired positions.

Adjust your program to produce the duty cycle(s) necessary to replicate the tester output at the desired positions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.