Control DC, Stepper & Servo With L293D Motor Driver Shield ...
Maybe your like
If you’ve ever dreamed of building your own robot, remote-controlled car, or any kind of project that moves, you’re going to need to learn how to control motors. There are three main types of motors you’ll come across: DC motors, stepper motors, and servo motors. Each one works differently and is good for different jobs.
The good news is that the L293D Motor Driver Shield makes controlling all these motors with your Arduino simple and easy. It can control:

In this tutorial, you’ll learn everything you need to know about the L293D Motor Driver Shield, control all these different types of motors and get your robot rolling in no time.
Let’s get started!
Hardware Overview
Driver Chipset
The shield uses two important chips that work together to control motors: the L293D motor drivers and the 74HC595 shift register.

The L293D is a dual-channel H-Bridge motor driver. Each chip can control two DC motors or one stepper motor. Since the shield has two of these chips, you can control up to four DC motors or two stepper motors at the same time.
The 74HC595 shift register acts like a helper chip. The Arduino only has a limited number of control pins available, so this chip takes signals from four Arduino pins and expands them to control all eight direction pins needed by the two L293D chips. This clever design saves you from using up all your Arduino’s pins.
Motor Power Connections
Motors need a lot of energy, especially inexpensive ones, since they are less efficient. The first important thing you need to know is what voltage it requires to operate. Different motors need different amounts of voltage. Some tiny hobby motors are designed to run on just 1.5 volts. However, it’s much more common to work with motors that need anywhere from 6 to 12 volts. The good news is that the L293D Motor Driver Shield can handle a wide range of voltages, from 4.5V to 25V, making it suitable for many different types of motors.
You can provide this high-voltage power for the motors in two ways: either through the DC jack on the Arduino board or through the two-terminal block on the shield labeled EXT_PWR.

Method 1
If you want to keep things simple and use just one power supply for both the Arduino and the motors—for example, if you’re building a two-wheel-drive robot powered by one battery—you can simply plug your power supply into either the DC jack on the Arduino like this:

or the two-pin EXT_PWR block on the shield like this:

Then, place the PWR jumper on the motor shield. This method is easier to set up, but there’s an important limitation: you can only use this approach when your motor supply voltage is 12 volts or less.
Method 2
If you prefer to use two separate power supplies—one for the Arduino and one for the motors—connect the Arduino’s power supply to its DC jack, or simply use a USB cable to power it. Then connect the motor power supply to the EXT_PWR block on the shield. When using this method, make sure you remove the PWR jumper from the motor shield.

This is generally the recommended and safer method for powering your motor projects because it keeps the motor power and Arduino power isolated from each other.
Warning:
Never apply power to both the EXT_PWR terminal and the Arduino board at the same time while the PWR jumper is still in place. Doing so will connect two power supplies together, which can cause a short circuit and may damage both the motor shield and the Arduino.
DC Motor Connections
The shield has four motor connection points labeled M1, M2, M3, and M4 along its edge. You can connect up to four DC motors that operate between 4.5V and 25V.

Each motor channel can provide up to 600 milliamps (mA) of continuous current, and can handle short bursts of up to 1.2 amps (A) per channel.
Stepper Motor Connections
You can also use the shield to connect two stepper motors. The first stepper motor connects to M1–M2, and the second to M3–M4.

Servo Motor Connections
The shield also includes two 3-pin headers for connecting servo motors.

There’s one thing to watch out for with servos, though. They get their power directly from the Arduino’s 5-volt supply instead of from the external motor power supply. This isn’t ideal because servos can draw quite a bit of power, which might cause the Arduino’s voltage regulator to get too hot or create electrical interference. The shield does have a small 100µF capacitor to help with this problem, but it’s better to be safe. If you want to use servos with this shield, stick to small, lightweight ones like the popular SG90 servo.
Bonus Features
The shield also comes with several useful features:

- It has a pulldown resistor array that keeps the motors turned off when you first power everything up, preventing any unexpected movement.
- There’s also a small on-board LED light that tells you when the motor power supply is working correctly.
- The shield has a RESET button conveniently placed on top, which is just an easy-to-reach version of the Arduino’s own reset button.
- Along the bottom right corner, you’ll find connections for all six analog pins from the Arduino, plus power and ground connections. You can add pin headers here if you want to connect sensors or other devices while using the shield.
Arduino to Shield Pin Connections
It’s important to know which Arduino pins the shield needs so you don’t accidentally try to use them for something else.
- For controlling DC motors and stepper motors, the shield uses digital pins 3, 4, 5, 6, 7, 8, 11, and 12.
- For the servo motors, it uses pins 9 and 10.
- Pins 2, 13, and all the analog pins remain completely free, so you can still use them for other projects or sensors while using this motor shield.
Installing the AFMotor Library
Before we can use the motor shield, we need to install AFMotor library. This library allows us to easily control DC motors, stepper motors, and servo motors using simple commands.
To install the library,
- First open your Arduino IDE program. Then click on the Library Manager icon on the left sidebar.
- Type “Adafruit Motor Shield” in the search box to filter your results.
- Look for the Adafruit Motor Shield library (V1 Firmware) by Adafruit.
- Click the Install button to add it to your Arduino IDE.

Experiment 1 – Controlling a DC Motor Using the L293D Shield
Now that you’ve got everything set up, let’s begin our first experiment. In this experiment, we’ll learn how to control a DC motor using the L293D Motor Driver Shield.
Wiring
Start by carefully mounting the motor shield on top of your Arduino board.
Next, you’ll need to provide power for your motor. For this experiment, we’re using BO motors, which are commonly used in two-wheel-drive robots. These motors work with anywhere from 3 to 12 volts, so we’re going to connect a 9-volt battery or power supply to the EXT_PWR terminal on the shield.
Here’s an important step you don’t want to miss. You need to remove the PWR jumper before applying power. This keeps the motor power separate from the Arduino’s power supply and helps prevent damage.
Finally, take your motor’s wires and connect them to one of the terminals marked M1, M2, M3, or M4 on the shield. For our example, we’re plugging it into M4.

Arduino Code
The following sketch demonstrates how to control both the speed and direction of your DC motor. You can use this program as a starting point for your own experiments and projects.
The sketch makes the motor start spinning in one direction and gradually accelerate to its maximum speed. Then it slowly decelerates until it stops completely. After a brief pause, the motor reverses direction and repeats the entire process, spinning the opposite way. This cycle continues over and over again.
#include <AFMotor.h> AF_DCMotor motor(4); void setup() { } void loop() { // Turn on motor motor.run(FORWARD); // Accelerate from zero to maximum speed for (int i = 0; i < 255; i++) { motor.setSpeed(i); delay(10); } // Decelerate from maximum speed to zero for (int i = 255; i != 0; i--) { motor.setSpeed(i); delay(10); } // Now turn off motor motor.run(RELEASE); delay(500); // Now change motor direction motor.run(BACKWARD); // Accelerate from zero to maximum speed for (int i = 0; i < 255; i++) { motor.setSpeed(i); delay(10); } // Decelerate from maximum speed to zero for (int i = 255; i != 0; i--) { motor.setSpeed(i); delay(10); } // Now turn off motor motor.run(RELEASE); delay(500); }Code Explanation
The program begins by including the AFMotor.h library:
#include <AFMotor.h>Next, we create a motor object using the AF_DCMotor class. When setting this up, you need to tell it which port your motor is plugged into on the shield. If your motor is connected to port M1, you use the number 1. For M2, you use 2, and so on. In this example, we’re using port M4, so we use the number 4.
AF_DCMotor motor(4);If you want to control more than one motor at the same time, you simply create additional motor objects for each one. For instance, if you had motors plugged into both M1 and M2, you would create two separate motor objects with their respective port numbers.
AF_DCMotor motorA(1); AF_DCMotor motorB(2);Once the object is created, the program uses two important commands to control the motor.
setSpeed(speed) – This function controls how fast the motor spins. The speed value can range from 0 to 255, where 0 means the motor is completely off and 255 means it’s running at full speed. You can change this value whenever you want throughout your program.
run(command) – This function tells the motor which direction to spin. The possible commands are:
- FORWARD – Makes the motor spin forward.
- BACKWARD – Makes the motor spin in reverse.
- RELEASE – Stops the motor completely, which works the same as setting the speed to 0. Keep in mind that when you stop the motor this way, it won’t stop instantly because the shield doesn’t have a braking system. The motor will gradually coast to a stop.
Experiment 2 – Controlling a 28BYJ-48 Unipolar Stepper Motor with the L293D Shield
In this experiment, we’ll learn how to control a unipolar stepper motor, such as the popular 28BYJ-48, using the L293D motor driver shield. The 28BYJ-48 is a small 5V stepper motor that takes 2048 steps to complete one full rotation.
Wiring
To connect your stepper motor, you can use either the M1–M2 terminals (port #1) or the M3–M4 terminals (port #2) on the L293D Motor Driver Shield. In this example, we’ll connect the motor to the M3–M4 terminals. One phase of the stepper motor should be connected to the M3 terminal, and the other phase should be connected to the M4 terminal. If your stepper motor has a common wire (which is typical in unipolar stepper motors), you can leave this wire unconnected.
To power the stepper motor properly, you’ll need to connect an external 5V power supply to the EXT_PWR terminal on your shield.
Just like we did before, make sure to remove the PWR jumper from the shield before you turn everything on. This step is really important to avoid creating a short circuit between your power supplies.

Arduino Code
The following sketch demonstrates how to control a 28BYJ-48 unipolar stepper motor in several different ways.
#include <AFMotor.h> // Number of steps per output rotation // Change this as per your motor's specification const int stepsPerRevolution = 2048; // connect motor to port #2 (M3 and M4) AF_Stepper motor(stepsPerRevolution, 2); void setup() { motor.setSpeed(10); // 10 rpm } void loop() { // Single coil steps motor.step(stepsPerRevolution, FORWARD, SINGLE); motor.step(stepsPerRevolution, BACKWARD, SINGLE); // Double coil steps motor.step(stepsPerRevolution, FORWARD, DOUBLE); motor.step(stepsPerRevolution, BACKWARD, DOUBLE); // Interleave coil steps motor.step(stepsPerRevolution, FORWARD, INTERLEAVE); motor.step(stepsPerRevolution, BACKWARD, INTERLEAVE); // Micrsostep steps motor.step(stepsPerRevolution, FORWARD, MICROSTEP); motor.step(stepsPerRevolution, BACKWARD, MICROSTEP); }Code Explanation
The sketch starts by including the AFMotor.h library, just like we did in the first experiment.
#include <AFMotor.h>Next, we create a variable called stepsPerRevolution that represents how many steps the motor needs to take to complete one full rotation. Since the 28BYJ-48 requires 2048 steps for a complete rotation, we set this value to 2048.
const int stepsPerRevolution = 2048;After that, we create a stepper motor object using the AF_Stepper class. When creating this object, we provide two pieces of information: the number of steps per revolution and the port number where the motor is connected. In this example, the motor is connected to port 2, which corresponds to M3–M4.
AF_Stepper motor(stepsPerRevolution, 2);Inside the setup section of the sketch, the motor’s speed is set using the setSpeed() function. This function determines how fast the motor spins. Instead of using a speed scale like 0 to 255, stepper motors use something called RPM, which stands for revolutions per minute. This tells the motor how many complete rotations you want it to make in one minute.
void setup() { motor.setSpeed(10); // 10 rpm }In the loop section, the program repeatedly calls the step() function to move the motor in different ways. Each time this function is called, we need to tell it three things: how many steps to take, which direction to go (either FORWARD or BACKWARD), and what stepping mode to use.
There are several stepping modes available:
- SINGLE – Only one coil of the motor is energized at a time. This uses less power but provides less torque.
- DOUBLE – Two coils are energized at once, giving the motor more torque for heavier loads.
- INTERLEAVE – This mode alternates between single and double steps to get twice the resolution (but it also makes it run at half the speed).
- MICROSTEP – In this mode, the coils are PWM’d to create smooth motion between steps.
Experiment 3 – Controlling a NEMA 17 Bipolar Stepper Motor with the L293D Shield
For our third experiment, we’re going to control a NEMA 17 bipolar stepper motor, which is very popular in 3D printers and CNC machines. It operates at 12V and takes 200 steps to complete one full rotation.
Wiring
Just like with the previous motor, we’re going to connect the NEMA 17 to the M3-M4 terminals (port #2).
To power this motor, we’ll need to connect an external 12V power supply to the EXT_PWR terminal on your motor shield. Once again, don’t forget to remove the PWR jumper before powering everything up.

Arduino Code
The following sketch demonstrates how to control a NEMA 17 bipolar stepper motor.
#include <AFMotor.h> // Number of steps per output rotation // Change this as per your motor's specification const int stepsPerRevolution = 200; // connect motor to port #2 (M3 and M4) AF_Stepper motor(stepsPerRevolution, 2); void setup() { motor.setSpeed(10); // 10 rpm } void loop() { // Single coil steps motor.step(stepsPerRevolution, FORWARD, SINGLE); motor.step(stepsPerRevolution, BACKWARD, SINGLE); // Double coil steps motor.step(stepsPerRevolution, FORWARD, DOUBLE); motor.step(stepsPerRevolution, BACKWARD, DOUBLE); // Interleave coil steps motor.step(stepsPerRevolution, FORWARD, INTERLEAVE); motor.step(stepsPerRevolution, BACKWARD, INTERLEAVE); // Micrsostep steps motor.step(stepsPerRevolution, FORWARD, MICROSTEP); motor.step(stepsPerRevolution, BACKWARD, MICROSTEP); }Code Explanation
You’ve probably noticed that this program looks almost identical to the previous one. The only difference you’ll see is in the stepsPerRevolution value. In the previous experiment, we set it to 2048 because that’s what the 28BYJ-48 motor needed. Now, for the NEMA 17 motor, we set it to 200. Everything else in the program works exactly the same way, using the same setSpeed() and step() functions to control the motor’s speed, direction, and stepping style.
Experiment 4 – Controlling a Servo Motor Using the L293D Shield
For our final experiment, we’re going to learn how to control a servo motor using the L293D shield. Servo motors are perfect for things like robot arms or moving parts that need exact positioning.
Wiring
The shield has two special 3-pin headers that make it easy to connect servo motors. These headers are connected to the Arduino’s PWM output pins D9 and D10. Pin D10 controls Servo 1, while pin D9 controls Servo 2.
One nice thing about working with servo motors is that they get their power directly from the Arduino’s 5V supply. This means you don’t need to worry about connecting an external power supply to the EXT_PWR terminal like we did in the previous experiments.

Arduino Code
The program below is based on Arduino’s built-in Sweep Example, which smoothly moves the shaft of a servo motor back and forth across 180 degrees.
#include <Servo.h> int servoPin = 10; Servo servo; // create servo object to control a servo void setup() { servo.attach(servoPin); } void loop() { // sweeps from 0 degrees to 180 degrees for (int angle = 0; angle < 180; angle++) { servo.write(angle); delay(15); } // sweeps from 180 degrees to 0 degrees for (int angle = 180; angle > 0; angle--) { servo.write(angle); delay(15); } }Code Explanation
The sketch begins by including the Servo library that comes with Arduino IDE. This library gives us easy commands to tell the servo exactly where we want it to move.
#include <Servo.h>Next, we tell Arduino which pin our servo is connected to. For this example, we’re using pin 10.
int servoPin = 10;Then, we create a servo object that we’ll use to communicate with and control the servo motor.
Servo servo;In the setup() function, we connect our servo object to the pin we chose earlier. This tells Arduino which pin should send signals to the servo.
servo.attach(servoPin);Now, in the loop() function, we first move the servo from 0 degrees to 180 degrees. We use a for loop that increases the angle by one degree at a time. Each time through the loop, the command servo.write(angle); moves the servo to its new position. To keep things smooth, we add a small 15-millisecond pause after each movement.
// sweeps from 0 degrees to 180 degrees for (int angle = 0; angle < 180; angle++) { servo.write(angle); delay(15); }Once the servo reaches 180 degrees, we make it move back to 0 degrees. We do this with another for loop that decreases the angle one degree at a time until we get back to 0.
// sweeps from 180 degrees to 0 degrees for (int angle = 180; angle > 0; angle--) { servo.write(angle); delay(15); }After reaching 0 degrees, the whole process starts over again. This makes the servo continuously sweep back and forth between 0 and 180 degrees.
Tag » Arduino Motor Shield 4 Dc Motors
-
Using DC Motors | Adafruit Motor Shield
-
Controlling A DC Motor With Motor Shield Rev3
-
Arduino L293D Motor Driver Shield Tutorial
-
Using L293D 4 DC Motors Shield For Arduino UNO And Mega
-
Four - Channels Motor Shield For Arduino
-
Control Several Motors With Motor Shield V1 And Arduino - AranaCorp
-
Arduino Motor Shield Tutorial : 6 Steps (with Pictures) - Instructables
-
Arduino Motor Shield 4 Channel L293D H-Bridge Dc Motor Control ...
-
Motor Shield 4 Channel L293D H-Bridge Dc Motor Control For Arduino
-
L293D DC Motor/Stepper/Servo Driver Shield For Arduino
-
[PDF] L293D Based Arduino Motor Shield
-
4-Channel Motor Shield (for Arduino) - Canada Robotix
-
Quad DC Motor Driver Shield For Arduino - DFRobot
-
4-Channels L293D Motor Shield For Arduino - Makerfabs