Arduino Motor Shield Tutorial

1,079,906

810

179

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 ...

Intro: 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. 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

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:
https://www.instructables.com/id/Arduino-Motor-Shield-Tutorial/

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

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:
https://www.instructables.com/id/Arduino-Motor-Shield-Tutorial/

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

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

}

5 People Made This Project!

Recommendations

  • Furniture Contest 2018

    Furniture Contest 2018
  • Tiny Home Contest

    Tiny Home Contest
  • Metalworking Contest

    Metalworking Contest

179 Discussions

0
None
DanielB857

Question 2 months ago on Step 4

How can I set, a time for the DC motor?
For example I want to open a door it take 10sec before the door is complete open, and what the DC motor to stop, wait 5min and the revers it down agin

2 more answers
0
None
randofoDanielB857

Answer 2 months ago

They delay function is in milliseconds. That means for every 1000 increments, the program will wait one second. For instance delay(2000); is the same as telling the code to wait for 2 seconds.

You would engage the function to turn on the motor, delay for 10 seconds, engage the stop function on the motor, create a delay for 300 seconds (5 minutes) and then reverse the motor (and then stop it again).

0
None
DanielB857randofo

Reply 2 months ago

can you combine with a clock, at 14:00, the engines will start spinning in 30sec, then close.

0
None
Manikantas3

Question 2 months ago on Step 6

what is the supply voltage required to run 4 DC motors with motor sheild and max voltage rating??

1 more answer
0
None
randofoManikantas3

Answer 2 months ago

It depends on the voltage requirements of the motor. Different motors have different voltage ratings.

However, the shield has a maximum voltage rating, and I would not put more than 12V through the shield. Although, your motors might require less voltage than that.

Supplying the right amount of current is important. You would want to make sure that yours motors do not draw more current than the shield is able to handle. I would not exceed more than about 2 amps per channel.

Also, this shield is a dual H-bridge, meaning it can only control 2 motors.

0
None
rburgos05

3 months ago

In case of interest to anyone, here is a truth table of the Arduino Motor Shield Rev3, channel A. The first row is the default value before you send anything to the motor shield.

DIRA BRAKEAPWMA PWMA A+A-

LOW LOW 0 1.4V 1.4V

LOW LOW 255 0V VMOT

LOW HIGH 0 1.4V 1.4V

LOW HIGH 255 0V 0V

HIGH LOW 0 1.4V 1.4V

HIGH LOW 255 VMOT 0V

HIGH HIGH 0 1.4V 1.4V

HIGH HIGH 255 VMOT VMOT

0
None
SavioR5

Question 5 months ago on Introduction

Hi all, I have a Arduino Mega and a Motor shield controlling a DCC command station for a model railroad. A-,A+,B- and B+ are used to send signals to the track. I need to also control 3 turnouts using 3 micro servors. Can this be done using the existing hardware.

0
None
SteveS162

Question 6 months ago

Hi I'm fairly new to this (trying to teach myself after brian probs). Is there a way to set both motors going at the same time? I want to use it for a car, when I set it away there is quite a delay from motor1 starting till motor2 starts. Many thanks for any advice and help.

1 more answer
0
None
randofoSteveS162

Answer 6 months ago

In the code above they should both engage at the same time, but at different speeds. If one is taking a little while to engage, maybe it is being slightly underpowered? I am not certain what the error is without having more information about your setup.

0
None
yamanss01

Question 7 months ago on Step 4

how does the direction of motor will change at runtime of robot . how can we manually control the direction of rotation . by using a 3 way switch then how to connect a 3 way switch to a motor shield.

1 more answer
0
None
randofoyamanss01

Answer 7 months ago

You would need to connect an input to the Arduino and then monitor the state of the input.

If the state of the input changes, you can use the Arduino to tell the motors to do something else.

I recommend reading the Arduino Class to learn more about Arduino inputs:
https://www.instructables.com/class/Arduino-Class/

0
None
randofoMusab Mahmood

Answer 7 months ago

This shield does not have bluetooth capability. You would need to get a bluetooth module and connect it to the unused pins on the shield.

You would then interpret the incoming data from the bluetooth module, and conditional statements (such as if > then) to interpret these commands and control the motor.

For instance:
If the bluetooth module sends the command forward, then turn both motors on to spin forward.

0
None
Carro33

10 months ago

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...

0
None
ugyeamit

1 year ago

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...

2 replies
0
None
randofougyeamit

Reply 1 year ago

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.

0
None
ugyeamitrandofo

Reply 1 year ago

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..

0
None
Kit Lorenz

2 years ago

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

2 replies
0
None
ErikH76Kit Lorenz

Reply 1 year ago

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

0
None
randofoKit Lorenz

Reply 2 years ago

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.