This example shows how to find the current position and use that to "seek" to a specific angle. Be sure to run the calibration program first and change these lines at the top of the code with your servo's values.

# Calibration setup
CALIB_MIN = 18112
CALIB_MAX = 49408

If you calibrated over a different range of angles, also change those lines to match.

# Example code for using analog feedback value to seek a position
import board
import pwmio
from simpleio import map_range
from adafruit_motor import servo
from analogio import AnalogIn

# Demo angles
angles = [0, 180, 0, 45, 180]

# Pin setup
SERVO_PIN = board.A1

# Calibration setup
CALIB_MIN = 18112
CALIB_MAX = 49408

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

def get_position():
    return map_range(feedback.value, CALIB_MIN, CALIB_MAX, ANGLE_MIN, ANGLE_MAX)

def seek_position(position, tolerance=2):
    servo.angle = position

    while abs(get_position() - position) > tolerance:

print("Servo feedback seek example.")
for angle in angles:
    print("Moving to {}...".format(angle), end="")

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

This page (Finding and Seeking) was last updated on Dec 01, 2021.

Text editor powered by tinymce.