It's easy to use the DRV8833 DC/Stepper Motor Driver or the TB6612 DC/Stepper Motor Driver breakouts with CircuitPython and Python using the Adafruit CircuitPython Motor library to control stepper motors. We'll show you how to wire them up and use the library to control a stepper motor. The code is the same for both breakouts, however the pinouts on each breakout are slightly different.

You can use this breakout with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.

CircuitPython Microcontroller Wiring

You can use any CircuitPython microcontroller board, but bear in mind that motors require external power to run. The Metro M0 and M4 have a convenient pin available for providing sufficient power, so this example will use the Metro M4.

For the VIN pin on the Metro to provide sufficient power to the stepper motor, you must plug in a 9V power supply into the barrel jack built into the Metro, and ensure the switch is "on".

We're using this stepper motor with this 9V power supply. With the Metro board, the 9V power supply will power both the Metro and the stepper motor.

Connect a Metro M4, the DRV8833 breakout and a stepper motor as follows:

  • Board VM to Metro VIN
  • Board GND to Metro GND
  • Board SLP to Metro 5V
  • Board AIN1 to Metro D9
  • Board AIN2 to Metro D10
  • Board BIN1 to Metro D11
  • Board BIN2 to Metro D12
  • Board AOUT (1 and 2) to stepper coil (red and yellow stepper wires)
  • Board BOUT (1 and 2) to stepper coil (green and grey stepper wires)
  • 9V power supply to barrel jack on Metro

For use with the TB6612, connect the Metro M4, breakout and stepper motor as follows:

  • Board VM to Metro VIN
  • Board VCC to breadboard power rail
  • Board GND to Metro GND
  • Board PWMB to breadboard power rail
  • Board BIN2 to Metro D12
  • Board BIN1 to Metro D11
  • Board AIN1 to Metro D9
  • Board AIN2 to Metro D10
  • Board PWMA to breadboard power rail
  • Breadboard power rail to Metro 5V
  • Board MOTORA (two pins) to stepper coil (red and yellow stepper wires)
  • Board MOTORB (two pins) to stepper coil (green and grey stepper wires)
  • 9V power supply to barrel jack on Metro

Python Computer Wiring

Since there's dozens of Linux computers/boards you can use we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported

The Raspberry Pi will not provide sufficient power to run the stepper motor. You must power the driver board externally with a 9V power supply for the motor to work.

We're using this stepper motor, and powering the driver board with this 9V power supply. With the Raspberry Pi, you'll need a separate 5V power supply for the Pi (on USB as normal).

Here's the Raspberry Pi, breakout and stepper motor wired up to the DRV8833:

  • Board VM to positive terminal on barrel jack
  • Board GND to negative terminal on barrel jack
  • Barrel jack to 9V power supply
  • Board GND to Pi GND
  • Board SLP to Pi 3.3V
  • Board AIN1 to Pi D19
  • Board AIN2 to Pi D26
  • Board BIN1 to Pi D20
  • Board BIN2 to Pi D21
  • Board AOUT (1 and 2) to stepper coil (red and yellow stepper wires)
  • Board BOUT (1 and 2) to stepper coil (green and grey stepper wires)

And, the Raspberry Pi, breakout and stepper motor wired up to the TB6612:

  • Board VM to positive terminal on barrel jack
  • Board GND to negative terminal on barrel jack
  • Barrel jack to 9V power supply
  • Board VCC to breadboard power rail
  • Board GND to Pi GND
  • Board PWMB to breadboard power rail
  • Board BIN2 to Pi D21
  • Board BIN1 to Pi D20
  • Board AIN1 to Pi D19
  • Board AIN2 to Pi D26
  • Board PWMA to breadboard power rail
  • Breadboard power rail to Pi 3.3V
  • Board MOTORA (two pins) to stepper coil (red and yellow stepper wires)
  • Board MOTORB (two pins) to stepper coil (green and grey stepper wires)

CircuitPython Installation of Motor Library

Next you'll need to install the Adafruit CircuitPython Motor library on your CircuitPython board.  

First make sure you are running the latest version of Adafruit CircuitPython for your board.

Next you'll need to install the necessary library to use the hardware--carefully follow the steps to find and install the library from Adafruit's CircuitPython library bundle.  Our introduction guide has a great page on how to install modules from the library bundle.

You'll need to manually install the necessary library from the bundle:

  • adafruit_motor

Before continuing make sure your board's lib folder or root filesystem has the  adafruit_motor folder copied over.

Python Installation of Motor Library

You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!

Once that's done, from your command line run the following command:

  • pip3 install adafruit-circuitpython-motor

If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!

CircuitPython & Python Usage

To demonstrate the usage of the DRV8833 and the TB6612, we'll use a complete code example to control a stepper motor. Code is the same for both boards.

Save the following code to your CIRCUITPY drive as code.py:

# Use this example for digital pin control of an H-bridge driver
# like a DRV8833, TB6612 or L298N.

import time
import board
import digitalio
from adafruit_motor import stepper

DELAY = 0.01
STEPS = 200

# You can use any available GPIO pin on both a microcontroller and a Raspberry Pi.
# The following pins are simply a suggestion. If you use different pins, update
# the following code to use your chosen pins.

# To use with CircuitPython and a microcontroller:
coils = (
    digitalio.DigitalInOut(board.D9),  # A1
    digitalio.DigitalInOut(board.D10),  # A2
    digitalio.DigitalInOut(board.D11),  # B1
    digitalio.DigitalInOut(board.D12),  # B2
)

# To use with a Raspberry Pi:
# coils = (
#     digitalio.DigitalInOut(board.D19),  # A1
#     digitalio.DigitalInOut(board.D26),  # A2
#     digitalio.DigitalInOut(board.D20),  # B1
#     digitalio.DigitalInOut(board.D21),  # B2
# )

for coil in coils:
    coil.direction = digitalio.Direction.OUTPUT

motor = stepper.StepperMotor(coils[0], coils[1], coils[2], coils[3], microsteps=None)

for step in range(STEPS):
    motor.onestep()
    time.sleep(DELAY)

for step in range(STEPS):
    motor.onestep(direction=stepper.BACKWARD)
    time.sleep(DELAY)

for step in range(STEPS):
    motor.onestep(style=stepper.DOUBLE)
    time.sleep(DELAY)

for step in range(STEPS):
    motor.onestep(direction=stepper.BACKWARD, style=stepper.DOUBLE)
    time.sleep(DELAY)

for step in range(STEPS):
    motor.onestep(style=stepper.INTERLEAVE)
    time.sleep(DELAY)

for step in range(STEPS):
    motor.onestep(direction=stepper.BACKWARD, style=stepper.INTERLEAVE)
    time.sleep(DELAY)

motor.release()

Once saved, watch your stepper motor move!

Let's take a look at the code. First, import the necessary libraries. Set the DELAY in seconds for the time between each motor control statement, and the number of STEPS used in each control block.

Next, set up the pins used by the driver board. If you followed the diagrams above, these will already be correct. If you did not, change these to match the pins you used. If you're using CircuitPython on a microcontroller, no changes are necessary. If you're using a Raspberry Pi with Adafruit Blinka, then you need to comment out the microcontroller pins, and uncomment the Raspberry Pi pins.

Then, set all the pins to OUTPUT, and instantiate motor with each of the four coils.

Now you can begin controlling the motor using the many options available in the Adafruit CircuitPython Motor library. You can check out the documentation for details.

That's all there is to controlling a stepper motor with a DRV8833 or the TB6612!

This guide was first published on Oct 26, 2016. It was last updated on Oct 26, 2016.

This page (Python & CircuitPython: Stepper Motors) was last updated on Nov 24, 2020.