Two motors on an L293D motor shield, on an Arduino Uno
The L293D motor shield on an Uno runs two DC motors with the AFMotor library: M1 ramps up and down forward while M3 runs backward, from a 9 V battery. The whole build, an Arduino Uno and 4 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Intermediate About 25 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $35. On Mokxi they're free.
What you'll learn
- A motor shield separates logic and motor power
- Direction and speed on DC motors
- Using the
AFMotorlibrary - Speed ramps with a
forloop
- Difficulty: Intermediate
- Time: About 25 minutes
- Board: Arduino Uno R3
- Parts: 5
How it works
An L293D motor shield plugs onto the Uno and drives DC motors. The Uno cannot power motors, so a battery on EXT+ and EXT- supplies them. Two motors are connected: one to M1 and one to M3.
The shield uses a 74HC595 shift register on pins 4, 7, 8 and 12 to set each motor's direction, and PWM on pin 11 for M1 and pin 6 for M3 to set speed. The AFMotor library hides this.
The right motor runs backward at a fixed speed of 180. The left motor runs forward and its speed ramps from 0 to 255 and back down again in steps of 5, every 20 ms.
Parts list
5 parts, plus the jumper wires. They retail for about $35; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × L293D motor shield
- 2 × DC motorIn Robot Car Kit.
- 1 × Battery: AA, coin cell, LiPoIn Handheld Console Kit, Laser Sword Kit, Power Bank Kit.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| L293D motor shield pin D12 | Arduino Uno R3 pin 12 |
| L293D motor shield pin D11 | Arduino Uno R3 pin 11 |
| L293D motor shield pin D8 | Arduino Uno R3 pin 8 |
| L293D motor shield pin D7 | Arduino Uno R3 pin 7 |
| L293D motor shield pin D6 | Arduino Uno R3 pin 6 |
| L293D motor shield pin D4 | Arduino Uno R3 pin 4 |
| L293D motor shield pin 5V | Arduino Uno R3 pin 5V |
| L293D motor shield pin GND | Arduino Uno R3 pin GND |
| Battery: AA, coin cell, LiPo pin V+ | L293D motor shield pin EXT+ |
| Battery: AA, coin cell, LiPo pin V- | L293D motor shield pin EXT- |
| DC motor (1) pin 1 | L293D motor shield pin M1A |
| DC motor (1) pin 2 | L293D motor shield pin M1B |
| DC motor (2) pin 1 | L293D motor shield pin M3A |
| DC motor (2) pin 2 | L293D motor shield pin M3B |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Two DC motors on the L293D motor shield, with the AFMotor library.
//
// The shield's 74HC595 (pins 4, 7, 8 and 12) sets each motor's direction and
// its two L293Ds take the speed as PWM: M1 on pin 11, M3 on pin 6. The motors'
// own supply goes on EXT+ and EXT-, here a 9 V battery.
#include <AFMotor.h>
AF_DCMotor left(1);
AF_DCMotor right(3);
void setup() {
right.setSpeed(180);
right.run(BACKWARD);
left.run(FORWARD);
}
void loop() {
for (int speed = 0; speed <= 255; speed += 5) {
left.setSpeed(speed);
delay(20);
}
for (int speed = 255; speed >= 0; speed -= 5) {
left.setSpeed(speed);
delay(20);
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Sets the right motor's speed to 180 and its direction to BACKWARD, then sets the left motor to FORWARD. The left motor's speed is set later in loop.
void setup() {
right.setSpeed(180);
right.run(BACKWARD);
left.run(FORWARD);
}loop sketch.ino
Ramps the left motor's speed from 0 up to 255 and back down to 0. Each step changes it by 5 and waits 20 ms, so the motor smoothly speeds up and slows down.
void loop() {
for (int speed = 0; speed <= 255; speed += 5) {
left.setSpeed(speed);
delay(20);
}
for (int speed = 255; speed >= 0; speed -= 5) {
left.setSpeed(speed);
delay(20);
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The motors do not turn This project
Check the battery is connected to EXT+ and EXT-. USB power alone is not enough. - A motor runs the wrong way This project
Swap its two wires, or swapFORWARDandBACKWARDin the code. - Motors do not move, or twitch and the board resets. L293D motor shield
Motors need their own supply on the EXT_PWR terminal, not the Arduino 5 V. Remove the PWR jumper when you do this. Join the grounds. Servo headers use the board 5 V. - A motor is too weak or the driver gets hot. L293D motor shield
Each L293D channel gives about 600 mA continuous. Stalled or large motors need a stronger driver. Check the motor's rated voltage and current, and add a heatsink. - The motor does not spin, or the pin gets hot or the board resets when it is wired straight to a GPIO pin. DC motor
A pin gives about 20 mA and a motor needs far more. Drive it through a transistor or motor driver, with its own supply and a shared ground. - The transistor or pin is damaged by spikes when the motor stops. DC motor
Put a flyback diode across the motor, cathode to the positive side. A motor is a coil and kicks back voltage when switched off. Add a 0.1 uF capacitor across the motor terminals to cut noise. - The battery is wired backwards, or the voltage is too low or too high for the circuit. A fresh AA is about 1.5 V, a LiPo is 3.7 V nominal and 4.2 V full. Battery: AA
Match the plus and minus marks. Check the voltage your parts need. Two AAs make 3 V, four make 6 V. Never feed a LiPo to a 3.3 V chip directly at 4.2 V. - The circuit resets or browns out when a motor or servo starts. A battery has internal resistance, so the voltage sags under load. Battery: AA
Use a fresh pack with enough current rating. Add a 100 uF or larger capacitor near the load. Power motors from their own pack and join the grounds.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.