/ examples / motor_pca9685_servo_sweep.py
motor_pca9685_servo_sweep.py
 1  import time
 2  
 3  from board import SCL, SDA
 4  import busio
 5  
 6  # Import the PCA9685 module. Available in the bundle and here:
 7  #   https://github.com/adafruit/Adafruit_CircuitPython_PCA9685
 8  from adafruit_pca9685 import PCA9685
 9  from adafruit_motor import servo
10  
11  i2c = busio.I2C(SCL, SDA)
12  
13  # Create a simple PCA9685 class instance.
14  pca = PCA9685(i2c)
15  # You can optionally provide a finer tuned reference clock speed to improve the accuracy of the
16  # timing pulses. This calibration will be specific to each board and its environment. See the
17  # calibration.py example in the PCA9685 driver.
18  # pca = PCA9685(i2c, reference_clock_speed=25630710)
19  pca.frequency = 50
20  
21  # To get the full range of the servo you will likely need to adjust the min_pulse and max_pulse to
22  # match the stall points of the servo.
23  # This is an example for the Sub-micro servo: https://www.adafruit.com/product/2201
24  # servo7 = servo.Servo(pca.channels[7], min_pulse=580, max_pulse=2350)
25  # This is an example for the Micro Servo - High Powered, High Torque Metal Gear:
26  #   https://www.adafruit.com/product/2307
27  # servo7 = servo.Servo(pca.channels[7], min_pulse=500, max_pulse=2600)
28  # This is an example for the Standard servo - TowerPro SG-5010 - 5010:
29  #   https://www.adafruit.com/product/155
30  # servo7 = servo.Servo(pca.channels[7], min_pulse=400, max_pulse=2400)
31  # This is an example for the Analog Feedback Servo: https://www.adafruit.com/product/1404
32  # servo7 = servo.Servo(pca.channels[7], min_pulse=600, max_pulse=2500)
33  # This is an example for the Micro servo - TowerPro SG-92R: https://www.adafruit.com/product/169
34  # servo7 = servo.Servo(pca.channels[7], min_pulse=500, max_pulse=2400)
35  
36  # The pulse range is 750 - 2250 by default. This range typically gives 135 degrees of
37  # range, but the default is to use 180 degrees. You can specify the expected range if you wish:
38  # servo7 = servo.Servo(pca.channels[7], actuation_range=135)
39  servo7 = servo.Servo(pca.channels[7])
40  
41  # We sleep in the loops to give the servo time to move into position.
42  for i in range(180):
43      servo7.angle = i
44      time.sleep(0.03)
45  for i in range(180):
46      servo7.angle = 180 - i
47      time.sleep(0.03)
48  
49  # You can also specify the movement fractionally.
50  fraction = 0.0
51  while fraction < 1.0:
52      servo7.fraction = fraction
53      fraction += 0.01
54      time.sleep(0.03)
55  
56  pca.deinit()