DEV Community

Cover image for Servo Motor Calibration: Does It Matter?
Ian Hong
Ian Hong

Posted on

Servo Motor Calibration: Does It Matter?

Introduction

Hobby servos are known for their low price, accessibility, and ease of use. However, it’s no secret that they are not meant for ultra high-precision tasks. What kind of tasks can be categorized as ultra high-precision? I hope to give some insight on the capabilities of these servos so you can decide that for yourself.

Below are some of the more common servos that are used in many beginner tutorials.

SG90 (blue), MG90S (transparent black), MG996 (opaque black)

Those are very jittery, so we will explore the next step up in quality, the DS3235.

For applications such as the steering mechanism of an RC car, the jitter and inaccuracy doesn’t matter because you just want to turn some amount in a direction. Where positional accuracy does matter is in applications such as robot arms and pan/tilt camera mechanisms.

Basic Operation

Before we dive into the calibration, it’s important to understand the fundamentals of how these servos operate.

To rotate the servo to a desired position, we use a microcontroller to send a pulse-width modulated (PWM) signal of a certain width (a length of time) to the servo. The servo rotates to an angle based on the width of the pulse.

Basic PWM control of a servo motor

Calibration

Now the question is: how do you know what pulse width to send for each angle? The answer varies wildly by model of servo, and there is even variation between units of the same model. Hence the need for calibration if you want to maximize accuracy.

The goal of the calibration is to map pulse width values to servo angles, and by attempting to do so we will be able to observe the relationship between PWM signal and servo angle. Is it truly linear? Is it exponential? Is there no clear relationship? We’ll find out!

Test Setup

To conduct this test, we need to be able to control the servo with an Arduino with a PCA9685 servo driver and see the servo’s position. The servo’s position is measured using a 3D printed a jig with notches every 15 degrees and an arm with a pointed tip.

3D printed jig for servo calibration

Arduino Code

Below is a minimal Arduino code snippet to rotate the servo to a position.

#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver servo = Adafruit_PWMServoDriver(0x40);

#define SERVO_FREQ 60 // Analog servos run at ~50 Hz updates

void setup() {
    servo.setPWMFreq(SERVO_FREQ)
    servo.setPWM(3, 0, 390);  // 390 is the PWM value, this is what you're changing.
}

void loop() {
}
Enter fullscreen mode Exit fullscreen mode

Experimentation

The goal is to find the exact PWM value that aligns the servo arm with each notch.

  1. Start at one extreme of the servo’s motion.
  2. Send different PWM values until the arm aligns as closely as possible with the notch at that extreme.
  3. Once the arm is aligned with the notch, record the angle of that notch (remember they are at 15 degree increments) and the corresponding PWM value.
  4. Repeat steps 2 and 3 for every notch through the servo’s entire range.

Data Processing

Next, we plot the data we collected to look for a relationship between the pulse width and the servo angle.
Pulse width vs. servo angle

The relationship looks pretty linear! That’s good news, because it means we can easily calculate the pulse width required for any servo angle using the line of best fit (which can be obtained in Excel).

For some further analysis, we can also look into the variations in the data. If the relationship was perfectly linear, we would observe that the difference in PWM values at any 15 degree servo increment is the same.
PWM differences at each 15 degree increment on the servo

The plot above shows some variation, which could be attributed to the following sources of error:

  • Measurements done by eye
  • Deadband (minimum PWM change to move the servo)
  • Gear backlash causing hysteresis

Conclusion

Turns out the DS3235 servo is pretty accurate and linear, or at least the one I tested, so you can probably get away with not performing a full calibration as long as you know the pulse width difference between two known positions. Now you know how to calibrate a servo, obtain a relationship between PWM and servo angle, and use that relationship to position the servo accurately.

Happy experimenting!

Top comments (0)