Skip to content

Commit 735c196

Browse files
authored
Merge pull request #36 from jvalrog/fix-servo-example
fixed pca9685_servo.py example
2 parents 4bea3ce + 57adf7c commit 735c196

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

examples/pca9685_servo.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,59 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
# This example moves a servo its full range (180 degrees by default) and then back.
4+
import time
55

66
from board import SCL, SDA
77
import busio
88

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
1111
from adafruit_motor import servo
12-
13-
# Import the PCA9685 module.
1412
from adafruit_pca9685 import PCA9685
1513

1614
i2c = busio.I2C(SCL, SDA)
1715

1816
# Create a simple PCA9685 class instance.
1917
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)
2022
pca.frequency = 50
2123

2224
# To get the full range of the servo you will likely need to adjust the min_pulse and max_pulse to
2325
# match the stall points of the servo.
2426
# 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)
2628
# This is an example for the Micro Servo - High Powered, High Torque Metal Gear:
2729
# 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)
2931
# This is an example for the Standard servo - TowerPro SG-5010 - 5010:
3032
# 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)
3234
# 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)
3438

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)
3642
servo7 = servo.Servo(pca.channels[7])
3743

44+
# We sleep in the loops to give the servo time to move into position.
3845
for i in range(180):
3946
servo7.angle = i
47+
time.sleep(0.03)
4048
for i in range(180):
4149
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+
4259
pca.deinit()

0 commit comments

Comments
 (0)