Adafruit Motor Shield v2 For Arduino
Adafruit Motor Shield v2 For Arduino
Guide Contents
Guide Contents
Overview
FAQ
11
11
14
18
Install Software
22
22
DC Motor
22
24
Powering Motors
27
Voltage requirements:
27
Current requirements:
27
27
27
If you would like to have a single DC power supply for the Arduino and motors
28
If you would like to have the Arduino powered off of USB and the motors powered off of
a DC power supply
28
If you would like to have 2 separate DC power supplies for the Arduino and motors. 29
Using RC Servos
30
Powering Servos
30
Using DC Motors
32
Connecting DC Motors
32
32
33
33
33
33
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 2 of 46
33
34
35
35
35
35
35
Stacking Shields
37
38
39
Resources
41
41
Library Reference
42
class Adafruit_MotorShield;
42
42
42
43
43
void setPWM(uint8_t pin, uint16_t val); void setPin(uint8_t pin, boolean val);
43
class Adafruit_DCMotor
43
Adafruit_DCMotor(void);
44
void run(uint8_t);
44
void setSpeed(uint8_t);
44
class Adafruit_StepperMotor
45
Adafruit_StepperMotor(void);
45
45
void setSpeed(uint16_t);
46
46
void release(void);
46
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 3 of 46
Overview
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 driver: with 1.2A
per channel and 3A peak current capability. 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
pins (SDA & SCL) 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.
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 4 of 46
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 5 of 46
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 with the Due!
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 6 of 46
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
Can I connect more motors?
Yes, by stacking shields! Every shield you stack on will add 4 DC motors or 2 stepper
motors (or 1 more stepper and 2 more DC motors).
You will not gain more servo connections as the servo contacts go to pin #9 and #10 on
the Arduino.
What if I also need some more servos?
Check out our lovely servo shield, also stackable with this motor shield and adds 16 freerunning servos per shield http://learn.adafruit.com/adafruit-16-channel-pwm-slash-servoshield (http://adafru.it/ciQ)
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!
I get the following error trying to run the example code: "error: Adafruit_MotorShield.h: No
such file or directory...."
Make sure you have installed the Adafruit_MotorShield library
How do I install the library?
Check the tutorial page on the subject here http://learn.adafruit.com/adafruit-motorshield-v2-for-arduino/install-software (http://adafru.it/ciO)
HELP! My motor doesnt work! - HELP! My motor doesnt work!...But the servos work FINE!
Is the power LED lit? The Stepper and DC motor connections will not work if the onboard
green Power LED is not lit brightly!
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 7 of 46
You must connect 5-12VDC power to the shield through the POWER terminal blocks or
through the DC barrel jack on the Arduino and VIN jumper.
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 8 of 46
I'm using a 4WD robot platform and I can't get anything to work.
The motors used in the 4WD robot platforms from some distributors have a lot of "brush
noise". This feeds back into the Arduino circuitry and causes unstable operation. This
problem can be solved by soldering 3 noise suppression capacitors to the motor. 1
between the motor terminals, and one from each terminal to the motor casing.
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 9 of 46
hardware/libraries/wire/utility/twi.h.
Find the line with: "#define TWI_FREQ 100000L"
and change it to "#define TWI_FREQ 400000L"
Or, you can add the following code to your setup() function:
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 10 of 46
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 11 of 46
Break apart the 0.1" header into 6, 8 and/or 10pin long pieces and slip the long ends into the
headers of your Arduino
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 12 of 46
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 13 of 46
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 14 of 46
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 15 of 46
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 16 of 46
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 17 of 46
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 18 of 46
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 19 of 46
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 20 of 46
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 21 of 46
Install Software
To use the shield on an Arduino, you'll need to install the Adafruit Motorshield v2 library. This
library is no t co mpatible with the o lder AF_Mo to r 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!
Start by downloading the zip of the library from the Github repository (http://adafru.it/ciN).
You can also just click the button below
AccelStepper Library
http://adafru.it/dh1
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 22 of 46
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 o r 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 remo ve the VIN jumper
If the Green LED isn't lit up brightless do no t co ntinue - you must power it via the VIN
jumper o r the terminal block
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 23 of 46
Once you have verified the motor is connected properly and you have the power LED lit up
brightly, we can upload our code.
In the IDE, load File->Examples->Adafruit_Mo to rShield->DCMo to rTest
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
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 24 of 46
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
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 o r 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 remo ve the VIN jumper
If the Green LED isn't lit up brightless do no t co ntinue - you must power it via the VIN
jumper o r the terminal block
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 25 of 46
Once you have verified the motor is connected properly and you have the power LED lit up
brightly, we can upload our code.
In the IDE, load File->Examples->Adafruit_Mo to rShield->StepperTest
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.
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 26 of 46
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.
MOST 1.5-3V MOTORS WILL NOT WORK
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 once you head towards 2A you'll probably want to put a heat-sink on the
motor driver, otherwise you will get thermal failure, possibly burning out the chip.
Yo u can't run mo to rs o ff o f a 9V battery so do n't waste yo ur 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% o f 'weird
mo to r pro blems' 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.
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 27 of 46
12VDC.
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 as a protection FET so you
will not damage the arduino/shield if you wire up your battery supply backwards, but it wont
work either!
Here's how it works:
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 28 of 46
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 29 of 46
Using RC Servos
Hobby servos are the easiest way to get going with motor control. They have a 3-pin 0.1"
female header connection with +5V, ground and signal inputs. The motor shield simply
brings out the PWM output lines from Arduino pins 9 and 10 to two 3-pin headers so that its
easy to plug in and go. They can take a lot of power so a 9V battery wont last more than a
few minutes!
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 (http://adafru.it/aOD).
Powering Servos
Po wer fo r the Servo s co mes fro m the Arduino 's o n-bo ard 5V regulato r,
po wered directly fro m the USB o r DC po wer jack o n 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
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 30 of 46
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.
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 31 of 46
Using DC Motors
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
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 32 of 46
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 33 of 46
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
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. (http://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
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 34 of 46
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 35 of 46
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 36 of 46
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 16channel servo shield and stack it with this shield to add a crazy large # of
servos. (http://adafru.it/1411)
Stacking shields is very easy. Each shield you want to stack on top of must have stacking
headers installed. Check our instructions for how to do so. (http://adafru.it/ciP) The top
shield does not have to have stacking headers unless you eventually want to put something
on top of it.
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 37 of 46
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 0x80 for a total of 32 unique addresses.
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 38 of 46
Board 3: Address = 0x63 Offset = binary 0011 (bridge A0 & A1, two rightmost jumpers)
Board 4: Address = 0x64 Offset = binary 0100 (bridge A2, middle jumper)
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.
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 39 of 46
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 40 of 46
Resources
Motor ideas and tutorials
Wikipedia has tons of information (http://adafru.it/aOF) on steppers
Jones on stepper motor types (http://adafru.it/aOH)
Jason on reverse engineering the stepper wire pinouts (http://adafru.it/aOI)
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 41 of 46
Library Reference
class Adafruit_MotorShield;
The Adafruit_MotorShield class represents a motor shield and must be instantiated before
any DCMotors or StepperMotors can be used. You will need to declare one
Adafruit_MotorShield for each shield in your system.
Adafruit Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 42 of 46
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 Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 43 of 46
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();
// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
// You can also make another motor on port M2
Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);
void run(uint8_t);
The run() function controls the motor state. The parameter can have one of 3 values:
FORWARD - Rotate in a forward direction
REVERSE - Rotate in the reverse direction
RELEASE - Stop rotation
Note that the "FORWARD" and "REVERSE" 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 Industries
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 44 of 46
class Adafruit_StepperMotor
The Adafruit_StepperMotor class represents a stepper motor attached to the shield. You
must declare an Adafruit_StepperMotor for each stepper motor in your system.
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);
https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino
Page 45 of 46
void setSpeed(uint16_t);
The setSpeed() function controls the speed of the stepper motor rotation. Speed is
specified in RPM.
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.
Adafruit Industries
Page 46 of 46