Arduino Motor Shields

DK Electronics Motor Shield

DK_Elec_Arduino_Motor_Shield_01.jpg

This board is also known as the Adafruit v1 Motor Shield

Features
2 connections for 5V 'hobby' servos connected to the Arduino's high-resolution dedicated timer - no jitter!
Up to 4 bi-directional DC motors with individual 8-bit speed selection (so, about 0.5% resolution)
Up to 2 stepper motors (unipolar or bipolar) with single coil, double coil, interleaved or micro-stepping.
4 H-Bridges: L293D chipset provides 0.6A per bridge (1.2A peak) with thermal shutdown protection, 4.5V to 25V
Pull down resistors keep motors disabled during power-up
Big terminal block connectors to easily hook up wires (10-22AWG) and power
Arduino reset button brought up top
2-pin terminal block to connect external power, for seperate logic/motor supplies
Tested compatible with Mega, Diecimila, & Duemilanove

Notes:

1. You can use 2 DC servos that run on 5V and up to 4 DC motors or 2 stepper motors (or 1 stepper and up to 2 DC motors)
2. The LED indicates the DC/Stepper motor power supply is working. The servo ports are 5V powered and do not use the DC motor supply.
3. The motor controllers are designed to run from 4.5V to 25V. MOST 1.5-3V MOTORS WILL NOT WORK.
4. The motor driver chips that come with the shield are designed to provide up to 600 mA per motor, with 1.2A peak current. Note that once you near 1A load you' MUST install a heatsink on the motor driver IC, otherwise you will have thermal failure, possibly ruining the chip.
5. SN754410: You can use the SN754410 motor driver chip as well because it is pin-compatible, has output diodes and can provide 1A per motor, 2A peak. There are caveats when using this IC. It seems the output diodes were designed for ESD protection only and that using them as kickback-protection is a hack and not guaranteed for performance. For that reason the original Adafruit kit did not ship with the SN754410 and instead uses the L293D with integrated kickback-protection diodes. If you're willing to risk it, it can be viewed as a way to get more current.
6. If you need more power you can piggyback up to (2) more L293D driver chips soldering them on top of the L293D. You double the current capability!
7. You can't run motors off of a 9V battery. Use a larger Lead Acid or NiMH battery pack.
8. You should use two power supplies (split supplies.) One should be for the Arduino and one for the electric motors. Most motor problems are due to noise on the power line caused by using the same supply. Obviously choose a power supply adequate for the expected current load.
9. For heavier (real world use) you should use a separate power supply for your servo motors. You can cut the trace on the shield PCB going to the positive(+) side of the servo connectors and then connect an external 5-6V supply!
10. There are two places you can get your motor 'high voltage supply' from. One is the DC jack on the Arduino board and the other is the 2-terminal block on the shield that is labeled EXT_PWR. Warning! The EXT_PWR terminals on the shield do not have a protection diode. Be utterly careful when connecting external power or you will destroy the motor shield and/or your Arduino. See below schematic.
adafruit_motor_shield_ext_power.jpg
11. To use two separate DC power supplies for the Arduino and motors. Plug in the supply for the Arduino into the DC jack, then connect the motor supply to the PWR_EXT block. Make sure the jumper is removed from the motor shield.

12. Using RC Servos - Hobby servos are the easiest way to get going with motor control. They have a 3-pin 0.1" female header connection with +5V, ground and signal inputs. The motor shield simply brings out the 16bit PWM output lines to two 3-pin headers so that its easy to plug in and go. They can draw a lot of power.

The advantage of using the onboard PWM is that its very precise and works in the background. You can use the built in Servo library.

Using the servos is easy, read the official Arduino documentation for how to use them and see the example Servo sketches in the IDE. See http://www.arduino.cc/en/Reference/Servo for more details.

Power for the Servos comes from the Arduino's on-board 5V regulator, powered directly from the USB or DC power jack on the Arduino. If you need an external supply, cut the trace right below the servo pins (on v1.2 boards) and connect a 5V or 6V DC supply directly. Using an external supply is risky as you can accidentally destroy the servos by connecting a power supply incorrectly!

13. When using the external supply header for servos, make sure that the bottom of the shield does not short out against the metal USB port housing on the Arduino. You need to insulate the bottom of the shield PCB under this area.

14. Using Stepper Motors - Stepper motors are great for (semi-)precise control, perfect for many robot and CNC projects. This motor shield supports up to 2 stepper motors. The library works identically for bi-polar and uni-polar motors.

For unipolar motors: to connect the stepper, first figure out which pins connected to which coil, and which pins are the center taps. If its a 5-wire motor then there will be 1 that is the center tap for both coils. Jason Babcock has a great article on reverse-engineering the stepper pinouts. See http://www.jasonbabcock.com/computing/breadboard/unipolar/index.html There is also a great document on stepper motor types by Douglas Jones. See http://homepage.divms.uiowa.edu/~jones/step/types.html The center taps should both be connected together to the GND terminal on the motor shield output block. then coil 1 should connect to one motor port (say M1 or M3) and coil 2 should connect to the other motor port (M2 or M4).

For bipolar motors: its just like unipolar motors except theres no 5th wire to connect to ground. The code is exactly the same.

Running a stepper is a little more intricate than running a DC motor.
1. Make sure you #include <AFMotor.h>
2. Create the stepper motor object with AF_Stepper(steps, stepper#) to setup the motor H-bridge and latches. Steps indicates how many steps per revolution the motor has. a 7.5 degree/step motor has 360/7.5 = 48 steps. Stepper# is which port it is connected to. If you're using M1 and M2, its port 1. If you're using M3 and M4 it's port 2.
3. Set the speed of the motor using setSpeed(rpm) where rpm is how many revolutions per minute you want the stepper to turn.
4. Then every time you want the motor to move, call the step(#steps, direction, steptype) procedure.#steps is how many steps you'd like it to take. direction is either FORWARD or BACKWARD and the step type is SINGLE, DOUBLE. INTERLEAVE or MICROSTEP. "Single" means single-coil activation, "double" means 2 coils are activated at once (for higher torque) and "interleave" means that it alternates between single and double to get twice the resolution (but of course its half the speed). "Microstepping" is a method where the coils are PWM'd to create smooth motion between steps. You can use whichever stepping method you want, changing it "on the fly" to as you may want minimum power, more torque, or more precision.
5. By default, the motor will 'hold' the position after its done stepping. If you want to release all the coils, so that it can spin freely, call release(). The stepping commands are 'blocking' and will return once the steps have finished. Because the stepping commands 'block' - you have to instruct the Stepper motors each time you want them to move. If you want to have more of a 'background task' stepper control, check out AccelStepper library https://github.com/adafruit/AccelStepper (install similarly to how you did with AFMotor) which has some examples for controlling two steppers simultaneously with varying acceleration.

Example code

#include <AFMotor.h>

AF_Stepper motor(48, 2);

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");

  motor.setSpeed(10);  // 10 rpm   

  motor.step(100, FORWARD, SINGLE); 
  motor.release();
  delay(1000);
}

void loop() {
  motor.step(100, FORWARD, SINGLE); 
  motor.step(100, BACKWARD, SINGLE); 

  motor.step(100, FORWARD, DOUBLE); 
  motor.step(100, BACKWARD, DOUBLE);

  motor.step(100, FORWARD, INTERLEAVE); 
  motor.step(100, BACKWARD, INTERLEAVE); 

  motor.step(100, FORWARD, MICROSTEP); 
  motor.step(100, BACKWARD, MICROSTEP); 
}

15. Using DC Motors - The motor shield can drive up to 4 DC motors bidirectionally. That means they can be driven forwards and backwards. The speed can also be varied at 0.5% increments using the high-quality built in PWM. This means the speed is very smooth and won't vary! Note that the H-bridge chip is not meant for driving loads over 0.6A or that peak over 1.2A so this is for small motors. Check the datasheet for information about the motor to verify its OK.

To connect a motor, simply solder two wires to the terminals and then connect them to either the M1, M2, M3, or M4. Then follow these steps in your sketch:

1. Make sure you #include <AFMotor.h>
2. Create the AF_DCMotor object with AF_DCMotor(motor#, frequency), to setup the motor H-bridge and latches. The constructor takes two arguments. The first is which port the motor is connected to, 1, 2, 3 or 4. frequency is how fast the speed controlling signal is. For motors 1 and 2 you can choose MOTOR12_64KHZ, MOTOR12_8KHZ, MOTOR12_2KHZ, or MOTOR12_1KHZ. A high speed like 64KHz wont be audible but a low speed like 1KHz will use less power. Motors 3 & 4 are only possible to run at 1KHz and will ignore any setting given
3. Then you can set the speed of the motor using setSpeed(speed) where the speed ranges from 0 (stopped) to 255 (full speed). You can set the speed whenever you want.
4. To run the motor, call run(direction) where direction is FORWARD, BACKWARD or RELEASE. Of course, the Arduino doesn't actually know if the motor is 'forward' or 'backward', so if you want to change which way it thinks is forward, simply swap the two wires from the motor to the shield.

#include <AFMotor.h>

AF_DCMotor motor(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Motor test!");

  motor.setSpeed(200);     // set the speed to 200/255
}

void loop() {
  Serial.print("tick");

  motor.run(FORWARD);      // turn it on going forward
  delay(1000);

  Serial.print("tock");
  motor.run(BACKWARD);     // the other way
  delay(1000);

  Serial.print("tack");
  motor.run(RELEASE);      // stopped
  delay(1000);
}

16. AF_DCMotor Class - The AF_DCMotor class provides speed and direction control for up to four DC motors. To use this in a sketch you must first add the following line at the beginning of your sketch:

#include <AFMotor.h>
.
AF_DCMotor motorname(portnum, freq)
This is the constructor for a DC motor. Call this constructor once for each motor in your sketch. Each motor instance must have a different name as in the example below.
Parameters:
port num - selects which channel (1-4) of the motor controller the motor will be connected to
freq - selects the PWM frequency. If no frequency is specified, 1KHz is used by default.

Frequencies for channel 1 & 2 are:
MOTOR12_64KHZ
MOTOR12_8KHZ
MOTOR12_2KHZ
MOTOR12_1KHZ

Frequencies for channel 3 & 4 are:
MOTOR34_64KHZ
MOTOR34_8KHZ
MOTOR34_1KHZ

Example code

AF_DCMotor motor4(4); // define motor on channel 4 with 1KHz default PWM
AF_DCMotor left_motor(1, MOTOR12_64KHZ);  // define motor on channel 1 with 64KHz PWM

NOTE: Higher frequencies will produce less audible hum in operation, but may result in lower torque with some motors.

setSpeed(speed)
Sets the speed of the motor.
Parameters:
speed- Valid values for 'speed' are between 0 and 255 with 0 being off and 255 as full throttle.

Note: DC Motor response is not typically linear, and so the actual RPM will not necessarily be proportional to the programmed speed.

run(cmd)
Sets the run-mode of the motor.

Parameters:
cmd - the desired run mode for the motor.

Valid values for cmd are:
FORWARD - run forward (actual direction of rotation will depend on motor wiring),
BACKWARD - run backwards (rotation will be in the opposite direction from FORWARD),
RELEASE - Stop the motor. This removes power from the motor and is equivalent to setSpeed(0). The motor shield does not implement dynamic breaking, so the motor may take some time to spin down.

Example Code

motor.run(FORWARD);
delay(1000);  // run forward for 1 second
motor.run(RELEASE);
delay(100);  // 'coast' for 1/10 second
motor.run(BACKWARDS);  // run in reverse

17. AF_Stepper Class The AF_Stepper class provides single and multi-step control for up to 2 stepper motors .. To use function this in a sketch you must first add the following include line at the beginning of your sketch:

*#include <AFMotor.h>

AF_Stepper steppername(steps, portnumber)
The AF_Stepper constructor defines a stepper motor. Call this once for each stepper motor in your sketch. Each stepper motor instance must have a unique name as in the example below.
Parameters:
steps - declare the number of steps per revolution for your motor.
num - declare how the motor will be wired to the shield.
Valid values for 'num' are 1 (channels 1 & 2) and 2 (channels 3 & 4).
Example Code:

AF_Stepper Stepper1(48, 1);  // A 48-step-per-revolution motor on channels 1 & 2
AF_Stepper Stepper2(200, 2);   // A 200-step-per-revolution motor on channels 3 & 4

step(steps, direction, style)
Step the motor.

Parameters:
steps - the number of steps to turn
direction - the direction of rotation (FORWARD or BACKWARD)
style - the style of stepping:

Valid values for 'style' are:
SINGLE - One coil is energized at a time.
DOUBLE - Two coils are energized at a time for more torque.
INTERLEAVE - Alternate between single and double to create a half-step in between. This can result in smoother operation, but because of the extra half-step, the speed is reduced by half too.
MICROSTEP - Adjacent coils are ramped up and down to create a number of 'micro-steps' between each full step. This results in finer resolution and smoother rotation, but with a loss in torque.
Note: Step is a synchronous command and will not return until all steps have completed. For concurrent motion of two motors, you must handle the step timing for both motors and use the "onestep()" function below.

Example Code:

Stepper1.step(100, FORWARD, DOUBLE); // 100 steps forward using double coil stepping
Stepper2.step(100, BACKWARD, MICROSTEP);   // 100 steps backward using double microstepping

setSpeed(RPMspeed)
set the speed of the motor

Parameters:
Speed - the speed in RPM

Note: The resulting step speed is based on the 'steps' parameter in the constructor. If this does not match the number of steps for your motor, you actual speed will be off as well.

Example Code:

Stepper1.setSpeed(10);  // Set motor 1 speed to 10 rpm  
Stepper2.setSpeed(30);  // Set motor 2 speed to 30 rpm

onestep(direction, stepstyle)
Single step the motor.

Parameters:
direction - the direction of rotation (FORWARD or BACKWARD)
stepstyle - the style of stepping:
Valid values for 'style' are:
SINGLE - One coil is energized at a time.
DOUBLE - Two coils are energized at a time for more torque.
INTERLEAVE - Alternate between single and double to create a half-step in between. This can result in smoother operation, but because of the extra half-step, the speed is reduced by half too.
MICROSTEP - Adjacent coils are ramped up and down to create a number of 'micro-steps' between each full step. This results in finer resolution and smoother rotation, but with a loss in torque.

Example:Code

Stepper1.onestep(FORWARD, DOUBLE);  // take one step forward using double coil stepping

release()
Release the holding torque on the motor. This reduces heating and current demand, but the motor will not actively resist rotation.

Example Code:

Stepper1.release(); // stop rotation and turn off holding torque.

To use this shield you must first install the Arduino libraries into your IDE!

AF_Motor Library
First Install the AF_Motor Library, before you can use the Motor shield, you must install the library into the IDE. See Adafruit-Motor-Shield-library-1.0.0-3-g99381df.zip https://github.com/adafruit/Adafruit-Motor-Shield-library/zipball/master I have also archived it here. See http://terminus.attitude.net/local--files/arduino-motor-shields/adafruit-Adafruit-Motor-Shield-library-1.0.0-3-g99381df.zip
Installation
Clone or download the github repo zip file
Rename the unzipped directory to AFMotor
Move the AFMotor directory into your arduinosketchfolder/libraries directory.
Restart IDE software.

See the Adafruit Tutorial https://learn.adafruit.com/adafruit-motor-shield/overview to get started. It is a very decent introduction and helps a beginner to quickly useful designs. Hopefully AdaFruit will always keep the articles and documentation around but if not I will.

Schematics & Layout
The Github repo is https://github.com/adafruit/Adafruit-Motor-Shield-for-Arduino but I will also archive it here See http://terminus.attitude.net/local--files/arduino-motor-shields/Adafruit-Motor-Shield-for-Arduino-master.zip for the contents of the Github repo as of 2018-08-04.

Arduino Stepper/Servo software library with microstepping support.
The Github repo is https://github.com/adafruit/Adafruit-Motor-Shield-library but I will also archive it here http://terminus.attitude.net/local--files/arduino-motor-shields/Adafruit-Motor-Shield-library-master.zip as of 2018-08-04.
Installation
Clone or download the github repo zip file.
Rename the directory to AFmotor (check that the renamed directory contains the .cpp and .h files) and install into the Arduinosketches/libraries directory.
Restart IDE software.

AccelStepper library with AFMotor support.
The Github repo is https://github.com/adafruit/AccelStepper but I will also archive it here. See AccelStepper-master.zip as of 2018-08-04.
This library allows for advanced stepper control including accelleration and decelleration, and concurrent stepper control! You still need AFmotor above!
Installation
Clone or download the github repo zip file.
Rename the directory to AccelStepper (check that the renamed directory contains the .cpp and .h files) and install into the Arduinosketches/libraries directory.
Restart IDE software.

Schematic:

DK_Multi-MotorShieldSchematic.jpg

AFMotor Library Reference by Bill Earl

afmotor-library-reference.pdf

The above document was mirrored from https://cdn-learn.adafruit.com/downloads/pdf/afmotor-library-reference.pdf