Arduino Motor Shield Tutorial by randofo
Featured
main1.jpg
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.
 
Remove these adsRemove these ads by Signing Up

Step 1: Install

1B.jpg
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.
1-40 of 70Next »
chris911 says: Apr 27, 2013. 2:47 PM
How do i write the code to make a momentary switch, start the motor. Without having to hold down the switch?
THE STIG37 says: Jul 9, 2012. 10:29 AM
i am using the code given with dougstrickland's correction and i try to upload it and the motor isnt turning and i get an orange line of code at the bottom that says
avrdude: stk500_getsync(): not in sync: resp=0x00
ringai says: Aug 11, 2012. 4:31 PM
I think that indicates an issue with the bootloader accepting your code. Can you run other code on it? Say, the blink code?

dman3 says: Apr 10, 2013. 11:41 AM
what if the wheels aren't turning?
dman3 says: Apr 10, 2013. 11:09 AM
i uploaded this code but the wheels aren't turning...
any help?
lumia_berkek says: Mar 22, 2013. 8:07 PM
thanks it was great for me , it works boss
AnAnalyst says: Mar 20, 2013. 8:35 PM
I could not follow your code so I wrote my own.  Here it is:

/*

This software will step (as opposed to wave or continuous drive) a two phase
4 wire stepper motor using the Arduino Mega 2560 and the Motor Shield Rev. 3

Of course, most of the examples I saw for stepper motor circuits didn't use
the Mega 2560.  This didn't prove to be an problem because the
Motor Shield Rev 3. is compatible with the Mega 2560. Just got to be careful
with the pin alignment when you stack the two boards.

While you can drive a stepper motor using the Motor Shield, trying to
understand it can be a bit difficult since the references on the schematic
have to do with driving a regular motor, not  a stepper motor.
So, to make the code more understandable, I redefined or relabeled the Pin Outs.

Note that Digital Out Pin 12 on the Shield corresponds to PWM 12 on
the Mega 2560 and so on.  If you are looking at the PIN Out for the CPU,
this corresponds to PB6.

As you probably already know, to energize a coil, you have to
connect one end to a positive voltage and the other end to ground. 
This way, current will flow from Positive to Ground. You can also
cause current to flow in the opposite direction by changing the voltages
in the other direction (Ground one end and apply a Positive voltage to the other).

If you are new to Adruino programming, make sure you use the exact case as
shown.  For example, Output is not the same as OUTPUT.
Also, don't forget the semicolon on the end of a program statement.

*/

/* There are many different ways(algorithms) that you use to drive
   your stepper motor.  Some provide more torque, some provide greater
   power savings.  The method you chose will depend on your
   over all design goals.
  
  
   The algorithm I coded up is know as the Full Step, Two Phase On.
   This implies that booth phases will be powered or energized at the
   same time. It has only four states per step so it's probably the
   easiest to code up.
     
    Sate        A1    B1   A2    B2
      1             H     H     L      L
      2             L      H    H      L
      3             L      L     H      H
      4             H     L     L       H

  
     
    
   If you execute states 1 - 4, it will turn clockwise.
   If you execute states 4 - 1, it will turn counter clockwise.
  
     
*/

// Redfine Pin Outs

//This is used to enable channel one on the driver chip.
//This corresponds to the PWMA on the driver schematic.
const int EnableOne = 3;

//This is used to enable channel two on the driver chip.
//This corresponds to PWMB on the driver schematic.
const int EnableTwo = 11;



//This corresponds to DIRA (referenced in two places) on the driver schematic.
const int CoilOneSideA = 12;

//Note that we are not going to define anything for the CoilOneSideB. The reason
//why is because it is controlled by the value at IN2 on the driver chip.
//The schematic shows it's connected to the output of a NAND gate.  The symbol is
//incorrect, it is an Exclusive Nor Gate.  The truth table for it is:
//   
//               X   Y   F
//               0   0   1      ******
//               0   1   0
//               1   0   0      ******
//               1   1   1
//
//The inputs to this gate are shown to be DIRA and BRAKE-A_DISABLE.
//If we set Break-A_Disable to a low or logic zero, the ouput(*) will be the opposite
//the other input, DIRA.  This is just what you need, a way to Invert DIRA.
//So now all you need to do is set the value for DIRA (CoilOneSideA) and the other
//side of the coil will always be at the opposite value.
//Note: If your design just used a driver chip, you  would need to drive or set the other
//side of the coil independently.
const int BreakADisable = 9;


//This corresponds to DIRB (Referenced in two places) on the schematic.
//Note that we are not going to define anything for the CoilTwoSideB for the same
//reason as discussed as above.
const int CoilTwoSideA = 13;

const int BreakBDisable = 8;

//This corresponds ro the SNSA on the schematic. If you use it, understand that it's analog.
//Right now it's not used.
const int CurrentSenseA = 0;

//This corresponds ro the SNSB on the schematic. If you use it, understand that it's analog.
//Right now it's not used.
const int CurrentSenseB= 1;

//I don't have a good feel for how long of a delay is needed but zero is
//not going to work to well.
int WaitTime  = 300;  //Setting this to longer values allows you watch each channel's LED



void setup() {
 
  //Set Pin Modes
  pinMode(EnableOne, OUTPUT);
  pinMode(EnableTwo, OUTPUT);
 
  pinMode (CoilOneSideA, OUTPUT);
  pinMode (BreakADisable, OUTPUT);
 
 
  pinMode (CoilTwoSideA, OUTPUT);
  pinMode (BreakBDisable, OUTPUT);
 
 
 
}

void loop()
{
  // This is the main code loop. It's where we energize the colis in on direction
  // and then in the other.
 
  int x = 100;
 
  for (x = 0; x <= 25; x++)
  {
     StepForward();
     // StepBackward();
  }
 
 

 
}

void StepForward()
{
  /*This is the step forward driver code*/
 
  //Enable both Changeless at the same time.
  digitalWrite(EnableOne,HIGH);
  digitalWrite (EnableTwo,HIGH);
 
 
  //Technically only have to do this once but I didn't want to code up
  //the logic to detect if this had been done before.
  digitalWrite(BreakADisable,LOW);
  digitalWrite(BreakBDisable,LOW);
 
  //Now energize the coils in following sequence
 
  digitalWrite(CoilOneSideA,HIGH);
  digitalWrite(CoilTwoSideA,HIGH);
 
  //Need to give the motor a finite amount of time to move before you try to make it move again.
  //this will also help reduce any moment over shoot or bounce.
  delay(WaitTime);
 
 
  digitalWrite(CoilOneSideA,LOW);
  //Leave CoildTwoSideA set to HIGH
 
  delay(WaitTime);
 
  //Leave CoilOneSideA set to Low
  digitalWrite(CoilTwoSideA,LOW);
 
  delay(WaitTime);
 
  digitalWrite(CoilOneSideA,HIGH);
  //Leave CoilTwoSideA set to low
 
  delay(WaitTime);
 
   
  //Now that you are done with the step, disable both Changeless.
  digitalWrite(EnableOne,HIGH);
  digitalWrite (EnableTwo,HIGH);
 
 
 
  
}

void StepBackward()
{
  /*This is the step backard driver code*/
 
  //Enable both Channles at the same time.
  digitalWrite(EnableOne,HIGH);
  digitalWrite (EnableTwo,HIGH);
 
 
  //Technically only have to do this once but I didn't want to code up
  //the logic to detect if this had been done before.
  digitalWrite(BreakADisable,LOW);
  digitalWrite(BreakBDisable,LOW);
 
  //Now energize the coils in foolowing sequence
 
  digitalWrite(CoilOneSideA,HIGH);
  digitalWrite(CoilTwoSideA,LOW);
 
  //Need to give the motor a fin tie amount of time to move before you try to make it move again.
  //this will also help reduce any moment over shoot or bounce.
  delay(WaitTime);
 
 
  digitalWrite(CoilOneSideA,LOW);
  //Leave CoildTwoSideA set to LOW
 
  delay(WaitTime);
 
  //Leave CoilOneSideA set to Low
  digitalWrite(CoilTwoSideA,HIGH);
 
  delay(WaitTime);
 
  digitalWrite(CoilOneSideA,HIGH);
  //Leave CoilTwoSideA set to HIGH
 
  delay(WaitTime);
 
   
  //Now that you are done with the step, disable both Channels.
  digitalWrite(EnableOne,HIGH);
  digitalWrite (EnableTwo,HIGH);
 
 
 
  
}
dudbarn says: Dec 2, 2012. 1:40 PM
I have copied and uploaded 2 of the programs to try. Both programs seem to upload fine, however, neither program has resulted in success of getting the motor to run. I am using a brand new arduino r3 and a brand new arduino motor shield. The wiring is hooked up as shown. Any help would be appreciated.
Thanks from a noob
mond2x says: Jan 17, 2013. 9:20 PM
hi mr. dudbarn as i've read you message I think to a point that we have the same problem encountered. do you have any solution now. please reply any help would be appreciated.
thanks.
freefloater says: Jan 12, 2013. 7:58 AM
does this also work for a solenoid? I need it to spin in each direction.
fbasmajian says: Nov 26, 2012. 8:58 PM
you have an error with the serial port. you need to tools-serial port-and then change the serial port
AntonioLopez says: Nov 18, 2012. 5:25 PM
Why can't I see my pictures on my post?
AntonioLopez says: Nov 18, 2012. 5:21 PM
Hi, I have the same Arduino and same motor shield. I uploaded the sketch, but I can't make it do anything, and I mean anything. Do I have to change the serial port which is five now?

thanks.......
justinmayer says: Oct 25, 2012. 10:48 AM
Hi there I'm new to the Arduino world and programming in general. I'm currently wanting to connect direction and on/off switches to my Arduino Uno/Motor Shield v3 and a bipolar stepper. I've read the previous posts regarding the code for this and think I understand it. The only part that I don't understand is where and how to connect the pushbutton switches to the Motor shield. I'm assuming there would be just one momentary switch for direction and one for on/off.
Thanks for any advise.
Justin
randofo (author) says: Oct 25, 2012. 10:57 AM
You should look at the Arduinio button and switch examples in the code library.

Any pin not being used by the Arduino shield can be set up as a switch input.

Check out intro to Arduino to get started.
justinmayer says: Oct 25, 2012. 9:08 PM
Thanks for getting back to me randofo. I've looked at the switch examples and that was helpful. As far as I can tell the following code is designed to start and stop as well as change the direction of the stepper. If so, where do the switches fit in? Also where does the code tell the stepper how fast to turn?
What I'm trying to do is to be able to press a button that makes the stepper turn x amount of steps at x rpm in one direction and then automatically stop until I press the button again. The other button would do the same thing but in the opposite direction. Can you show me a code that would do this?
Thanks much, justinmayer
I
// give the motor control pins names:
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;

void setup() {

//Setup Channel A
pinMode(dirA, OUTPUT); //Initiates Motor Channel A pin
pinMode(brakeA, OUTPUT); //Initiates Brake Channel A pin

//Setup Channel B
pinMode(dirB, OUTPUT); //Initiates Motor Channel A pin
pinMode(brakeB, OUTPUT); //Initiates Brake Channel A pin
randofo (author) says: Oct 26, 2012. 8:37 AM
That could just sets up the initial states of the Arduino. The loop code is what is making it spin, and it is only going clockwise.

You should check out the link thedanger just posted:
http://krisarnold.com/2012/02/03/arduino-and-the-official-motor-shield-r3/
justinmayer says: Oct 26, 2012. 9:44 AM
Thanks. Yes I have seen krisarnold's code which fills in more pieces of the puzzle. I still don't see any code for on/off, direction SWITCHES however! Am I missing something?
randofo (author) says: Oct 26, 2012. 11:50 AM
Those you are going to have to write! You should look into if-statements....

There are many ways to do this, but one way which might work for you is....

psudo-code:

if switch 1 triggered to "on"{
if switch 2 triggered left{
move motor left
}
else{
move motor right
}
}

justinmayer says: Oct 28, 2012. 11:30 PM
Thanks randofo. I decided to see if I could simply have a pushbutton switch just start the stepper turning when I press it and stop it when I let go.
So far the code uploaded without any errors but the stepper does not spin when I press the button but the brakes are on either way!
Any ideas? Thanks, Justin

// Include the Stepper Library
#include

// Map our pins to constants to make things easier to keep track of
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;
const int switchPin = 7;

// The amount of steps for a full revolution of your motor.
// 360 / stepAngle
const int STEPS = 200;

// Initialize the Stepper class
Stepper myStepper(STEPS, dirA, dirB);

void setup() {
// Set the RPM of the motor
myStepper.setSpeed(50);

//
pinMode(switchPin, INPUT);

// Turn on pulse width modulation
pinMode(pwmA, OUTPUT);
digitalWrite(pwmA, HIGH);
pinMode(pwmB, OUTPUT);
digitalWrite(pwmB, HIGH);

// Turn off the brakes
pinMode(brakeA, OUTPUT);
digitalWrite(brakeA, LOW);
pinMode(brakeB, OUTPUT);
digitalWrite(brakeB, LOW);


}

void loop()
{
if (digitalRead(switchPin == HIGH)
){
// Turn on pulse width modulation
pinMode(pwmA, OUTPUT);
digitalWrite(pwmA, HIGH);
pinMode(pwmB, OUTPUT);
digitalWrite(pwmB, HIGH);

// Turn off the brakes
pinMode(brakeA, OUTPUT);
digitalWrite(brakeA, LOW);
pinMode(brakeB, OUTPUT);
digitalWrite(brakeB, LOW);
}
else
{
// Turn off pulse width modulation
pinMode(pwmA, OUTPUT);
digitalWrite(pwmA, LOW);
pinMode(pwmB, OUTPUT);
digitalWrite(pwmB, LOW);

// Turn on the brakes
pinMode(brakeA, OUTPUT);
digitalWrite(brakeA, HIGH);
pinMode(brakeB, OUTPUT);
digitalWrite(brakeB, HIGH);
}



}
justinmayer says: Oct 29, 2012. 8:06 PM
I GOT IT TO WORK WITH THIS CODE:
// Include the Stepper Library
#include

// Map our pins to constants to make things easier to keep track of
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;
const int switchPin = 7;

// The amount of steps for a full revolution of your motor.
// 360 / stepAngle
const int STEPS = 200;

// Initialize the Stepper class
Stepper myStepper(STEPS, dirA, dirB);

void setup() {
// Set the RPM of the motor
myStepper.setSpeed(50);

// Turn on pulse width modulation
pinMode(pwmA, OUTPUT);
digitalWrite(pwmA, HIGH);
pinMode(pwmB, OUTPUT);
digitalWrite(pwmB, HIGH);

// Turn off the brakes
pinMode(brakeA, OUTPUT);
digitalWrite(brakeA, LOW);
pinMode(brakeB, OUTPUT);
digitalWrite(brakeB, LOW);

}

void loop() {
// Move the motor X amount of steps
if (digitalRead(switchPin) == HIGH)
myStepper.step(800);

}
thedanger says: Oct 14, 2012. 5:43 PM
Great info, I have been wanting to do something with arduino, anything really. I ended up getting an uno and motor shield. But I'm new to all of this, and there is very little on the motorshield as far as....lets say , make it do something. I followed your instructable and got this stepper up and running first try. One question though, how can i control the rpms/speed, and can i just tell it to take "x" amount of steps and then reverse the same number back?
randofo (author) says: Oct 16, 2012. 2:57 PM
Changing the delaylegnth variable will speed it up and slow it down.

Each part of the code that looks like this is a single step:

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

There has to be an easy way to cycle through these with code, but I can't work through that at this given moment. I will post some more code when I find myself with some free time. However, searching for "Arduino stepper motor code" might bring up something useful...
thedanger says: Oct 16, 2012. 5:54 PM
Maewert pointed me in the right direction, here is the link he gave me that compiled the steps, so you just tell it how many steps to take.

http://krisarnold.com/2012/02/03/arduino-and-the-official-motor-shield-r3/

starvan says: Oct 14, 2012. 4:46 AM
Hi! I need to control two stepper motors but each motor shield only controls one. Is it possible to control two stepper motors using 1 arduino and 2 motor shields, connecting one motor shield to another having 3 floors (arduino+motor shield 1+motor shield 2)?
randofo (author) says: Oct 16, 2012. 2:54 PM
If you want them to both do exactly the same thing... it might...?

You can use the free pins from the Arduino to wire to the shield's required pins and run them side-by-side. This would require remapping the unused pins to the required pins on the second shield.
AntonioLopez says: Sep 30, 2012. 6:07 PM
Is there a way to tell a dc motor how many revolutions to turn and then reverse?

I don't want to take apart what I already have and switch to a stepper motor.
skaar says: Oct 3, 2012. 12:50 AM
put an optoisolator disk on the shaft, as it turns, you count the flashes.
dejavucurly says: Aug 18, 2012. 11:10 AM
Hi every one.
I need to add direction button and start-stop button any one can help please?

for this code

/*************************************************************
Motor Shield Stepper Demo
by Randy Sarafan

For more information see:
http://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, 1023); //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, 1023); //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, 1023); //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, 1023); //Moves CH B

delay(delaylegnth);

}
ringai says: Aug 11, 2012. 4:38 PM
Hi Randy. The answer is probably obvious, but is is it possible to use two motor shields on a single Arduino?
randofo (author) says: Aug 11, 2012. 7:58 PM
Not stacked together. However, you should be able to wire the unused pins from the first one to the required pins on the second one, and drive both at once.
MichaelCaldas says: May 25, 2012. 9:22 AM
Hi.

I´ve two motors 6V and I need to supply with an external battery.

Which battery I will manage through the two engines Arduino Motor Shield?
ringai says: Aug 11, 2012. 4:33 PM
Right. Connect the external battery to the Motor Shield. The Arduino will still need its own power, too. So that will be connected to USB or to its own external power source.
thecoonskin says: May 13, 2012. 4:58 PM
can I run a 6V DC motor on the Arduinos power supply? Or do I need an external power supply?
randofo (author) says: May 14, 2012. 9:27 AM
You should be able to, but it will run better with an external supply.
vek11 says: Jul 24, 2012. 10:41 AM
how much should that supply be...I read it should be around 7.2v, but I just want to make sure as well.
4lifenerdfighter says: Jul 16, 2012. 4:27 PM
So this doesn't work with the Leonardo?
nkay3 says: Jul 17, 2012. 11:31 AM
This pololu graphic has it connecting to the Leonardo just fine: http://www.pololu.com/catalog/product/2503
4lifenerdfighter says: Jul 17, 2012. 3:21 PM
But that isn't the "official" motor shield.
THE STIG37 says: Jul 5, 2012. 8:45 AM
what about a 3.7V DC motor
1-40 of 70Next »
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!