Skip to content

Commit a889448

Browse files
authored
Merge pull request adafruit#23 from plamponi/pwm_freq
PWMOut variable frequency not required, uses only one counter
2 parents 7f93c2a + f7fb4d8 commit a889448

File tree

3 files changed

+56
-29
lines changed

3 files changed

+56
-29
lines changed

adafruit_motor/stepper.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,17 @@ class StepperMotor:
6969
:param ~pulseio.PWMOut bin2: `pulseio.PWMOut`-compatible output connected to the driver for
7070
the fourth coil (unipolar) or second input to second coil (bipolar).
7171
:param int microsteps: Number of microsteps between full steps. Must be at least 2 and even.
72+
73+
.. note:: The ``StepperMotor`` class requires PWM frequencies > 1500. Specify
74+
``frequency=1500`` or higher when instantiating the above ``PWMOut`` objects.
7275
"""
7376
def __init__(self, ain1, ain2, bin1, bin2, *, microsteps=16):
7477
self._coil = (ain2, bin1, ain1, bin2)
7578

76-
# set a safe pwm freq for each output
79+
# reject unsafe (low) pwm freq
7780
for i in range(4):
7881
if self._coil[i].frequency < 1500:
79-
self._coil[i].frequency = 2000
82+
raise ValueError("PWMOut: 'frequency' must be at least 1500")
8083

8184
self._current_microstep = 0
8285
if microsteps < 2:

examples/stepper_motor.py

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,24 @@
1-
# This example uses an Adafruit Stepper and DC Motor FeatherWing to run a Stepper Motor.
2-
# https://www.adafruit.com/product/2927
1+
# Run a Stepper Motor. Tested on ItsyBitsy M4 Express + DRV8833.
2+
# https://www.adafruit.com/product/3800
3+
# https://www.adafruit.com/product/3297
34

45
import time
56

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-
7+
import board
8+
import pulseio
139
from adafruit_motor import stepper
1410

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 = 1600
11+
AIn1 = pulseio.PWMOut(board.D9, frequency=1600)
12+
AIn2 = pulseio.PWMOut(board.D10, frequency=1600)
13+
BIn1 = pulseio.PWMOut(board.D11, frequency=1600)
14+
BIn2 = pulseio.PWMOut(board.D12, frequency=1600)
2015

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.
16+
stepper_motor = stepper.StepperMotor(AIn1, AIn2, BIn1, BIn2)
2517

26-
pca.channels[7].duty_cycle = 0xffff
27-
pca.channels[2].duty_cycle = 0xffff
28-
stepper_motor = stepper.StepperMotor(pca.channels[4], pca.channels[3], # Motor 3
29-
pca.channels[5], pca.channels[6]) # Motor 4
30-
31-
for i in range(100):
18+
for i in range(1000):
3219
stepper_motor.onestep()
3320
time.sleep(0.01)
3421

35-
for i in range(100):
22+
for i in range(1000):
3623
stepper_motor.onestep(direction=stepper.BACKWARD)
3724
time.sleep(0.01)
38-
39-
pca.deinit()

examples/stepper_motor_pca9685.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# This example uses an Adafruit Stepper and DC Motor FeatherWing to run a Stepper 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 stepper
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 = 1600
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+
pca.channels[7].duty_cycle = 0xffff
27+
pca.channels[2].duty_cycle = 0xffff
28+
stepper_motor = stepper.StepperMotor(pca.channels[4], pca.channels[3], # Motor 3
29+
pca.channels[5], pca.channels[6]) # Motor 4
30+
31+
for i in range(100):
32+
stepper_motor.onestep()
33+
time.sleep(0.01)
34+
35+
for i in range(100):
36+
stepper_motor.onestep(direction=stepper.BACKWARD)
37+
time.sleep(0.01)
38+
39+
pca.deinit()

0 commit comments

Comments
 (0)