|
1 | 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 |
| 2 | +import board |
| 3 | +import pulseio |
9 | 4 | from adafruit_motor import servo
|
10 | 5 |
|
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 |
| 6 | +# create a PWMOut object on the control pin. |
| 7 | +pwm = pulseio.PWMOut(board.D5, duty_cycle=0, frequency=50) |
20 | 8 |
|
21 | 9 | # To get the full range of the servo you will likely need to adjust the min_pulse and max_pulse to
|
22 | 10 | # match the stall points of the servo.
|
23 | 11 | # 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) |
| 12 | +# servo = servo.Servo(pwm, min_pulse=580, max_pulse=2350) |
25 | 13 | # This is an example for the Micro Servo - High Powered, High Torque Metal Gear:
|
26 | 14 | # https://www.adafruit.com/product/2307
|
27 |
| -# servo7 = servo.Servo(pca.channels[7], min_pulse=500, max_pulse=2600) |
| 15 | +# servo = servo.Servo(pwm, min_pulse=500, max_pulse=2600) |
28 | 16 | # This is an example for the Standard servo - TowerPro SG-5010 - 5010:
|
29 | 17 | # https://www.adafruit.com/product/155
|
30 |
| -# servo7 = servo.Servo(pca.channels[7], min_pulse=400, max_pulse=2400) |
| 18 | +# servo = servo.Servo(pwm, min_pulse=400, max_pulse=2400) |
31 | 19 | # 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) |
| 20 | +# servo = servo.Servo(pwm, min_pulse=600, max_pulse=2500) |
33 | 21 | # 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) |
| 22 | +# servo = servo.Servo(pwm, min_pulse=500, max_pulse=2400) |
35 | 23 |
|
36 | 24 | # The pulse range is 750 - 2250 by default. This range typically gives 135 degrees of
|
37 | 25 | # 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]) |
| 26 | +# servo = servo.Servo(board.D5, actuation_range=135) |
| 27 | +servo = servo.Servo(pwm) |
40 | 28 |
|
41 | 29 | # We sleep in the loops to give the servo time to move into position.
|
| 30 | +print("Sweep from 0 to 180") |
42 | 31 | for i in range(180):
|
43 |
| - servo7.angle = i |
44 |
| - time.sleep(0.03) |
| 32 | + servo.angle = i |
| 33 | + time.sleep(0.01) |
| 34 | +print("Sweep from 180 to 0") |
45 | 35 | for i in range(180):
|
46 |
| - servo7.angle = 180 - i |
47 |
| - time.sleep(0.03) |
| 36 | + servo.angle = 180 - i |
| 37 | + time.sleep(0.01) |
| 38 | + |
| 39 | +print("Move to 90 degrees") |
| 40 | +servo.angle = 90 |
| 41 | +time.sleep(1) |
| 42 | +print("Release servo motor for 10 seconds") |
| 43 | +servo.fraction = None |
| 44 | +time.sleep(10) |
48 | 45 |
|
49 | 46 | # You can also specify the movement fractionally.
|
| 47 | +print("Sweep from 0 to 1.0 fractionally") |
50 | 48 | fraction = 0.0
|
51 | 49 | while fraction < 1.0:
|
52 |
| - servo7.fraction = fraction |
| 50 | + servo.fraction = fraction |
53 | 51 | fraction += 0.01
|
54 |
| - time.sleep(0.03) |
55 |
| - |
56 |
| -pca.deinit() |
| 52 | + time.sleep(0.01) |
0 commit comments