Arduino Motor Shield Tutorial

1,216,431

854

204

Introduction: Arduino Motor Shield Tutorial

About: My name is Randy and I am a Community Manager in these here parts. In a previous life I had founded and run the Instructables Design Studio (RIP) @ Autodesk's Pier 9 Technology Center. I'm also the author of …

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. For all of these reasons, the Arduino Motor Shield if a cool little to have in your arsenal for rapid prototyping, and general experimenting.

(Note that some of the links on this page are affiliate links. This does not change the cost of the item for you. I reinvest whatever proceeds I receive into making new projects. If you would like any suggestions for alternative suppliers, please let me know.)

Step 1: 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

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

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

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:
<pre>/*************************************************************
Motor Shield 1-Channel DC Motor Demo
by Randy Sarafan

For more information see:
https://www.instructables.com/id/Arduino-Motor-Shield-Tutorial/

*************************************************************/

void setup() {
  
  //Setup Channel A
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin
  
}

void loop(){
  
  //forward @ full speed
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 255);   //Spins the motor on Channel A at full speed
  
  delay(3000);
  
  digitalWrite(9, HIGH); //Eengage the Brake for Channel A

  delay(1000);
  
  //backward @ half speed
  digitalWrite(12, LOW); //Establishes backward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 123);   //Spins the motor on Channel A at half speed
  
  delay(3000);
  
  digitalWrite(9, HIGH); //Eengage the Brake for Channel A
  
  delay(1000);
  
}

Step 5: 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:
<pre>/*************************************************************
Motor Shield 2-Channel DC Motor Demo
by Randy Sarafan

For more information see:
https://www.instructables.com/id/Arduino-Motor-Shield-Tutorial/

*************************************************************/

void setup() {
  
  //Setup Channel A
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin

  //Setup Channel B
  pinMode(13, OUTPUT); //Initiates Motor Channel A pin
  pinMode(8, OUTPUT);  //Initiates Brake Channel A pin
  
}

void loop(){

  
  //Motor A forward @ full speed
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 255);   //Spins the motor on Channel A at full speed

  //Motor B backward @ half speed
  digitalWrite(13, LOW);  //Establishes backward direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 123);    //Spins the motor on Channel B at half speed

  
  delay(3000);

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


  delay(1000);
  
  
  //Motor A forward @ full speed
  digitalWrite(12, LOW);  //Establishes backward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 123);    //Spins the motor on Channel A at half speed
  
  //Motor B forward @ full speed
  digitalWrite(13, HIGH); //Establishes forward direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 255);   //Spins the motor on Channel B at full speed
  
  
  delay(3000);
  
  
  digitalWrite(9, HIGH);  //Engage the Brake for Channel A
  digitalWrite(9, HIGH);  //Engage the Brake for Channel B
  
  
  delay(1000);
  
}

Step 6: 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:
<pre>/*************************************************************
Motor Shield Stepper Demo
by Randy Sarafan

For more information see:
<a href="https://www.instructables.com/id/Arduino-Motor-Shield-Tutorial/">
https://www.instructables.com/id/Arduino-Motor-Sh...</a>

*************************************************************/

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);

}

Here is code to make the stepper spin in the opposite direction:
<pre>/*************************************************************
Motor Shield Stepper Demo
by Randy Sarafan

For more information see:
<a href="https://www.instructables.com/id/Arduino-Motor-Shield-Tutorial/">
https://www.instructables.com/id/Arduino-Motor-Sh...</a>

*************************************************************/

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, HIGH);   //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, LOW);   //Sets direction of CH B
  analogWrite(11, 255);   //Moves CH B
  
  delay(delaylegnth);

}

Did you find this useful, fun, or entertaining?
Follow @madeineuphoria to see my latest projects.

Arduino Challenge

Participated in the
Arduino Challenge

7 People Made This Project!

Recommendations

  • Micro:bit Contest

    Micro:bit Contest
  • Sewing Challenge

    Sewing Challenge
  • Organization Contest

    Organization Contest

204 Comments

0
Anicet
Anicet

5 weeks ago

Here is the code to make the stepper spin in the anti-clockwise direction. The two codes given above are the same and therefore both make the stepper spin clockwise.

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(8, LOW); //ENABLE CH A
digitalWrite(9, HIGH); //DISABLE CH B

digitalWrite(12, HIGH); //Sets direction of CH A
analogWrite(3, 255); //Moves CH A

delay(delaylegnth);

digitalWrite(8, HIGH); //DISABLE CH A
digitalWrite(9, LOW); //ENABLE CH B

digitalWrite(13, LOW); //Sets direction of CH B
analogWrite(11, 255); //Moves CH B

delay(delaylegnth);

digitalWrite(8, LOW); //ENABLE CH A
digitalWrite(9, HIGH); //DISABLE CH B

digitalWrite(12, LOW); //Sets direction of CH A
analogWrite(3, 255); //Moves CH A

delay(delaylegnth);

digitalWrite(8, HIGH); //DISABLE CH A
digitalWrite(9, LOW); //ENABLE CH B

digitalWrite(13, HIGH); //Sets direction of CH B
analogWrite(11, 255); //Moves CH B

delay(delaylegnth);
}

0
hroc651
hroc651

Question 2 months ago on Step 1

Where do i find the library for the ARDUINO MOTOR SHIELD. i tried copy and pasting the 2 motor code to the Arduino sketch . But i keep getting an error. I figure i need to install the Library first.

0
robinhut1957
robinhut1957

Question 9 months ago

Hallo Freaks


Da ich unter Legasthenie leide hoffe ich, dass ihr ein
gewisses Verständnis aufbringen könnt für meine Fragestellung. Ich versuche,
dass Motor Shield REV3 in gang zu bringen. Leider ist dies mir bis heute nicht
gelungen. Nun habe ich eine Frage, die vielleicht was lustig erscheint. Könnte
mir jemand ein kurzes Programm schreiben? Das Programm sollte beinhalten das
mein Steppmotor mit 4 Anschlüssen Vor- und rückwärts laufen sollte sowie Geschwindigkeit
und mehr braucht es eigentlich nicht. Die Geschwindigkeitsparameter kann ich dann
selbst einstellen. Ich hoffe, dass jemand für mein Anliegen Verständnis
aufbringen kann.





Mit freundlichen Grüssen


Robert

0
MdaraV
MdaraV

Question 10 months ago

Hie how are you :-) Randofo
hallo,thanks a lot for the article it really helped me.,but i have a different situation here .i have a nodemcu ESP8266 and BMT DUAL DC MOTOR SHIELD L298P.Iam builting a rc car with 1 motor for the rear wheels forward and reverse and 1 servor to control the steering..the motor driver shield i have is compatible with Arduino uno,i am finding difficulties in connecting it to nodemcu.
any help i will appreciate
thank you.

0
mrhopeedu
mrhopeedu

1 year ago

Hi Randofo,

Thanks for the tutotial, really good info to understand motor shield.

Does the motor shield control the two DC motors at synchronized speed?
What I mean is if u attach the motors to robot as wheels and let it run on the floor does it run in straight line?

I worked on a motor controller L298N but struggled to get the 2 DC motor to revolve in sync. My understanding is that due to friction 2 DC motor will never revolve the same rate so I wonder if this motor shield can be the answer to my issue.

0
dudes
dudes

Reply 1 year ago

No, it can drive the two motors with the same voltage and PWM duty cycle, but the nature of DC motors is that they don't all spin the exact same speed when given the exact same power, even if they're the same model. The only way to truly get two DC motors synchronized is to have some sort of feedback from the motors and actively adjust the individual speeds to match them.
If you need them to be synchronized, the easiest method would be to use continuous rotation servos, which have a feedback system built in to them. Or use a stepper motor, which operates via discrete steps rather than rotation speed.

0
mrhopeedu
mrhopeedu

Reply 1 year ago

hey buddy,

I did think about stepper motor but someone mentioned the robot will move very slow with stepper motor but thanks for the detailed feedback, that clarifies the mystery.

0
mignatovsky
mignatovsky

Question 1 year ago

Hello,
I have ARDUINO MOTOR SHIELD REV3. My goal is to drive 2 dc motors and operate 6 solenoids (model train turnout motors).
1. Can one motor shield board handle all this?
2. I can't find any concrete examples on how to connect and how to operate solenoids with this motor shield.
Any help is appreciated. Michael

0
dudes
dudes

Answer 1 year ago

Not by itself it can't. This shield has two individual outputs, so it could drive two motors, two solenoids, or one stepper motor at a time. Since solenoids don't need a full H-bridge to operate, you could simply use six transistors or the ULN2004, which is a 7-transistor array IC.
So you could use this shield for the two motors and the ULN2004 for the six solenoids.
As for how to operate solenoids with this shield, it is absolutely possible, you simply connect and operate them as if they were regular DC motors and they will click back and forth rather than spin.

0
tvan doveren
tvan doveren

1 year ago

Hi there,
Have you ever felt a/o measured the temperature of the chip on the Arduino Motor shield?
How hot can it become.before it fails? We went to 120 C and unplugged the shield from the power supply. Are we too anxious? We use 9V power supply.
greetings, Tom
.

0
randofo
randofo

Reply 1 year ago

No, I have not measured the temperature, but it can get really hot to the touch. One thing I have found sometimes helps is disconnecting the sheilds Vin pin from the Arduino (assuming the Arduino is also powered separately by USB). I am not 100% sure why this helps, but it sometimes does.

If you have connected very large motors that draw more current than each channel of the board can handle that might be causing an overheating problem. Another thing is if you are doing a lot of frequent starting, stopping or reversing, that might contribute to excessive heat.

Motor controllers are meant to handle some degree of heat, but if it starts to smell, smoke, or discolor, you want to disconnect it immediately.

0
FIFI38
FIFI38

Question 1 year ago on Step 6

Bonjour,
I bought an Arduino motor shield (new by a resseler) and as soon as I plug it to any Arduino board (uno or mega!) the leds of the board stop to blink and the motor shield processor becomes very hot!
I reset many times both boards and it's Always the same problem!!
What can I do ?
Thanks
Philippe

0
randofo
randofo

Answer 1 year ago

I don't know. Are all of the pins inserted correctly? Are you using an externals power supply? If so, what is the voltage? It sounds like you are somehow shorting a connection on the Arduino board. Does it have this result if nothing is connected to the sheild?

0
superfliege90
superfliege90

1 year ago

Found a bug in "Here is code for controlling two motors:"

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

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

correct:

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

can you fix this?

btw good tutorial.



0
Tahalilm
Tahalilm

4 years ago

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

0
JairoM1
JairoM1

Reply 2 years ago

Pin 3 is PWM for Channel A.

0
industrypam
industrypam

Question 2 years ago

Hi. I'm not sure about how to code something. In fact my project is simple, i have two push buttons for one motor. One of these two button should order the forward direction and the other one the backward. I'm a beginner in code so please could you help me to solve this problem?

0
randofo
randofo

Answer 2 years ago

I'll give you some pseudo code:

(Code for setting up button pins here)

If(button1pressed == true){
stop motor function

//This pseudo code is assuming the motor is already spinning in the opposite direction once you pressed the button to change its rotation. You want to always first stop the motor and let the energy dissipate rather than suddenly reverse direction and have that energy from the sudden direction change go back into the circuit

delay a couple milliseconds

//wait a few milliseconds for the motor to de-energize before engaging the motor again

motor forward code
}

if(button2pressed == true){
stop motor function
delay a couple milliseconds
motor backward code
}

0
nobel522
nobel522

2 years ago

Hi! am very new to Arduino! I am currently working on a project and thinking of external power supply. But as seen on some online videos i cannot see any jumper on L298p shield. Could you please help me

0
randofo
randofo

Reply 2 years ago

There should be two screw terminals labeled Vin and Ground. This is external power.