Simple tests

Ensure your device works with this simple test.

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

Motor PCA9685 DC Motor

This example uses an Adafruit Stepper and DC Motor FeatherWing to run a DC Motor

examples/motor_pca9685_dc_motor.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4# This example uses an Adafruit Stepper and DC Motor FeatherWing to run a DC Motor.
 5#   https://www.adafruit.com/product/2927
 6
 7import time
 8
 9import busio
10
11# Import the PCA9685 module. Available in the bundle and here:
12#   https://github.com/adafruit/Adafruit_CircuitPython_PCA9685
13from adafruit_pca9685 import PCA9685
14from board import SCL, SDA
15
16from adafruit_motor import motor
17
18i2c = busio.I2C(SCL, SDA)
19
20# Create a simple PCA9685 class instance for the Motor FeatherWing's default address.
21pca = PCA9685(i2c, address=0x60)
22pca.frequency = 100
23
24# Motor 1 is channels 9 and 10 with 8 held high.
25# Motor 2 is channels 11 and 12 with 13 held high.
26# Motor 3 is channels 3 and 4 with 2 held high.
27# Motor 4 is channels 5 and 6 with 7 held high.
28
29# DC Motors generate electrical noise when running that can reset the microcontroller in extreme
30# cases. A capacitor can be used to help prevent this. The demo uses motor 4 because it worked ok
31# in testing without a capacitor.
32# See here for more info: https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino/faq#faq-13
33pca.channels[7].duty_cycle = 0xFFFF
34motor4 = motor.DCMotor(pca.channels[5], pca.channels[6])
35motor4.decay_mode = motor.SLOW_DECAY  # Set motor to active braking mode to improve performance
36
37print("Forwards slow")
38motor4.throttle = 0.5
39print("throttle:", motor4.throttle)
40time.sleep(1)
41
42print("Forwards")
43motor4.throttle = 1
44print("throttle:", motor4.throttle)
45time.sleep(1)
46
47print("Backwards")
48motor4.throttle = -1
49print("throttle:", motor4.throttle)
50time.sleep(1)
51
52print("Backwards slow")
53motor4.throttle = -0.5
54print("throttle:", motor4.throttle)
55time.sleep(1)
56
57print("Stop")
58motor4.throttle = 0
59print("throttle:", motor4.throttle)
60time.sleep(1)
61
62print("Spin freely")
63motor4.throttle = None
64print("throttle:", motor4.throttle)
65
66pca.deinit()

Motor PCA9685 Stepper Motor

This example uses an Adafruit Stepper and DC Motor FeatherWing to run a Stepper Motor.

examples/motor_pca9685_stepper_motor.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4# This example uses an Adafruit Stepper and DC Motor FeatherWing to run a Stepper Motor.
 5#   https://www.adafruit.com/product/2927
 6
 7import time
 8
 9import busio
10
11# Import the PCA9685 module. Available in the bundle and here:
12#   https://github.com/adafruit/Adafruit_CircuitPython_PCA9685
13from adafruit_pca9685 import PCA9685
14from board import SCL, SDA
15
16from adafruit_motor import stepper
17
18i2c = busio.I2C(SCL, SDA)
19
20# Create a simple PCA9685 class instance for the Motor FeatherWing's default address.
21pca = PCA9685(i2c, address=0x60)
22pca.frequency = 1600
23
24# Motor 1 is channels 9 and 10 with 8 held high.
25# Motor 2 is channels 11 and 12 with 13 held high.
26# Motor 3 is channels 3 and 4 with 2 held high.
27# Motor 4 is channels 5 and 6 with 7 held high.
28
29pca.channels[7].duty_cycle = 0xFFFF
30pca.channels[2].duty_cycle = 0xFFFF
31stepper_motor = stepper.StepperMotor(
32    pca.channels[4], pca.channels[3], pca.channels[5], pca.channels[6]
33)
34
35for i in range(100):
36    stepper_motor.onestep()
37    time.sleep(0.01)
38
39for i in range(100):
40    stepper_motor.onestep(direction=stepper.BACKWARD)
41    time.sleep(0.01)
42
43pca.deinit()

Motor Servo Sweep

Show the servo sweep capabilities

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

Motor PCA9685 Continuous Motor

Show an example with a continuous motor

examples/motor_pca9685_continuous_servo.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import time
 5
 6import busio
 7
 8# Import the PCA9685 module. Available in the bundle and here:
 9#   https://github.com/adafruit/Adafruit_CircuitPython_PCA9685
10from adafruit_pca9685 import PCA9685
11from board import SCL, SDA
12
13from adafruit_motor import servo
14
15i2c = busio.I2C(SCL, SDA)
16
17# Create a simple PCA9685 class instance.
18pca = PCA9685(i2c)
19# You can optionally provide a finer tuned reference clock speed to improve the accuracy of the
20# timing pulses. This calibration will be specific to each board and its environment. See the
21# calibration.py example in the PCA9685 driver.
22# pca = PCA9685(i2c, reference_clock_speed=25630710)
23pca.frequency = 50
24
25# The pulse range is 750 - 2250 by default.
26servo7 = servo.ContinuousServo(pca.channels[7])
27# If your servo doesn't stop once the script is finished you may need to tune the
28# reference_clock_speed above or the min_pulse and max_pulse timings below.
29# servo7 = servo.ContinuousServo(pca.channels[7], min_pulse=750, max_pulse=2250)
30
31print("Forwards")
32servo7.throttle = 1
33time.sleep(1)
34
35print("Backwards")
36servo7.throttle = -1
37time.sleep(1)
38
39print("Stop")
40servo7.throttle = 0
41
42pca.deinit()