Adafruit Motor Shield V2

You will be redirected back to this guide once you sign in, and can then subscribe to this guide.

Intermediate Project guide

Python & CircuitPython

We've written a handy CircuitPython library for the various DC Motor and Stepper kits called Adafruit CircuitPython MotorKit that handles all the complicated setup for you. All you need to do is import the appropriate class from the library, and then all the features of that class are available for use. We're going to show you how to import the MotorKit class and use it to control DC and stepper motors with the Adafruit Stepper + DC Motor Shield.

CircuitPython Microcontroller Wiring

First assemble the Shield exactly as shown in the previous pages. There's no wiring needed to connect the Shield to the Metro. The example below shows wiring two DC motors to the Shield once it has been attached to a Metro. You'll want to connect a barrel jack to the power terminal to attach an appropriate external power source to the Shield. The Shield will not function without an external power source!

CircuitPython Installation of MotorKit and Necessary Libraries

You'll need to install a few libraries on your Metro board.

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

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

If you choose, you can manually install the libraries individually on your board:

Before continuing make sure your board's lib folder or root filesystem has the adafruit_pca9685.mpy, adafruit_register, adafruit_motor, adafruit_bus_device and adafruit_motorkit.mpy files and folders copied over.

Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

CircuitPython Usage

To demonstrate the usage, we'll initialise the library and use Python code to control DC and stepper motors from the board's Python REPL.

First you'll need to import and initialize the MotorKit class.

from adafruit_motorkit import MotorKit kit = MotorKit()
from adafruit_motorkit import MotorKit kit = MotorKit()

The four motor spots on the Shield are available as motor1 , motor2 , motor3 , and motor4 .

In this example we'll use motor1 .

Note: For small DC motors like sold in the shop you might run into problems with electrical noise they generate and erratic behavior on your board. If you see erratic behavior like the motor not spinning or the board resetting at high motor speeds this is likely the problem. See this motor guide FAQ page for information on capacitors you can solder to the motor to reduce noise.

Now to move a motor you can set the throttle attribute. We don't call it speed because it doesn't correlate to a particular number of revolutions per minute (RPM). RPM depends on the motor and the voltage which is unknown.

For example to drive motor M1 forward at a full speed you set it to 1.0 :

kit.motor1.throttle = 1.0
kit.motor1.throttle = 1.0

To run the motor at half throttle forward use a decimal:

kit.motor1.throttle = 0.5
kit.motor1.throttle = 0.5

Or to reverse the direction use a negative throttle:

kit.motor1.throttle = -0.5
kit.motor1.throttle = -0.5

You can stop the motor with a throttle of 0 :

kit.motor1.throttle = 0
kit.motor1.throttle = 0

To let the motor coast and then spin freely set throttle to None .

kit.motor1.throttle = None
kit.motor1.throttle = None

That's all there is to controlling DC motors with CircuitPython! With DC motors you can build fun moving projects like robots or remote controlled cars that glide around with ease.

Stepper Motors

Similar DC motors, stepper motors are available as stepper1 and stepper2 . stepper1 is made up of the M1 and M2 terminals, and stepper2 is made up of the M3 and M4 terminals.

We'll use stepper1 in our example.

The most basic function (and the default) is to do one single coil step.

kit.stepper1.onestep()
kit.stepper1.onestep()

You can also call the onestep function with two optional keyword arguments. To use these, you'll need to import stepper as well.

from adafruit_motor import stepper
from adafruit_motor import stepper

Then you have access to the following options: