Adafruit Motor Shield v2 for Arduino
Adafruit Motor Shield v2 for Arduino
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Overview 5
FAQ 7
Install Software 22
• Install Adafruit Motor Shield V2 library
• Running the Example Code
•
• DC Motor
• Stepper Motor Test
Library Reference 27
• class Adafruit_MotorShield;
• Adafruit_MotorShield(uint8_t addr = 0x60);
• void begin(uint16_t freq = 1600);
• Adafruit_DCMotor *getMotor(uint8_t n);
• Adafruit_StepperMotor *getStepper(uint16_t steps, uint8_t n);
• void setPWM(uint8_t pin, uint16_t val);void setPin(uint8_t pin, boolean val);
• class Adafruit_DCMotor
• Adafruit_DCMotor(void);
• void run(uint8_t);
• void setSpeed(uint8_t);
• class Adafruit_StepperMotor
• Adafruit_StepperMotor(void);
• void step(uint16_t steps, uint8_t dir, uint8_t style = SINGLE);
• void setSpeed(uint16_t);
• uint8_t onestep(uint8_t dir, uint8_t style);
• void release(void);
Powering Motors 32
• Voltage requirements:
• Current requirements:
• Setting up your shield for powering Hobby Servos
• Setting up your shield for powering DC and Stepper Motors
• If you would like to have a single DC power supply for the Arduino and motors
• If you would like to have the Arduino powered off of USB and the motors powered off of a DC power supply
• If you would like to have 2 separate DC power supplies for the Arduino and motors.
Using RC Servos 35
• Powering Servos
Using DC Motors 37
• Connecting DC Motors
Python Docs 46
Stacking Shields 46
• Addressing the Shields
• Writing Code for Multiple Shields
Resources 49
• Motor ideas and tutorials
The original Adafruit Motorshield kit is one of our most beloved kits, which is why we
decided to make something even better. We have upgraded the shield kit to make the
bestest, easiest way to drive DC and Stepper motors. This shield will make quick work
of your next robotics project! We kept the ability to drive up to 4 DC motors or 2
stepper motors, but added many improvements:
Instead of a L293D darlington driver, we now have the TB6612 MOSFET drivers
with 1.2A per channel current capability (you can draw up to 3A peak for approx 20ms
at a time). It also has much lower voltage drops across the motor so you get more
torque out of your batteries, and there are built-in flyback diodes as well.
Instead of using a latch and the Arduino's PWM pins, we have a fully-dedicated PWM
driver chip onboard. This chip handles all the motor and speed controls over I2C.
Only two GPIO pins (SDA & SCL) plus 5v and GND are required to drive the multiple
motors, and since it's I2C you can also connect any other I2C devices or shields to
the same pins. This also makes it drop-in compatible with any Arduino, such as the
Uno, Leonardo, Due and Mega R3.
Motor control is through I2C bus communication with this board and not through
PWM, serial, or other methods.
Lots of other little improvements such as a polarity protection FET on the power pins
and a big prototyping area. And the shield is assembled and tested here at Adafruit
so all you have to do is solder on straight or stacking headers and the terminal blocks.
FAQ
How many motors can I use with this shield?
You can use 2 DC hobby servos that run on 5V and up to 4 DC motors or 2 stepper
motors (or 1 stepper and up to 2 DC motors) that run on 5-12VDC
It can work with Mega R2 and lower if you solder a jumper wire from the shield's
SDA pin to Digital 20 and the SCL pin to Digital 21
To use a V3 shield with older processor boards that do not have an IOREF pin, you
must configure the logic voltage level. Find the set of 3 pads labeled "Logic" and
solder a bridge from the center pad to either the 5v or 3v pad on either side
depending on the voltage of your board (e.g. 5v for UNO & Mega. 3v for Due).
As of Arduino 1.5.6-r2 BETA, there is a bug in the Due Wire library that prevents
multiple Motor Shields from working properly!
The NEMA 17 motor we have in the shop has a phase resistance of about 35 ohms,
so it is a good match for the shield.
See this guide for details: Matching the Driver to the Stepper (https://adafru.it/r2d)
The shield uses the SDA and SCL i2c pins to control DC and stepper motors. On
the Arduino UNO these are also known as A4 and A5. On the Mega these are also
known as Digital 20 and 21. On the Leonardo these are also known as digital 2 and
3. Do not use those pins on those Arduinos with this shield with anything other
than an i2c sensor/driver.
Since the shield uses I2C to communicate, you can connect any other i2c sensor or
driver to the SDA/SCL pins as long as they do not use address 0x60 (the default
address of the shield) or 0x70 (the 'all call' address that this chip uses for group-
control)
If you want to use the servo connections, they are on pins #9 and #10. If you do not
use the connector then those pins are simply not used.
Note that pins A4 and A5 are connected to SDA and SCL for compatibility with
classic Arduinos. These pins are not available for use on other processors.
Or, you can add the following code to your setup() function: (Note: this line must
be inserted after the call to begin())
We don't show soldering in the 2x3 stacking header but you should solder that in
as well - even though this shield does not use it, the one above may need those
pins!
Install Software
Install Adafruit Motor Shield V2 library
To use the shield on an Arduino, you'll need to install the Adafruit Motorshield v2
library. This library is not compatible with the older AF_Motor library used for v1
shields. However, if you have code for the older shield, adapting the code to use the
new shield isn't difficult. We had to change the interface a little to support shield
stacking, & we think its worth it!
If you plan to use AccelStepper for acceleration control or for simultaneous control of
multiple stepper motors, you will also need to download and install the AccelStepper
library:
AccelStepper Library
https://adafru.it/IpB
For more details on how to install Arduino libraries, check out our detailed
tutorial! (https://adafru.it/aYM)
DC Motor
The library comes with a few examples to get you started up fast. We suggest getting
started with the DC motor example. You can use any DC motor that can be powered
by 6V-12VDC
First, restart the IDE to make sure the new library is loaded.
You must also supply 5-12VDC to power the motor. There are two ways to do this
1. You can power the Arduino via the DC Barrel Jack and insert the VIN Jumper
shown as the tall black handle right next to the green Power LED below
2. You can power the Arduino via the DC Barrel jack or USB port. Then Power the
shield via the 5-12VDC motor power terminal port, the double terminal block
next to the green Power LED and remove the VIN jumper
If the Green LED next to the power terminal block isn't lit up brightly do not
continue!
You should see and hear the DC motor turn on and move back and forth, attaching a
slip of paper or tape as a 'flag' can help you visualize the movement if you have
trouble seeing the movement
Plug the shield into the Arduino and connect a stepper motor to motor port 2 - unlike
DC motors, the wire order does 'matter'. Connect to the top two terminal ports (coil #1)
and the bottom two terminal ports (coil #2).
• If you have a bipolar motor, do not connect to the middle pin (GND).
• If you are using a unipolar motor with 5 wires, connect the common wire to
GND.
• If you are using a unipolar motor with 6 wires, you can connect the two 'center
coil wires' together to GND
1. You can power the Arduino via the DC Barrel Jack and insert the VIN Jumper
shown as the tall black handle right next to the green Power LED below
2. You can power the Arduino via the DC Barrel jack or USB port. Then Power the
shield via the 5-12VDC motor power terminal port, the double terminal block
next to the green Power LED and remove the VIN jumper
If the Green LED isn't lit up brightly do not continue - you must power it via the VIN
jumper or the terminal block
You should see and hear the stepper motor turn on and move back and forth,
attaching a slip of paper or tape as a 'flag' can help you visualize the movement if you
have trouble seeing the movement. There are four ways to move a stepper, with
varying speed, torque and smoothness tradeoffs. This example code will demonstrate
all four.
Library Reference
class Adafruit_DCMotor
The Adafruit_DCMotor class represents a DC motor attached to the shield. You must
declare an Adafruit_DCMotor for each motor in your system.
Adafruit_DCMotor(void);
The constructor takes no arguments. The motor object is typically initialized by
assigning a motor object retrieved from the shield class as below:
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
void run(uint8_t);
The run() function controls the motor state. The parameter can have one of 3 values:
Note that the "FORWARD" and "BACKWARD" directions are arbitrary. If they do not
match the actual direction of your vehicle or robot, simple swap the motor leads.
Also note that "RELEASE" simply cuts power to the motor. It does not apply any
braking.
void setSpeed(uint8_t);
The setSpeed() function controls the power level delivered to the motor. The speed
parameter is a value between 0 and 255.
Note that setSpeed just controls the power delivered to the motor. The actual speed
of the motor will depend on several factors, including: The motor, the power supply
and the load.
Adafruit_StepperMotor(void);
The constructor takes no arguments. The stepper motor is typically initialized by
assigning a stepper object retrieved from the shield as below:
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
The ste() function is synchronous and does not return until all steps are complete.
When complete the motor remains powered to apply "holding torque" to maintain
void setSpeed(uint16_t);
The setSpeed() function controls the speed of the stepper motor rotation. Speed is
specified in RPM.
Note: Calling step() with a step count of 1 is not the same as calling onestep(). The
step function has a delay based on the speed set in setSpeed(). onestep() has no
delay.
void release(void);
The release() function removes all power from the motor. Call this function to reduce
power requirements if holding torque is not required to maintain position.
Powering Motors
Motors need a lot of energy, especially cheap motors since they're less efficient.
Voltage requirements:
The first important thing to figure out what voltage the motor is going to use. If you're
lucky your motor came with some sort of specifications. Some small hobby motors are
only intended to run at 1.5V, but its just as common to have 6-12V motors. The motor
controllers on this shield are designed to run from 5V to 12V.
Current requirements:
The second thing to figure out is how much current your motor will need. The motor
driver chips that come with the kit are designed to provide up to 1.2 A per motor, with
3A peak current. Note that the 'peak' rating is for very brief peaks such as during
startup. Peak current levels can only be tolerated for a few milliseconds. If you will
be pushing the 1.2A continuous limit you'll probably want to put a heat-sink on the
motor driver, otherwise you will get thermal failure, possibly burning out the chip.
You can't run motors off of a 9V battery so don't waste your time/batteries!
Use a big Lead Acid or NiMH battery pack. Its also very much suggested that you set
up two power supplies (split supply) one for the Arduino and one for the motors. 99%
of 'weird motor problems' are due to noise on the power line from sharing power
supplies and/or not having a powerful enough supply! Even small DC motors can draw
up to 3 Amps when they stall.
Servos are powered off of the same regulated 5V that the Arduino uses. This is OK
for the small hobby servos suggested. Basically, power up your Arduino with the USB
port or DC barrel jack and you're good to go. If you want something beefier, cut the
trace going to the optional servo power terminal and wire up your own 5-6V supply!
The DC Jack on the Arduino has a protection diode so you won't be able to mess
things up too bad if you plug in the wrong kind of power. The terminal block has a
protection FET so you will not damage the arduino/shield if you wire up your battery
supply backwards, but it wont work either!
Note that you may have problems with Arduino resets if the battery supply is not able
to provide constant power, so it is not a suggested way of powering your motor
project. You cannot use a 9V battery for this, it must be 4 to 8 AA batteries or a single/
double lead acid battery pack.
Plug in the USB cable. Then connect the motor supply to the power terminal block on
the shield. Do not place the jumper on the shield.
This is a suggested method of powering your motor project as it has a split supply,
one power supply for logic, and one supply for motors
Plug in the supply for the Arduino into the DC jack, and connect the motor supply to
the power terminal block. Make sure the jumper is removed from the motor shield.
No matter what, if you want to use the DC motor/Stepper system the motor shield
LED should be lit indicating good motor power
Using RC Servos
The nice thing about using the onboard PWM is that its very precise and goes about
its business in the background. You can use the built in Servo library
Using the servos is easy, please read the official Arduino documentation for how to
use them and see the example Servo sketches in the IDE (https://adafru.it/aOD).
Powering Servos
Power for the Servos comes from the Arduino's on-board 5V regulator, powered
directly from the USB or DC power jack on the Arduino. If you need an external
supply, cut the 5v trace on the bottom of the board and connect a 5V or 6V DC
supply directly to the Opt Servo power input. Using an external supply is for
advanced users as you can accidentally destroy the servos by connecting a power
supply incorrectly!
When using external servo power, be careful not to let it short out against the
USB socket shell on the processor board. Insulate the top of the USB socket
with some electrical tape.
The motor shield can drive up to 4 DC motors bi-directionally. That means they can be
driven forwards and backwards. The speed can also be varied at 0.5% increments
using the high-quality built in PWM. This means the speed is very smooth and won't
vary!
Note that the H-bridge chip is not meant for driving continuous loads of 1.2A, so this is
for small motors. Check the datasheet for information about the motor to verify its OK!
Connecting DC Motors
To connect a motor, simply solder two wires to the terminals and then connect them
to either the M1, M2, M3, or M4. Then follow these steps in your sketch
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
with getMotor(port#). Port# is which port it is connected to. If you're using M1 its 1, M2
use 2, M3 use 3 and M4 use 4
AFMS.begin();
myMotor->setSpeed(150);
myMotor->run(FORWARD);
Stepper motors are great for (semi-)precise control, perfect for many robot and CNC
projects. This motor shield supports up to 2 stepper motors. The library works
identically for bi-polar and uni-polar motors
Before connecting a motor, be sure to check the motor specifications for compatibility
with the shield (https://adafru.it/r2d).
For unipolar motors: to connect up the stepper, first figure out which pins connected
to which coil, and which pins are the center taps. If its a 5-wire motor then there will
be 1 that is the center tap for both coils. Theres plenty of tutorials online on how to
reverse engineer the coils pinout. (https://adafru.it/aOO) The center taps should both
be connected together to the GND terminal on the motor shield output block. then
coil 1 should connect to one motor port (say M1 or M3) and coil 2 should connect to
the other motor port (M2 or M4).
For bipolar motors: its just like unipolar motors except theres no 5th wire to connect
to ground. The code is exactly the same.
Running a stepper is a little more intricate than running a DC motor but its still very
easy
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
Steps indicates how many steps per revolution the motor has. A 7.5 degree/step
motor has 360/7.5 = 48 steps.
Stepper# is which port it is connected to. If you're using M1 and M2, its port 1. If
you're using M3 and M4 indicate port 2
Theres tons of information about the pros and cons of these different stepping
methods in the resources page.
You can use whichever stepping method you want, changing it "on the fly" to as you
may want minimum power, more torque, or more precision.
By default, the motor will 'hold' the position after its done stepping. If you want to
release all the coils, so that it can spin freely, call release()
The stepping commands are 'blocking' and will return once the steps have finished.
Because the stepping commands 'block' - you have to instruct the Stepper motors
each time you want them to move. If you want to have more of a 'background task'
stepper control, check out the AccelStepper library (https://adafru.it/X1e) . There are
several AccelStepper examples included with the motor shield library.
First make sure you are running the latest version of Adafruit CircuitPython (https://
adafru.it/Amd) 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 (https://adafru.it/uap). Our CircuitPython starter guide has a great page on
how to install the library bundle (https://adafru.it/ABU).
If you choose, you can manually install the libraries individually on your board:
• adafruit_pca9685.mpy
• adafruit_bus_device/
• adafruit_register/
• adafruit_motor/
• adafruit_motorkit.mpy
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 (https://adafru.it/Awz) so you are at the
CircuitPython >>> prompt.
DC Motors
The four motor spots on the Shield are available as motor1 , motor2 , motor3 , and
motor4 .
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 (https://adafru.it/scl).
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 = 0.5
kit.motor1.throttle = -0.5
To let the motor coast and then spin freely set throttle to 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.
The most basic function (and the default) is to do one single coil step.
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.
◦ stepper.FORWARD (default)
◦ stepper.BACKWARD .
• release() which releases all the coils so the motor can free spin, and also
won't use any power
The function returns the current step 'position' in microsteps which can be handy to
understand how far the stepper has moved, or you can ignore the result.
kit.stepper1.onestep(direction=stepper.BACKWARD, style=stepper.DOUBLE)
You can even use a loop to continuously call onestep and move the stepper, for
example a loop of 200 microsteps forward for smooth movement:
for i in range(200):
kit.stepper1.onestep(style=stepper.MICROSTEP)
That's all there is to controlling a stepper motor from CircuitPython! Steppers are
handy motors for when you need smooth or precise control of something--for
example 3D printers and CNC machines use steppers to precisely move tools around
surfaces.
kit = MotorKit(i2c=board.I2C())
kit.motor1.throttle = 1.0
time.sleep(0.5)
kit.motor1.throttle = 0
kit = MotorKit(i2c=board.I2C())
for i in range(100):
kit.stepper1.onestep()
time.sleep(0.01)
Python Docs
Python Docs (https://adafru.it/DeE)
Stacking Shields
One of the cool things about this shield design is that it is possible to stack shields.
Every shield you stack can control another 2 steppers or 4 DC motors (or a mix of the
two)
You can stack up to 32 shields for a total of 64 steppers or 128 DC motors! Most
people will probably just stack two or maybe three but hey, you never know. (PS if you
drive 64 steppers from one of these shields send us a photo, OK?)
Note that stacking shields does not increase the servo connections - those are hard-
wired to the Arduino digital 9 & 10 pins. If you need to control a lot of servos, you can
use our 16-channel servo shield and stack it with this shield to add a crazy large # of
servos. (http://adafru.it/1411)
The only thing to watch for when stacking shields is every shield must have a unique
I2C address. The default address is 0x60. You can adjust the address of the shields to
range from 0x60 to 0x7F for a total of 32 unique addresses.
To program the address offset, use a drop of solder to bridge the corresponding
address jumper for each binary '1' in the address.
The right-most jumper is address bit #0, then to the left of that is address bit #1, etc
up to address bit #4
etc.
Note that address 0x70 is the "all call" address for the controller chip on the
shield. All boards will respond to address 0x70 - regardless of the address
jumper settings.
One motor shield is going to be called AFMSbot (bottom shield, so we remember) and
one is AFMStop (top shield) so we can keep them apart. When you create the shield
object, specify the address you set for it above.
// On the top shield, connect two steppers, each with 200 steps
Adafruit_StepperMotor *myStepper2 = AFMStop.getStepper(200, 1);
Adafruit_StepperMotor *myStepper3 = AFMStop.getStepper(200, 2);
// On the bottom shield connect a stepper to port M3/M4 with 200 steps
Adafruit_StepperMotor *myStepper1 = AFMSbot.getStepper(200, 2);
// And a DC Motor to port M1
Adafruit_DCMotor *myMotor1 = AFMSbot.getMotor(1);
You can request a stepper or DC motor from any port, just be sure to use the right
AFMS controller object when you call getMotor or getStepper!
Then, both shields must have begin called, before you use the motors connected
Resources
Motor ideas and tutorials
• Wikipedia has tons of information (https://adafru.it/aOF) on steppers
• Jones on stepper motor types (https://adafru.it/19oe)
• Jason on reverse engineering the stepper wire pinouts (https://adafru.it/aOI)