AFMotor: DC motors and steppers on the L293D shield
The blue or red "L293D Motor Drive Shield" in most Arduino kits is a copy of Adafruit’s first motor shield, and AFMotor is the library written for it. It runs four DC motors on the M1 to M4 terminals, or two steppers (M1 and M2 make stepper 1, M3 and M4 make stepper 2). The two servo headers on the shield are just pins 9 and 10, which you drive with the ordinary Servo library.
Built in on Mokxi: #include <AFMotor.h> and it builds, with nothing to install. Free, and no account needed.
What Adafruit Motor Shield library (AFMotor, v1 shield and L293D clones) does
The shield saves pins with a 74HC595 shift register that holds all eight direction bits. Changing a motor’s direction shifts a byte out on pins 8 (data), 4 (clock) and 12 (latch), with pin 7 as the output enable. Speed is PWM on the L293D enable inputs: motor 1 on pin 11, motor 2 on 3, motor 3 on 6 and motor 4 on 5. Those pins are taken while the shield is on, even for motors you never use.
Mokxi has no one-piece shield, so the example builds it from its chips: a 74HC595, an L293D with a battery pack on its motor supply, and a DC motor on motor 1’s outputs. The library drives real pins, so a logic analyzer on 8, 4 and 12 shows each direction change. The PWM frequency argument (MOTOR12_64KHZ and the rest) is accepted and ignored. For the newer V2 shield, which talks I2C through a PCA9685, use the Adafruit Motor Shield V2 library instead.
| Library Manager name | Adafruit Motor Shield library (AFMotor, v1 shield and L293D clones) |
|---|---|
| Author | Adafruit |
| Include | #include <AFMotor.h> |
| Boards | Arduino Uno, Arduino Nano, Arduino Mega 2560, Arduino Leonardo, SparkFun Pro Micro, Arduino Micro, Arduino Pro Mini 3.3 V, Arduino Uno R4, Arduino Uno R4 WiFi |
| In the simulator | Simulated: it runs against the part the way it does on the bench. |
| Parts it drives | Motor driver, L293D, 74HC595 shift register, DC motor |
Install it
It builds on Arduino Uno, Arduino Nano, Arduino Mega 2560, Arduino Leonardo, SparkFun Pro Micro, Arduino Micro, Arduino Pro Mini 3.3 V, Arduino Uno R4, Arduino Uno R4 WiFi.
In Mokxi it is built in: #include <AFMotor.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "Adafruit Motor Shield library (AFMotor, v1 shield and L293D clones)" by Adafruit from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add Adafruit Motor Shield library (AFMotor, v1 shield and L293D clones) to lib_deps in PlatformIO. Mokxi follows the calls of release 1.0.1, so a sketch written here builds there too.
What to know in the simulator: Build the shield from its parts: a 74HC595 on pins 8, 4, 12 and 7 and L293Ds enabled from 11, 3, 6 and 5 (the header says which Q output goes where). The PWM frequency argument is ignored.
The calls you will use
In the order a sketch meets them.
AF_DCMotor motor(1)
A DC motor on terminal M1 to M4. An optional second argument names a PWM frequency, which the simulator ignores.
motor.setSpeed(200)
Speed from 0 to 255, written with analogWrite() to that motor’s enable pin. It does not start the motor by itself.
motor.run(FORWARD)
Sets direction through the shift register: FORWARD, BACKWARD, BRAKE (both inputs high, which shorts the motor) or RELEASE (both low, so it coasts).
AF_Stepper stepper(200, 2)
A stepper with 200 steps a revolution on port 2, which is terminals M3 and M4. Port 1 is M1 and M2.
stepper.setSpeed(30)
The stepper’s speed in revolutions a minute, used to space the steps of the next step() call.
stepper.step(100, FORWARD, DOUBLE)
Moves a number of steps and returns when done. The style is SINGLE, DOUBLE (two coils, more torque), INTERLEAVE (half steps) or MICROSTEP.
stepper.onestep(FORWARD, SINGLE)
Moves exactly one step and returns at once, for sketches that pace the steps themselves in loop().
stepper.release()
Turns off both coils so the stepper stops drawing current and can be turned by hand.
Example: A DC motor on the L293D shield
On the Arduino Uno R3. It runs in the browser exactly as printed.
#include <AFMotor.h>
// The shield's 74HC595 is on pins 8 (data), 4 (clock), 12 (latch) and 7
// (enable); motor 1's speed is pin 11. Q2 and Q3 carry motor 1's direction.
AF_DCMotor motor(1);
void setup() {
Serial.begin(9600);
motor.setSpeed(200);
motor.run(RELEASE);
delay(100);
motor.run(FORWARD);
Serial.println("forward");
}
void loop() {}Wiring
- Arduino Uno R3 8 to 74HC595 shift register DS
- Arduino Uno R3 4 to 74HC595 shift register SHCP
- Arduino Uno R3 12 to 74HC595 shift register STCP
- Arduino Uno R3 7 to 74HC595 shift register OE
- Arduino Uno R3 5V to 74HC595 shift register MR
- Arduino Uno R3 5V to 74HC595 shift register VCC
- Arduino Uno R3 GND to 74HC595 shift register GND
- 74HC595 shift register Q2 to Motor driver, L293D IN1
- 74HC595 shift register Q3 to Motor driver, L293D IN2
- Arduino Uno R3 11 to Motor driver, L293D EN12
- Arduino Uno R3 5V to Motor driver, L293D VSS
- Arduino Uno R3 GND to Motor driver, L293D GND1
- Battery: AA, coin cell, LiPo V+ to Motor driver, L293D VS
- Battery: AA, coin cell, LiPo V- to Motor driver, L293D GND2
- Battery: AA, coin cell, LiPo V- to Arduino Uno R3 GND
- Motor driver, L293D OUT1 to DC motor 1
- Motor driver, L293D OUT2 to DC motor 2
Common errors and how to fix them
The motor hums but does not turn, or the Arduino resets when it starts
The motors are starved of power. The L293D drops about two volts, and USB power cannot start a motor. Feed the shield’s EXT_PWR terminal from a battery pack or adapter, and remove the PWR jumper if that supply is above 12 V.
Motor 1 or motor 2 stops working after adding another part
Pins 11 and 3 are those motors’ speed pins, and tone() or IRremote take over timer 2, which makes PWM on both. Move the other part, or put your motors on M3 and M4, whose speed pins are 6 and 5.
'AF_DCMotor' does not name a type
The sketch is not including AFMotor.h, often because it was written for the V2 shield and includes Adafruit_MotorShield.h. The two libraries are not interchangeable: match the include and the calls to the shield you actually have.
The rest of the sketch freezes while the stepper moves
step() blocks until the last step is taken. For buttons or a display to keep working, call onestep() once per pass of loop() on a millis() timer, or hand the stepping to AccelStepper with onestep() inside its callbacks.
Questions and answers
How do I tell a V1 shield from a V2?
A V1 shield or clone has two large L293D chips and a 74HC595 and uses AFMotor. A V2 has small TB6612 drivers and a PCA9685, connects over I2C and uses Adafruit_MotorShield.
Can I still use servos with this shield?
Yes. The SERVO_1 and SERVO_2 headers are wired to pins 10 and 9 with 5 V and ground, and the Servo library drives them as usual.
Projects that use Adafruit Motor Shield library (AFMotor, v1 shield and L293D clones)
Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.
Keep going
Try Adafruit Motor Shield library (AFMotor, v1 shield and L293D clones) without installing anything
Open the example, change the sketch, and keep your version in a free account.