This example shows how to control both speed and direction of a DC motor using CircuitPython.
The DRV8833 can drive two DC motors even though this example only shows a single motor on the "A" set of pins. A second motor can be driven in the same way. Just use the "B" set of pins for both input and output. Code would be the same, updated for the specific pins connected to the "B" channel inputs.
Wiring
Here is the wiring diagram to use for these examples. We show a Metro M4 Express, but in general any microcontroller can be used. For speed control, PWM outputs are needed, two per DC motor.
The Metro connections are:
- GND to GND
- AIN1 to Digital 5
- AIN2 to Digital 6
- SLP to Digital 7
Motor connections are:
- AOUT1 to DC motor
- AOUT2 to DC motor
The wiring connection order for the DC motor generally does not matter and will only affect the resulting motor direction.
External power is used for the motor and is connected via the screw terminal blocks. This power supply must be enough to support whatever DC motor is being used, within the range capable of the DRV8833 (2.7V to 10.8V).
The SLP pin can also be tied directly to 3.3V output pin on the Metro for the DRV8833 to be "always active".
Basic ON/OFF Control
This example does not control speed but is useful for understanding basic motor control. For this, only two digital output pins are needed, which should be readily available on any microcontroller board.
# SPDX-FileCopyrightText: 2025 Carter Nelson for Adafruit Industries # # SPDX-License-Identifier: MIT import time import board import digitalio # Configure pins AIN1 = digitalio.DigitalInOut(board.D5) AIN2 = digitalio.DigitalInOut(board.D6) SLP = digitalio.DigitalInOut(board.D7) AIN1.switch_to_output() AIN2.switch_to_output() SLP.switch_to_output() # Enable DRV8833 SLP.value = True # Loop forever while True: # # FORWARD # print("Forward") AIN1.value = True AIN2.value = False time.sleep(1) # # REVERSE # print("Reverse") AIN1.value = False AIN2.value = True time.sleep(1)
PWM Speed Control
OK, now let's control speed using PWM. The pins 5 and 6 on the Metro M4 were chosen since they support PWM output. The general idea is to set one pin LOW (or HIGH) and then PWM the other pin.
# SPDX-FileCopyrightText: 2025 Carter Nelson for Adafruit Industries # # SPDX-License-Identifier: MIT import time import board import digitalio import pwmio # Configure pins AIN1 = pwmio.PWMOut(board.D5, frequency=2000) AIN2 = pwmio.PWMOut(board.D6, frequency=2000) SLP = digitalio.DigitalInOut(board.D7) SLP.switch_to_output() # Enable DRV8833 SLP.value = True # Loop forever while True: # # FORWARD # print("Forward") AIN2.duty_cycle = 0 print(" ramping up") for duty_cycle in range(0, 65536, 100): AIN1.duty_cycle = duty_cycle time.sleep(0.01) print(" ramping down") for duty_cycle in range(65535, -1, -100): AIN1.duty_cycle = duty_cycle time.sleep(0.01) # # REVERSE # print("Reverse") AIN1.duty_cycle = 0 print(" ramping up") for duty_cycle in range(0, 65536, 100): AIN2.duty_cycle = duty_cycle time.sleep(0.01) print(" ramping down") for duty_cycle in range(65535, -1, -100): AIN2.duty_cycle = duty_cycle time.sleep(0.01)
Page last edited March 25, 2025
Text editor powered by tinymce.