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