A tutorial for the Arduino Motor Shield with a simple project.

### Overview

The Arduino Motor Shield is a shield that lets you control various loads that a typical Arduino pin cannot drive.  The motor shield has quite a few features such as current measuring and the ability to drive a single stepper motor.  At the heart of this shield is the L298P dual full bridge driver that can handle up to 3 amps for very short durations or 2 amps continuously per channel. Thorough example code is available for all the sections in the attached zipped folder.

### Power Supply Requirements

The motors attached to the motor shield need an ample power supply.  By using the USB connection, the current will often be limited to 500mA or 1 amp. Many motors will draw more current than the amount the USB source can supply.  To reduce the risk of possible damange to a usb port an external power supply should be used.

### Setup

In order to used this shield, you need to initialize several of the pins used by the shield. This setup code is typically placed in the setup function.  To use all the functionality of the shield, pins 8,9,12, and 13 need to be initialized as output pins.  This can be done with the following code:

                    pinMode(8,OUTPUT);       //Channel A Brake Pin Initialize
pinMode(9,OUTPUT);       //Channel B Brake Pin Initialize
pinMode(12,OUTPUT);      //Channel A Direction Pin Initialize
pinMode(13,OUTPUT);      //Channel B Direction Pin Initialize



### Direction Control

To control the motor’s direction, Pin 12 (Channel A) and Pin 13 (Channel B) are used.  To drive the motor forward* this pins needs to be brought high. The pins can be driven low to put the motors into reverse.

Important note: Changing direction rapidly can cause unexpected effects. From a mechanical standpoint, going from forward to reverse rapidly could potentially damage a gear box.  From an electrical standpoint, it can cause large current and voltage spikes.  To resolve these issues, a motor needs to be taken from one direction to another with a small pause inbetween. An example of this can be found in Basic_Control.ino, attached.

*Since the “forward” direction of the motors depends on application, for this tutorial "forward" will refer to positive voltage on the + screw terminal on the shield.

### Speed Control

To control the motors speed Pin 3 (Channel A) and Pin 11 (Channel B) can use PWM signals to vary the speed of the motors.  To use the PWM feature on the arduino the analogWrite function needs to be called.  In the function a pin needs to be defined and a speed between 0-255 needs to defined. An example of this can be found in Basic_Control.ino, attached.

### Braking

The motor driver on the Arduino Motor Shield has the ability to apply an engine brake.  The brake works by shorting both terminals of the motor together.  The brake is controlled by Pin 8 (Channel A) and Pin 9 (Channel B).

### Current Sensing

Another feature of the Arduino Motor Shield is the ability to determine the amount of current the motor (or any inductive load) is drawing.  Current sensing can be useful for robotics applications, such as traction control and determining if the robot is pushing an object. The current sense pins are A0 (Channel A) and A1 (Channel B).  The Motor Shield will output 3.3v on the current sense pins when the maximum channel current (2 amps) is reached.

After a small amount of math, it can be determined that each integer will represent 2.96mA. So for example, if the analogRead(A0) produces a value of 121 the motor (or load) is drawing 0.36 amps.  An example of this can be found in Current_to_Serial.ino, attached.

### Simple Project - Speed and Direction Control with a Potentiometer

By using the Arduino Motor Shield and a potentiometer, the speed and direction of a small motor can be controlled.

Parts Needed:

Steps:

1. Plug In the motor shield into the Arduino.

2. Wire the potentiometer.  The wiper of the potentiometer goes to pin A2 and the others go to Vin (assuming you're running off of USB power) and GND.

3. Attach the motor.  For this project “Channel A” is used.

4. Attach the AA Battery Pack.  Be sure to double check the polarity with a volt meter!

5. In the attached zip file there is a file for the Arduino IDE titled "pot_motor_control.ino".  Using the Arduino IDE, upload the code to your Arduino board. To upload the code to the Arduino, press the right arrow in the circle.

6. You are all set!  Move the potentiometer to adjust the speed and direction of the motor.

You now have a great way to control motor direction and speed using your Arduino.  This makes a great way to power your next robotics project.   In my next article I will show you how to incorporate this into a robotic platform. Happy building!

Give this project a try for yourself! Get the BOM.

• Mary Ling 2016-02-08

Since Arduino requires 7V, is it not a problem to power it with only 6V over the shield? I know that it works anyway, since I did something similar, but I wonder whether this could damage the arduino on the long run.
Shouldn’t be more correct to power shield and arduino separately?

• Mike Bartaby 2016-05-05

What’s the maximum voltage output you can get? Can this be controlled?

• sphenderson01 2017-05-24

Is it me or are the brake pin numbers for channel A and channel B incorrect on this guide? Should be:
Channel A = PIN 9
Channel B = PIN 8

• RK37 2017-05-24

Hi sphenderson01, could you elaborate on this issue so that we can check to see if the article requires a correction? What exactly makes you think that the pin numbers are swapped?