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