Arduino and the Official Motor Shield R3

The motor shield provided sold by Arduino is pretty awesome and can control 2 DC brush motors or one stepper motor. After coming home with mine I found finding a good example of how to use it with the Stepper class a little difficult to find at first.

Alot of the documentation I was able to find was old and outdated. Hopefully this post will help anyone else in the same boat. Below is sample code to get the Arduino Official Motor Shield working with a bi-polar stepping motor and the Stepper library provided by Arduino. This example will power up the motor and spin it one revolution, then pause for 2 seconds, and then reverse.

// Include the Stepper Library
#include <Stepper.h>

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

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

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

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

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

  // Log some shit
  Serial.begin(9600);
}

void loop() {
  // Move the motor X amount of steps
  myStepper.step(STEPS);
  Serial.println(STEPS);
  // Pause
  delay(2000);

  // Move the motor X amount of steps the other way
  myStepper.step(-STEPS);
  Serial.println(-STEPS);
  // Pause
  delay(2000);
}

5 Responses to “Arduino and the Official Motor Shield R3”

  1. Jim Craig says:

    Thank you! I have been looking all over for this. I wish I’d stopped here first.

    I have quite a few Berger RDM57 steppers from old slide projectors and thought they’d be ideal. It took some searching, experimentation and finally just looking at what was before me to figure out which wires were A+, A-, etc.

    Now, do you know anything about the white and orange pin headers on the top of the board? That could be useful as well.

  2. ajalics says:

    This is very good information about using the “official” R3 motor shield you can get from radioshack. This should be better documented.

    Problem with the code included above:
    // Include the Stepper Library
    #include

    Should be:
    // Include the Stepper Library
    #include

  3. ajalics says:

    // Include the Stepper Library
    #include

    should be

    // Include the Stepper Library
    #include <>

  4. ajalics says:

    // Include the Stepper Library
    #include <Stepper.h>

  5. Richard says:

    Thanks Kris! I think this is the best example/sample program/page out there for the new motor shield (R3). I made the suggested edit, and changed some parameters for my Nema 17 motor and it runs great at 200-300 rpms, but no faster (12v). I was wondering if the speed is voltage and/or program dependent? I’d like to get 600 to 1000 rpms for my latest project (speed is more important then precision since I am opening some small doors). And suggestions would be appreciated.

Leave a Reply

See also: