You can use the program below to help determine the feedback values that correspond to your servo's range of motion.

If you want to calibrate over a difference angle range, change these lines at the top. However, 0 and 180 are the maximum limits.

# Calibration setup
ANGLE_MIN = 0
ANGLE_MAX = 180

When the code runs, it will print out the analog reading values that correspond to the min/max angles. Write these values down - they'll be used in the other examples.

# SPDX-FileCopyrightText: 2023 Carter Nelson for Adafruit Industries
#
# SPDX-License-Identifier: MIT

# Example code for calibrating analog feedback values to servo range

import time
import board
import pwmio
from analogio import AnalogIn
from adafruit_motor import servo

# Pin setup
SERVO_PIN = board.A1
FEEDBACK_PIN = board.A3

# Calibration setup
ANGLE_MIN = 0
ANGLE_MAX = 180

# Setup servo
pwm = pwmio.PWMOut(SERVO_PIN, duty_cycle=2 ** 15, frequency=50)
servo = servo.Servo(pwm)
servo.angle = None

# Setup feedback
feedback = AnalogIn(FEEDBACK_PIN)

print("Servo feedback calibration.")

# Helper function to average analog readings
def read_feedback(samples=10, delay=0.01):
    reading = 0
    for _ in range(samples):
        reading += feedback.value
        time.sleep(delay)
    return int(reading/samples)

# Move to MIN angle
print("Moving to {}...".format(ANGLE_MIN), end="")
servo.angle = ANGLE_MIN
time.sleep(2)
print("Done.")
feedback_min = read_feedback()

# Move to MAX angle
print("Moving to {}...".format(ANGLE_MAX), end="")
servo.angle = ANGLE_MAX
time.sleep(2)
print("Done.")
feedback_max = read_feedback()

# Print results
print("="*20)
print("Feedback MIN = {}".format(feedback_min))
print("Feedback MAX = {}".format(feedback_max))
print("="*20)

# Deactivate servo
servo.angle = None

When the code runs, it will print out the analog reading values that correspond to the min/max angles.

In the output above, the two values of interest are 15377 and 42890. Write these values down - they'll be used in the other examples.

This guide was first published on Aug 24, 2013. It was last updated on Apr 14, 2024.

This page (Calibrating The Feedback) was last updated on Apr 14, 2024.

Text editor powered by tinymce.