Simple control of a DC motor. DC motors have two wires and should not be connected directly to the PWM connections. Instead use intermediate circuitry to control a much stronger power source with the PWM. The Adafruit Stepper + DC Motor FeatherWing, Adafruit TB6612 1.2A DC/Stepper Motor Driver Breakout Board and Adafruit Motor/Stepper/Servo Shield for Arduino v2 Kit - v2.3 do this for popular form factors already.
The TB6612 boards feature three inputs XIN1, XIN2 and PWMX. Since we PWM the INs directly its expected that the PWM pin is consistently high.
Author(s): Scott Shawcroft
- class adafruit_motor.motor.DCMotor(positive_pwm: pwmio.PWMOut, negative_pwm: pwmio.PWMOut)¶
DC motor driver.
negative_pwmcan be swapped if the motor runs in the opposite direction from what was expected for “forwards”.
Motor controller recirculation current decay mode is selectable and defaults to
motor.SLOW_DECAYis recommended to improve spin threshold, speed-to-throttle linearity, and PWM frequency sensitivity.
Decay mode settings only effect the operational performance of controller chips such as the DRV8833, DRV8871, and TB6612. Either decay mode setting is compatible with discrete h-bridge controller circuitry such as the L9110H and L293D; operational performance is not altered.
- property decay_mode: int¶
Motor controller recirculation current decay mode. A value of
motor.FAST_DECAYsets the motor controller to the default fast recirculation current decay mode (coasting);
motor.SLOW_DECAYsets slow decay (braking) mode.
- adafruit_motor.motor.FAST_DECAY = 0¶
Recirculation current fast decay mode (coasting)
- adafruit_motor.motor.SLOW_DECAY = 1¶
Recirculation current slow decay mode (braking)
Servos are motor based actuators that incorporate a feedback loop into the design. These feedback loops enable pulse width modulated control to determine position or rotational speed.
Author(s): Scott Shawcroft
- class adafruit_motor.servo.ContinuousServo(pwm_out: pwmio.PWMOut, *, min_pulse: int = 750, max_pulse: int = 2250)¶
Control a continuous rotation servo.
- class adafruit_motor.servo.Servo(pwm_out: pwmio.PWMOut, *, actuation_range: int = 180, min_pulse: int = 750, max_pulse: int = 2250)¶
Control the position of a servo.
pwm_out (PWMOut) – PWM output object.
actuation_range (int) – The physical range of motion of the servo in degrees, for the given
min_pulse (int) – The minimum pulse width of the servo in microseconds.
max_pulse (int) – The maximum pulse width of the servo in microseconds.
actuation_rangeis an exposed property and can be changed at any time:
servo = Servo(pwm) servo.actuation_range = 135
The specified pulse width range of a servo has historically been 1000-2000us, for a 90 degree range of motion. But nearly all modern servos have a 170-180 degree range, and the pulse widths can go well out of the range to achieve this extended motion. The default values here of
2250typically give 135 degrees of motion. You can set
actuation_rangeto correspond to the actual range of motion you observe with your given
You can extend the pulse width above and below these limits to get a wider range of movement. But if you go too low or too high, the servo mechanism may hit the end stops, buzz, and draw extra current as it stalls. Test carefully to find the safe minimum and maximum.
Stepper motors feature multiple wire coils that are used to rotate the magnets connected to the motor shaft in a precise way. Each increment of the motor is called a step. Stepper motors have a varying number of steps per rotation so check the motor’s documentation to determine exactly how precise each step is.
Author(s): Tony DiCola, Scott Shawcroft
- adafruit_motor.stepper.BACKWARD = 2¶
- adafruit_motor.stepper.DOUBLE = 2¶
Step so that each step only activates two coils to produce more torque.
- adafruit_motor.stepper.FORWARD = 1¶
- adafruit_motor.stepper.INTERLEAVE = 3¶
Step half a step to alternate between single coil and double coil steps.
- adafruit_motor.stepper.MICROSTEP = 4¶
Step a fraction of a step by partially activating two neighboring coils. Step size is determined by
- adafruit_motor.stepper.SINGLE = 1¶
Step so that each step only activates a single coil
- class adafruit_motor.stepper.StepperMotor(ain1: pwmio.PWMOut | DigitalInOut, ain2: pwmio.PWMOut | DigitalInOut, bin1: pwmio.PWMOut | DigitalInOut, bin2: pwmio.PWMOut | DigitalInOut, *, microsteps: int | None = 16)¶
A bipolar stepper motor or four coil unipolar motor. The use of microstepping requires pins that can output PWM. For non-microstepping, can set microsteps to None and use digital out pins.
microsteps (int) – Number of microsteps between full steps. Must be at least 2 and even.
microsteps – set to
- onestep(*, direction: int = 1, style: int = 1) None ¶
Performs one step of a particular style. The actual rotation amount will vary by style.
DOUBLEwill normal cause a full step rotation.
INTERLEAVEwill normally do a half step rotation.
MICROSTEPwill perform the smallest configured step.