Ardumoto Shield Hookup Guide
This Tutorial is Retired!
This tutorial covers concepts or technologies that are no longer current. It's still here for you to read and enjoy, but may not be as useful as our newest tutorials.
View the updated tutorial: Ardumoto Shield Kit Hookup Guide
Example Code
Controlling the Ardumoto Shield is super-easy. If you can blink LEDs, you can make the Ardumoto Shield spin its motors. Here, we'll provide a simple, expandable example sketch to show how to drive the pair of motors on the Ardumoto.
The Example Sketch
Copy and paste the example sketch below, or click the here to download it:
language:c
/* Ardumoto Example Sketch
by: Jim Lindblom
date: November 8, 2013
license: Public domain. Please use, reuse, and modify this
sketch!
Three useful functions are defined:
setupArdumoto() -- Setup the Ardumoto Shield pins
driveArdumoto([motor], [direction], [speed]) -- Drive [motor]
(0 for A, 1 for B) in [direction] (0 or 1) at a [speed]
between 0 and 255. It will spin until told to stop.
stopArdumoto([motor]) -- Stop driving [motor] (0 or 1).
setupArdumoto() is called in the setup().
The loop() demonstrates use of the motor driving functions.
*/
// Clockwise and counter-clockwise definitions.
// Depending on how you wired your motors, you may need to swap.
#define CW 0
#define CCW 1
// Motor definitions to make life easier:
#define MOTOR_A 0
#define MOTOR_B 1
// Pin Assignments //
// Don't change these! These pins are statically defined by shield layout
const byte PWMA = 3; // PWM control (speed) for motor A
const byte PWMB = 11; // PWM control (speed) for motor B
const byte DIRA = 12; // Direction control for motor A
const byte DIRB = 13; // Direction control for motor B
void setup()
{
setupArdumoto(); // Set all pins as outputs
}
void loop()
{
// Drive motor A (and only motor A) at various speeds, then stop.
driveArdumoto(MOTOR_A, CCW, 255); // Set motor A to CCW at max
delay(1000); // Motor A will spin as set for 1 second
driveArdumoto(MOTOR_A, CW, 127); // Set motor A to CW at half
delay(1000); // Motor A will keep trucking for 1 second
stopArdumoto(MOTOR_A); // STOP motor A
// Drive motor B (and only motor B) at various speeds, then stop.
driveArdumoto(MOTOR_B, CCW, 255); // Set motor B to CCW at max
delay(1000); // Motor B will spin as set for 1 second
driveArdumoto(MOTOR_B, CW, 127); // Set motor B to CW at half
delay(1000); // Motor B will keep trucking for 1 second
stopArdumoto(MOTOR_B); // STOP motor B
// Now spin both!
driveArdumoto(MOTOR_A, CW, 255); // Motor A at max speed.
driveArdumoto(MOTOR_B, CW, 255); // Motor B at max speed.
delay(1000); // Drive forward for a second
// Now go backwards at half that speed:
driveArdumoto(MOTOR_A, CCW, 127); // Motor A at max speed.
driveArdumoto(MOTOR_B, CCW, 127); // Motor B at max speed.
}
// driveArdumoto drives 'motor' in 'dir' direction at 'spd' speed
void driveArdumoto(byte motor, byte dir, byte spd)
{
if (motor == MOTOR_A)
{
digitalWrite(DIRA, dir);
analogWrite(PWMA, spd);
}
else if (motor == MOTOR_B)
{
digitalWrite(DIRB, dir);
analogWrite(PWMB, spd);
}
}
// stopArdumoto makes a motor stop
void stopArdumoto(byte motor)
{
driveArdumoto(motor, 0, 0);
}
// setupArdumoto initialize all pins
void setupArdumoto()
{
// All pins should be setup as outputs:
pinMode(PWMA, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(DIRA, OUTPUT);
pinMode(DIRB, OUTPUT);
// Initialize all pins as low:
digitalWrite(PWMA, LOW);
digitalWrite(PWMB, LOW);
digitalWrite(DIRA, LOW);
digitalWrite(DIRB, LOW);
}
Then upload to your Arduino and watch your motors spin! If you want to dig really deep into the sketch, check out the comments.
Explaining the Sketch
For each motor there are two mechanisms we can control -- the direction of rotation and the speed. Each of those mechanisms is controlled by one pin on the Arduino.
Controlling Rotation Direction
We can only spin the motor in two directions -- clockwise or counter-clockwise -- so we only need two values -- 0 or 1 -- to control that from the Arduino. We can simply digitalWrite
either of the direction pins (pin 12 for motor A, pin 13 for motor B) HIGH
or LOW
to go forward or backward.
For example, if you want motor A to spin clockwise, you simply need to digitalWrite
pin 12 LOW
:
language:c
digitalWrite(12, LOW); // Motor A will spin clockwise
To make it spin the other way, write the pin HIGH
.
language:c
digitalWrite(12, HIGH); // Motor A will spin counter-clockwise
(Note: The rotation direction depends on how you wired the motor to your shield. If you swapped the red and black wires, the motor will spin opposite of how we've described here.)
Speeding
To control the speed of a motor we need to analogWrite
to the PWM pins (pin 3 for motor A, pin 11 for motor B). A higher analogWrite
value means a faster spin. Writing the pin LOW
(or 0) will stop the motor.
PWM value | Motor Spin Speed |
---|---|
0 | Off (Stop) |
127 | Half speed |
255 | Full speed |
If we want to turn motor A up to maximum speed, this is all we need:
language:c
analogWrite(3, 255); // Motor A at max speed
After that line of code is executed, the motor will spin until stopped. To stop the motor, replace 255 with 0:
language:c
analogWrite(3, 0); // Stop motor A
Don't forget to set your direction before spinning your motor!