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
 5import board
 6import pwmio
 7from adafruit_motor import servo
 8
 9# create a PWMOut object on the control pin.
10pwm = pwmio.PWMOut(board.D5, duty_cycle=0, frequency=50)
11
12# To get the full range of the servo you will likely need to adjust the min_pulse and max_pulse to
13# match the stall points of the servo.
14# This is an example for the Sub-micro servo: https://www.adafruit.com/product/2201
15# servo = servo.Servo(pwm, min_pulse=580, max_pulse=2350)
16# This is an example for the Micro Servo - High Powered, High Torque Metal Gear:
17#   https://www.adafruit.com/product/2307
18# servo = servo.Servo(pwm, min_pulse=500, max_pulse=2600)
19# This is an example for the Standard servo - TowerPro SG-5010 - 5010:
20#   https://www.adafruit.com/product/155
21# servo = servo.Servo(pwm, min_pulse=400, max_pulse=2400)
22# This is an example for the Analog Feedback Servo: https://www.adafruit.com/product/1404
23# servo = servo.Servo(pwm, min_pulse=600, max_pulse=2500)
24# This is an example for the Micro servo - TowerPro SG-92R: https://www.adafruit.com/product/169
25# servo = servo.Servo(pwm, min_pulse=500, max_pulse=2400)
26
27# The pulse range is 750 - 2250 by default. This range typically gives 135 degrees of
28# range, but the default is to use 180 degrees. You can specify the expected range if you wish:
29# servo = servo.Servo(board.D5, actuation_range=135)
30servo = servo.Servo(pwm)
31
32# We sleep in the loops to give the servo time to move into position.
33print("Sweep from 0 to 180")
34for i in range(180):
35    servo.angle = i
36    time.sleep(0.01)
37print("Sweep from 180 to 0")
38for i in range(180):
39    servo.angle = 180 - i
40    time.sleep(0.01)
41
42print("Move to 90 degrees")
43servo.angle = 90
44time.sleep(1)
45print("Release servo motor for 10 seconds")
46servo.fraction = None
47time.sleep(10)
48
49# You can also specify the movement fractionally.
50print("Sweep from 0 to 1.0 fractionally")
51fraction = 0.0
52while fraction < 1.0:
53    servo.fraction = fraction
54    fraction += 0.01
55    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
 9from board import SCL, SDA
10import busio
11
12# Import the PCA9685 module. Available in the bundle and here:
13#   https://github.com/adafruit/Adafruit_CircuitPython_PCA9685
14from adafruit_pca9685 import PCA9685
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 = (
36    motor.SLOW_DECAY
37)  # Set motor to active braking mode to improve performance
38
39print("Forwards slow")
40motor4.throttle = 0.5
41print("throttle:", motor4.throttle)
42time.sleep(1)
43
44print("Forwards")
45motor4.throttle = 1
46print("throttle:", motor4.throttle)
47time.sleep(1)
48
49print("Backwards")
50motor4.throttle = -1
51print("throttle:", motor4.throttle)
52time.sleep(1)
53
54print("Backwards slow")
55motor4.throttle = -0.5
56print("throttle:", motor4.throttle)
57time.sleep(1)
58
59print("Stop")
60motor4.throttle = 0
61print("throttle:", motor4.throttle)
62time.sleep(1)
63
64print("Spin freely")
65motor4.throttle = None
66print("throttle:", motor4.throttle)
67
68pca.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
 9from board import SCL, SDA
10import busio
11
12# Import the PCA9685 module. Available in the bundle and here:
13#   https://github.com/adafruit/Adafruit_CircuitPython_PCA9685
14from adafruit_pca9685 import PCA9685
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
 6from board import SCL, SDA
 7import busio
 8
 9# Import the PCA9685 module. Available in the bundle and here:
10#   https://github.com/adafruit/Adafruit_CircuitPython_PCA9685
11from adafruit_pca9685 import PCA9685
12from adafruit_motor import servo
13
14i2c = busio.I2C(SCL, SDA)
15
16# Create a simple PCA9685 class instance.
17pca = 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)
22pca.frequency = 50
23
24# To get the full range of the servo you will likely need to adjust the min_pulse and max_pulse to
25# match the stall points of the servo.
26# This is an example for the Sub-micro servo: https://www.adafruit.com/product/2201
27# servo7 = servo.Servo(pca.channels[7], min_pulse=580, max_pulse=2350)
28# This is an example for the Micro Servo - High Powered, High Torque Metal Gear:
29#   https://www.adafruit.com/product/2307
30# servo7 = servo.Servo(pca.channels[7], min_pulse=500, max_pulse=2600)
31# This is an example for the Standard servo - TowerPro SG-5010 - 5010:
32#   https://www.adafruit.com/product/155
33# servo7 = servo.Servo(pca.channels[7], min_pulse=400, max_pulse=2400)
34# This is an example for the Analog Feedback Servo: https://www.adafruit.com/product/1404
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)
38
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)
42servo7 = servo.Servo(pca.channels[7])
43
44# We sleep in the loops to give the servo time to move into position.
45for i in range(180):
46    servo7.angle = i
47    time.sleep(0.03)
48for i in range(180):
49    servo7.angle = 180 - i
50    time.sleep(0.03)
51
52# You can also specify the movement fractionally.
53fraction = 0.0
54while fraction < 1.0:
55    servo7.fraction = fraction
56    fraction += 0.01
57    time.sleep(0.03)
58
59pca.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
 6from board import SCL, SDA
 7import busio
 8
 9# Import the PCA9685 module. Available in the bundle and here:
10#   https://github.com/adafruit/Adafruit_CircuitPython_PCA9685
11from adafruit_pca9685 import PCA9685
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()