Introduction: Arduino Motor Shield Tutorial

Picture of Arduino Motor Shield Tutorial

The Arduino Motor Shield allows you to easily control motor direction and speed using an Arduino. By allowing you to simply address Arduino pins, it makes it very simple to incorporate a motor into your project. It also allows you to be able to power a motor with a separate power supply of up to 12v. Best of all, the shield is very easy to find. Aside from being sold a number of places online, they are now stocked by most Radioshack stores. For all of these reasons, the Arduino Motor Shield if a cool little to have in your arsenal for rapid prototyping, and general experimenting.

Step 1: Install

Picture of Install

The pins of the official Arduino motor shield will only align with Arduino Uno Rev. 3.

In order to make it work with older versions of the Arduino, you will need to trim a few pins off the motor shield. However, this is not, at all, recommended.

Insert the motor shield pins into the socket of the Arduino Uno.

Step 2: Shield Features

Picture of Shield Features
The motor shield has 2 channels, which allows for the control of two DC motors, or 1 stepper motor.

It also has 6 headers for the attachment of Tinkerkit inputs, outputs, and communication lines. The use of these pins is somewhat limited, and therefor not covered in this tutorial.

With an external power supply, the motor shield can safely supply up to 12V and 2A per motor channel (or 4A to a single channel).

There are pins on the Arduino that are always in use by the shield. By addressing these pins you can select a motor channel to initiate, specify the motor direction (polarity), set motor speed (PWM), stop and start the motor, and monitor the current absorption of each channel .

The pin breakdown is as follows:

FunctionChannel AChannel B
Direction Digital 12 Digital 13
Speed (PWM) Digital 3 Digital 11
Brake Digital 9 Digital 8
Current Sensing Analog 0 Analog 1


For more information about the technical specs, check out the motor shield's official page on the Arduino site.

Step 3: Program

Picture of Program
Plug the Arduino into your computer's USB port and open the Arduino development environment.

In order to get the board to do anything, you need to initialize the motor channel by toggling three parameters:
  1. First you need to set the motor direction (polarity of the power supply) by setting it either HIGH or LOW.
  2. Then you need to disengage the brake pin for the motor channel by setting it to LOW.
  3. Finally, to get the motor to start moving, you need to set the speed by sending a PWM command (analogWrite) to the appropriate pin.

If you do not do all three of these things, the motor will not turn on.

In the following steps are some common examples of common motor setups.

Step 4: One Motor

Picture of One Motor
To control a motor using the Arduino Motor Shield, first plug the motor's positive (red) wire into Channel A's + terminal on the motor shield, and the motor's ground (black) wire into Channel A's - terminal on the shield.

An external power supply is not always necessary, but it drastically improves the motor's performance. It is recommended that you always use one.

To connect your external power supply, connect the positive (red) wire from the power supply to the "Vin" terminal, and the ground (black) wire to the "GND" terminal.

Finally, upload the code to control the Motor Shield to the Arduino.

Here is the code for controlling one motor:

Step 5: Two Motors

Picture of Two Motors
Interfacing with two motors is pretty much the same as interfacing with one motor. Simply plug the motor into Channel B.

The only difference code-wise is that you need to engage a second channel to control the second motor.

Here is code for controlling two motors:

Step 6: Stepper Motor

Picture of Stepper Motor
There are a number of different types of stepper motors, but in this tutorial we will specifically be addressing bipolar stepper motors. Bipolar stepper motors typically have 4 pins, which correspond to two coils. To use a stepper, you need to power these two coils in phase with alternating polarity.

To figure out which two pins make up a single coil, insert an LED into any two pins on its socket and rotate the motor shaft. If the LED lights up, you found one coil. The other two pins should make up the other coil.

To make a bipolar motor spin:
  1. Power the first coil.
  2. Next power the second coil with reverse polarity.
  3. Then power the first coil with reverse polarity.
  4. Finally, power the second coil.
To reverse the motor direction of a bipolar stepper, simply reverse the polarity of the second coil.

Here is code to make the stepper spin in one direction:

Here is code to make the stepper spin in the opposite direction:

Comments

Carro33 (author)2017-10-31

hi every one.i need an info please.

i have an adafruit motor shield on top of arduino uno.every thing was working untill i connected two 2.7 amp motor on two channels of the motorshield ( adafruit ). one of the two chip smoked lol. the other one is working so i can still use two motor.my question is can i put an arduino motorshield on top of the adafruit motorshield so i will have my two bigger motor back ?thank you for any help and sorry about my english...

ugyeamit (author)2017-09-18

can i get some help with using that shield to connect to communicate with the Bluetooth channels... how should i write the code, im stuck in the middle of my project...

randofo (author)ugyeamit2017-09-18

This shield does not have bluetooth. Are you using something bluetooth enabled?

The best thing to do would be to figure out how to use this shield to control motors without bluetooth.

Then, find a tutorial for interfacing with your bluetooth module.

Then write a conditional (if -> then statement) which basically says, if my bluetooth receiver gets a command, then control the motor.

ugyeamit (author)randofo2017-09-18

Im using hc 06 with l298N motor driver ic... and i looked up to a similar project and even used his apps and all the connections with the same code , i am able to connect with the Bluetooth module but its not controlling the motor... what could be wrong... thanks for your support..

Kit Lorenz (author)2016-09-22

Can one also put another shield on top of the first one to support 4 dc motors?

ErikH76 (author)Kit Lorenz2017-08-15

This should be doable, as long as pin assignments do not interfere.

randofo (author)Kit Lorenz2016-09-22

I don't know if that would work.

However, if that worked at all, you would end up with two sets of synchronized motors. Basically, each channel on each shield would end up being controlled the same.

Kit Lorenz (author)randofo2016-09-23

ok, thanks!

aqeelkhan4772 (author)2016-11-16

hie everyone! i have a problem ... i need help in arduino project .i am using lcd shield and stepper motor at the same time but the problem is that these both device had the common pin pin 8 and 9 on arduino board can i change the pin of stepper motor to used other pin

mail id
aqeelkhan4772@gmail.com

ErikH76 (author)aqeelkhan47722017-08-15

Yes, declare them when you init them as different pins.

ulven_brud (author)2017-08-04

Hello :)
Thank you for the tutorial. I have a question, in the case of 2 separate power supplies for arduino and the shield, would one need to connect the 2 just by the pins you mentioned?

Thank you for any sort of information you can provide :)

ErikH76 (author)ulven_brud2017-08-15

IF the motors that you are powering need more than 9v then you should desolder and cut the trace on the VIN on the underside of the board, then power 12v to the VCC and GND on the board, then give NEG and POS to the motor. If under 9v then just supply 9v to the VCC and GND and it will power the arduino.

IF the motors need less than 9v, then just power the motor shield, it will supply power to the arduino.

cademis made it! (author)2017-07-02

Thanks for that tutorial which makes it possible to drive my stepper w/o any libraries. I re-use your program to control a linear drive and it works perfectly!

Tahalilm (author)2017-03-03

Why is it the digitalWrite is specifying 12, 9 and 3?

cyborg2006 made it! (author)2017-02-23

This is working. Thanks for this example.

Jamiever6 made it! (author)2016-12-29

Excellent tutorial, thank you. I just salvaged a stepper motor from an old scanner and few DC motors. Looking forward to use them in a new project.

adele14 (author)2016-10-04

Hi,

I am working on an arduino system using a stepper motor (4SM42HT38-1684A) and a motor shield. I wanted to make the motor turn in one direction, stop and then turn in the other directions. I used the two codes above in two loops but the motor is still going in the same direction (i am sure that the 2nd loop is running but it is still the same direction). Could you help me please ? Is there anything to adapt in theses codes to make them run together ?

Thanks a lot,

Regards,

Adele

msgat (author)2016-10-03

Hi
Thankyou for your tutorial.

I am trying to drive a stepper motor 42SH47 - M RS No 535-0401.
I have cut the vin link and supplied the motor separately.

Using the code to move in one direction , I find that the motor is oscillating rather than rotating in one direction only.

Do you have any suggestions as to what I might be doing wrong ?

Thanks for your time

regards

M

randofo (author)msgat2016-10-03

The motor does not have a V-in link. It should have 4 wires. These comprise two separate pairs of motor drive coils which need to be powered in sequence. Each pair needs to be correctly attached to each channel.

msgat (author)randofo2016-10-04

Hi

I decided to up the voltage to the motor.
When I got to 3.3 V it started to rotate correctly.
The manufacturer states 2.8V supply!!

I will contact the manufacturer to see if we can explain!.
Thanks again for your help and the article.

Regards

mark

Johnson Selva (author)2016-10-02

nice article man it helped me a lot.... sweet and short...

will you able to explain other pins too (OUT6 OUT5 etc.,)

HARI6 (author)2016-09-30

I HAVE A DOUBT .WHEN I CONNECT MY MOTORS TO CHANNEL 'B'.IT WORKS BUT MY ARDUINO BOARD BELOW ,THE LED WHICH IS CONNECTED TO ON BOARD PIN 13 ALSO GOES ON AND OFF DEPENDING ON MY MOTOR CONTROL.IS THIS A PROBLEM.HOW CAN I SPLVE IT.

Kit Lorenz (author)2016-09-23

Just one more question, servos will also work the same way right?

randofo (author)Kit Lorenz2016-09-26

No. Servos work differently than DC motors. They need a control signal.

bsolar (author)2016-08-13

Thanks for the help:) this is such a good tutorial! It really made it clear what you can do with this shield. I have been searching all over the internet and have only found incomplete tutorials. Typos aside, this is a great tutorial! (sorry for typos, I'm swedish...

AshwinT5 (author)2016-07-15

const int motorPin1 = A0;

const int motorPin2 = A3;

void setup()

{

pinMode(motorPin1, HIGH);

pinMode(motorPin2, LOW);

}

void loop()

{

analogWrite(motorPin1, 255);

analogWrite(motorPin2, 254);

}

can it work for full speed

tinyweasel (author)2016-07-10

Thanks for the Instructable! I noticed a little typo in your code, thought I'd let you know.

digitalWrite(9, HIGH); //Engage the Brake for Channel A
digitalWrite(9, HIGH); //Engage the Brake for Channel B

Should be pin 8 for Channel B. Just in case anyone copied and pasted and is getting weird results. :)

AgentMess (author)2016-05-06

Nice instructable. I really like the idea of connecting an LED to the stepper motor to figure out what pins belong to the same coil.

firephotoputer (author)2012-04-10

For the stepper motor code, what is the difference between the two versions that makes them spin the motor in opposite directions? The two code sets look the same to me, but since I am just starting with this, I can easily be overlooking the critical difference. And is "delaylegnth" supposed to be "delaylength" (as I said, just starting, so I am not familiar with the syntax quirks). Thanks!

randofo (author)firephotoputer2012-04-10

delaylegnth is a variable, so it was defined (misspelled) by me. If you correct one instance of the spelling, just be sure to correct all of them in the code.

The difference between the two is the direction of Channel B in each of the codes. Look at the difference of sequence for: digitalWrite(13, HIGH); //Sets direction of CH B

Ana CecíliaM (author)randofo2016-04-21

Hi there,

I know this is a long shot that you'll even see this but I tried both codes (for both directions) and they both spin the motor in the same direction...

I've been trying hard to understand what I should do, but there is little to no documentation supporting the motor shield. Help?! :)

Ofibro (author)randofo2015-06-09

hi great project

+_____+_________+_________+__________+__________+_________+

i have a problem with my external power supply

i tried everything, from disconnecting and connecting the bridge under the board right below the VIN pin i tried using a 9v jack i connected the 9v battery to the arduino's VIN pin directly.

============

NO MATTER WHAT I TRY IT ONLY WORKS WITH THE USB POWER SUPPLY

============

to make matters worse, i bought another one yet still no use i borrowed two of them from my friend yet STILL no use

============

im deeply in need of you

============

P.S i know how to program it really good my problem lies on the power supply

cheers

mwnllt

+___________+__________+___________+_____________+__________+______+

Ofibro (author)Ofibro2015-06-10

WOW,

just to the point of giving up I FINNALY got it.

all that it needed was amps.

I was using an lakline 9V battery which supplied 500mAh, which of course wasnt enough..

so i bought a couple of rechargeable batteries and VOLA it worked

keep in mind that those rechargeable batteries were 2200mAh @ 1.2 volts x5 so that was enough to get those motors moving!!

thanks anyways and great project.

cheers.

GabrielR28 (author)Ofibro2015-10-21

I do not speak English. friend you could upload some pictures of the connection to external source. as is the connection of the bridge. Thank you

SantitheGreat (author)Ofibro2015-09-20

u have to use a regular battery pack for the arduino itself, then an extra 9v battery.

firephotoputer (author)randofo2012-04-10

Now that you have pointed it out, the difference between the versions is obvious. Thank you!

DavidH469 (author)2016-04-09

Hey hey, try someone to use this motorshield with arduino duemilanove? If yes can you help me with it? I am beginner.

Thanks a lot!

kibble01 (author)2016-03-10

How would you go about using the code above with a switch to stop the stepper motor kind of like a endstop.

so far I have it stopping but it will stop after the loop is completed, is there a way to stop it right away?

cheers

Dave

YaddyVirus (author)2016-03-08

Will this work on the Arduino Mega 2560 R3? I want to use this in a obstacle avoiding robot.

And the link to the motor shield is broken

UtsavC (author)2016-02-22

I want my motor to move in all direction. and also want to stop and start it any time not fixed I am connecting arduino with bluetooth module so I want codes for 2motors



can any one help me

UtsavC (author)2016-02-22

I have ordered same motor driver shield for my project
I have a doubt that can we move 2 motors and 1 stepper sinultaneously???

BorisP12 (author)2016-02-07

Can you help me. i have an OSEPP Motor and Servo sheild that is Arduino compatiable but i have no idea how to work it...any thoughts?

planetoid127 (author)2016-02-02

Thanks for the tutorial, I made it and it was great! I also like the way you put in the code meaning and code.

Alok Shaw (author)2016-01-05

i am using arduino adafruit motor control shield V1 ,for controlling 4 12v dc motors....but i am confused about the power supply for the motor shield as i read on the manual of adafruit fruit that 9v supply will not work(why no idea)!! what sort of power supply other than lipo can i supply?? and how much should i supply..for the 4 dc geared motors(12V,300rpm)

darkstar1899 (author)Alok Shaw2016-01-05

A different power supply and some relays might prove useful. Note the relays are to be controlled by the board but the 12 V power source should not be grounded to the board itself.

LawrenceM8 (author)2015-09-07

I followed the instructions, and it worked fine. When I installed a remote control, the receiver did not pick up any signal or pick up wrong signal when the motor is spinning. When the motor is not spinning, it could pick up signal and everything worked. This is the same effect as of before installing the motorshield. Does anyone else have this problem?

Alok Shaw (author)LawrenceM82016-01-05

to which pin , you connected the IR reciever??

AdityaP25 (author)2015-10-11

Harshvardhan and Javier I think both of your code will not work as in both of your codes you have chosen pins 2,3,4,5 which actually are not PWM pins so your code will not work as analogWrite function only works with the PWM pins

Harshvardhan Singh (author)2015-09-30

is this code ok to drive 2 motors front right back and left.

void setup() {
pinMode (2,OUTPUT);//RIGHT MOTOR
pinMode (3, OUTPUT);//RIGHT MOTOR
pinMode (4,OUTPUT);//LEFT MOTOR
pinMode (5, OUTPUT);//LEFT MOTOR
}

void loop() {
//front
digitalWrite (2, LOW);//RIGHT MOTOR
analogWrite (3, 250);//RIGHT MOTOR
digitalWrite (4, LOW);//LEFT MOTOR
analogWrite (5, 250);//LEFT MOTOR
//left
digitalWrite (2, LOW);//RIGHT MOTOR
analogWrite (3, 150);//RIGHT MOTOR
digitalWrite (4, HIGH);//LEFT MOTOR
analogWrite (5, 105);//LEFT MOTOR
//back
digitalWrite (2, HIGH);//RIGHT MOTOR
analogWrite (3, 125);//RIGHT MOTOR
digitalWrite (4, HIGH);//LEFT MOTOR
analogWrite (5, 125);//LEFT MOTOR
//right
digitalWrite (2, HIGH);//RIGHT MOTOR
analogWrite (3, 105);//RIGHT MOTOR
digitalWrite (4, LOW);//LEFT MOTOR
analogWrite (5, 150);//LEFT MOTOR
}

JavierO5 made it! (author)2015-09-30

Hi everybody,

I have a Arduino uno connect to a Arduino Motor Shield r3, and i am trying to control a stepper motor from Maxuino (this one http://www.gotronic.fr/pj-784.pdf).

I have been following the Chris Coleman tutorial on vimeo but i might have a problem with the setting of the pin.

From the arduino software, this code works:

int delaylegnth = 30;

void setup() {

//establish motor direction toggle pins

pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???

pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???

//establish motor brake pins

pinMode(9, OUTPUT); //brake (disable) CH A

pinMode(8, OUTPUT); //brake (disable) CH B

}

void loop(){

digitalWrite(9, LOW); //ENABLE CH A

digitalWrite(8, HIGH); //DISABLE CH B

digitalWrite(12, HIGH); //Sets direction of CH A

analogWrite(3, 255); //Moves CH A

delay(delaylegnth);

digitalWrite(9, HIGH); //DISABLE CH A

digitalWrite(8, LOW); //ENABLE CH B

digitalWrite(13, LOW); //Sets direction of CH B

analogWrite(11, 255); //Moves CH B

delay(delaylegnth);

digitalWrite(9, LOW); //ENABLE CH A

digitalWrite(8, HIGH); //DISABLE CH B

digitalWrite(12, LOW); //Sets direction of CH A

analogWrite(3, 255); //Moves CH A

delay(delaylegnth);

digitalWrite(9, HIGH); //DISABLE CH A

digitalWrite(8, LOW); //ENABLE CH B

digitalWrite(13, HIGH); //Sets direction of CH B

analogWrite(11, 255); //Moves CH B

delay(delaylegnth);

}

But in Maxuino it doesn't works. The lights on the motor shield board arent moving the same way, as if the pins wasn't setting up correctly, but i can't found how to solve it.

It is kind of an emergency, i'm trying to make it work for friday. If someone has any idea that could help me to understand whats going on, i will be glad to read it. Thanks in advance!

Pierre

About This Instructable

1,026,581views

790favorites

License:

Bio: My name is Randy and I founded the Instructables Design Studio. I'm also the author of the books 'Simple Bots,' and '62 Projects to ... More »
More by randofo:Large Motor ProjectsRemove a Wheelchair Motor BrakeBuild a Robot
Add instructable to: