code.py
1 # SPDX-FileCopyrightText: 2019 Carter Nelson for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 # Example code for calibrating analog feedback values to servo range 6 7 import time 8 import board 9 import pwmio 10 from adafruit_motor import servo 11 from analogio import AnalogIn 12 13 # Pin setup 14 SERVO_PIN = board.A1 15 FEEDBACK_PIN = board.A5 16 17 # Calibration setup 18 ANGLE_MIN = 0 19 ANGLE_MAX = 180 20 21 # Setup servo 22 pwm = pwmio.PWMOut(SERVO_PIN, duty_cycle=2 ** 15, frequency=50) 23 servo = servo.Servo(pwm) 24 servo.angle = None 25 26 # Setup feedback 27 feedback = AnalogIn(FEEDBACK_PIN) 28 29 print("Servo feedback calibration.") 30 # Move to MIN angle 31 print("Moving to {}...".format(ANGLE_MIN), end="") 32 servo.angle = ANGLE_MIN 33 time.sleep(2) 34 print("Done.") 35 feedback_min = feedback.value 36 # Move to MAX angle 37 print("Moving to {}...".format(ANGLE_MAX), end="") 38 servo.angle = ANGLE_MAX 39 time.sleep(2) 40 print("Done.") 41 feedback_max = feedback.value 42 # Print results 43 print("Feedback MIN = {}".format(feedback_min)) 44 print("Feedback MAX = {}".format(feedback_max)) 45 # Deactivate servo 46 servo.angle = None