If you are planning on assembling your new robot, you will eventually want to learn about controlling variety of motors like DC motors, Stepper motors & servos. One of the easiest and inexpensive way to do that is to interface L293D Motor Driver Shield with Arduino. It’s a full-featured motor shield – perfect for many robot and CNC projects.

It can drive :

  • 4 bi-directional DC motors with 8-bit speed selection(0-255)
  • 2 stepper motors (unipolar or bipolar) with single coil, double coil, interleaved or micro-stepping.
  • 2 servo motors

L293D Motor Driver & 74HC595 Shift Register

L293D Motor Driver Shield Chipset

The L293D is a dual-channel H-Bridge motor driver capable of driving a pair of DC motors or single stepper motor.

As the shield comes with two L293D motor driver chipsets, that means it can individually drive up to four DC motors making it ideal for building four-wheel robot platforms.

The shield offers total 4 H-Bridges and each H-bridge can deliver up to 0.6A to the motor.

The shield also comes with a 74HC595 shift register that extends 4 digital pins of the Arduino to the 8 direction control pins of two L293D chips.

Power Supply

L293D Motor Driver Shield Power Supply Terminals

There exists three scenarios when it comes to supplying power for the motors through shield.

  • Single DC power supply for both Arduino and motors:
    If you would like to have a single DC power supply for both Arduino and motors, simply plug it into the DC jack on the Arduino or the 2-pin EXT_PWR block on the shield. Place the power jumper on the motor shield.
    You can employ this method only when motor supply voltage is less than 12V.
  • (Recommended) Arduino powered through USB and motors through a DC power supply:
    If you would like to have the Arduino powered off of USB and the motors powered off of a DC power supply, plug in the USB cable. Then connect the motor supply to the EXT_PWR block on the shield. Do not place the jumper on the shield.
  • Two separate DC power supplies for the Arduino and motors:
    If you would like to have 2 separate DC power supplies for the Arduino and motors. Plug in the supply for the Arduino into the DC jack, and connect the motor supply to the EXT_PWR block. Make sure the jumper is removed from the motor shield.

WARNING

DO NOT supply power to the EXT_PWR input when jumper is in place. It may damage the motor shield and also your Arduino!

As a bonus, the shield offers below features:

  • The shield comes with a pulldown resistor array to keep motors switched off during power-up.
  • The on-board LED indicates the motor power supply is Okay. If it is not lit, the motors will not run.
  • The RESET is nothing but Arduino’s reset button. It just brought up top for convenience.

Output Terminals

L293D Motor Driver Shield Output Terminals Pinout

The output channels of both the L293D chips are broken out to the edge of the shield with two 5-pin screw terminals viz. M1, M2, M3 & M4. You can connect four DC motors having voltages between 4.5 to 25V to these terminals.

Each channel on the module can deliver up to 600mA to the DC motor. However, the amount of current supplied to the motor depends on system’s power supply.

You can also connect two stepper motors to output terminals. One stepper motor to motor port M1-M2 and other to M3-M4.

The GND terminal is also provided if you happen to have a unipolar stepper motor. You can connect the center taps of both stepper motors to this terminal.

The shield brings out the 16bit PWM output lines to two 3-pin headers to which you can connect two servo motors.

Unused pins on L293D shield

Digital pins #2, #13 and analog pins A0-A5 are not used by the shield.

Analog pins are broken out in the bottom right corner where pin 2 has a small breakout. If you want to use these pins, you can connect some headers to it.

Installing AFMotor Library

In order to communicate with the shield, we need to install AFMotor.h library so that we can issue simple commands to control DC, stepper & servo motors.

To install the library navigate to the Sketch > Include Library > Manage Libraries… Wait for Library Manager to download libraries index and update list of installed libraries.

Arduino Library Installation - Selecting Manage Libraries in Arduino IDE

Filter your search by typing ‘motor shield’. There should be a couple entries. Look for Adafruit Motor Shield library(V1 Firmware) by Adafruit. Click on that entry, and then select Install.

Installing AFMotor Library

Driving DC Motors with L293D Shield

Now that we know everything about the shield, we can begin hooking it up to our Arduino!

Start by plugging the shield on the top of the Arduino.

Next, connect power supply to the motors. Although you can connect DC motors having voltages between 4.5 to 25V to the shield, in our experiment we are using DC Motors that are rated for 9V. So, we will connect external 9V power supply to the EXT_PWR terminal.

Now, connect the motor to either M1, M2, M3 or M4 motor terminals. In our experiment we are connecting it to M4.

Wiring DC Motor to L293D Motor Shield & Arduino
Wiring DC Motor to L293D Motor Shield & Arduino

The following sketch will give you complete understanding on how to control speed and spinning direction of a DC motor with L293D motor driver shield and can serve as the basis for more practical experiments and projects.

#include <AFMotor.h>

AF_DCMotor motor(4);

void setup() 
{
	//Set initial speed of the motor & stop
	motor.setSpeed(200);
	motor.run(RELEASE);
}

void loop() 
{
	uint8_t i;

	// Turn on motor
	motor.run(FORWARD);
	
	// Accelerate from zero to maximum speed
	for (i=0; i<255; i++) 
	{
		motor.setSpeed(i);  
		delay(10);
	}
	
	// Decelerate from maximum speed to zero
	for (i=255; i!=0; i--) 
	{
		motor.setSpeed(i);  
		delay(10);
	}

	// Now change motor direction
	motor.run(BACKWARD);
	
	// Accelerate from zero to maximum speed
	for (i=0; i<255; i++) 
	{
		motor.setSpeed(i);  
		delay(10);
	}

	// Decelerate from maximum speed to zero
	for (i=255; i!=0; i--) 
	{
		motor.setSpeed(i);  
		delay(10);
	}

	// Now turn off motor
	motor.run(RELEASE);
	delay(1000);
}

Code Explanation:

The sketch starts by including the AFMotor.h library.

The second line AF_DCMotor motor(motorPort#); creates an object of library. Here you need to declare the motor port number to which motor is connected. For port M1 write 1, for M2 write 2 and so on.

If you want to connect multiple motors to the shield, create separate object for each motor. For example, following code snippet creates two AFmotor objects.

AF_DCMotor motor1(1);
AF_DCMotor motor2(2);

In setup and loop section of the code we simply call below two functions to control the speed and spinning direction of a motor.

  • setSpeed(speed) function sets the speed of the motor. The speed ranges from 0 to 255 with 0 being off and 255 as full throttle. You can set the speed whenever you want in program.
  • run(cmd) function sets the run-mode of 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.

Driving Stepper Motors with L293D Shield

Let’s connect stepper motor to the L293D shield. Start by plugging the shield on the top of the Arduino.

For 28BYJ-48 unipolar stepper

If you are using 28BYJ-48 unipolar stepper, those motors are rated at 5V and offer 48 steps per revolution. So, connect external 5V power supply to the EXT_PWR terminal.

Remember to remove the PWR jumper.

Now, connect the motor to either M1-M2(port#1) or M3-M4(port#2) stepper motor terminals. In our experiment we are connecting it to M3-M4.

Wiring Unipolar Stepper Motor to L293D Motor Shield & Arduino
Wiring Unipolar Stepper Motor to L293D Motor Shield & Arduino

For NEMA 17 bipolar stepper

If you are using NEMA 17 bipolar stepper, those motors are rated at 12V and offer 200 steps per revolution. So, connect external 12V power supply to the EXT_PWR terminal.

Remember to remove the PWR jumper.

Now, connect the motor to either M1-M2(port#1) or M3-M4(port#2) stepper motor terminals. In our experiment we are connecting it to M3-M4.

Wiring Bipolar Stepper Motor to L293D Motor Shield & Arduino
Wiring Bipolar Stepper Motor to L293D Motor Shield & Arduino

Arduino Code

The following sketch will give you complete understanding on how to control a unipolar or bipolar stepper motor with L293D shield and is same for both the motors except stepsPerRevolution parameter.

Change this parameter as per your motor’s specification before trying the sketch out. For example, for NEMA 17 set it to 200 and for 28BYJ-48 set it to 48.

#include <AFMotor.h>

// Number of steps per output rotation
// Change this as per your motor's specification
const int stepsPerRevolution = 48;

// connect motor to port #2 (M3 and M4)
AF_Stepper motor(stepsPerRevolution, 2);

void setup() {
  Serial.begin(9600);
  Serial.println("Stepper test!");

  motor.setSpeed(10);  // 10 rpm   
}

void loop() {
  Serial.println("Single coil steps");
  motor.step(100, FORWARD, SINGLE); 
  motor.step(100, BACKWARD, SINGLE); 

  Serial.println("Double coil steps");
  motor.step(100, FORWARD, DOUBLE); 
  motor.step(100, BACKWARD, DOUBLE);

  Serial.println("Interleave coil steps");
  motor.step(100, FORWARD, INTERLEAVE); 
  motor.step(100, BACKWARD, INTERLEAVE); 

  Serial.println("Micrsostep steps");
  motor.step(100, FORWARD, MICROSTEP); 
  motor.step(100, BACKWARD, MICROSTEP); 
}

Code Explanation:

The sketch starts by including the AFMotor.h library.

The second line AF_Stepper motor(48, 2); creates an object of library. Here you need to pass steps-per-revolution of motor and port number to which motor is connected, as parameters.

In setup and loop section of the code we simply call below two functions to control the speed and spinning direction of a motor.

  • setSpeed(rpm) function sets the speed of the motor, where rpm is how many revolutions per minute you want the stepper to turn.
  • step(#steps, direction, steptype) function is called every time you want the motor to move. #steps is how many steps you’d like it to take. direction is either FORWARD or BACKWARD, and valid values for stepstyle 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.

Driving Servo Motors with L293D Shield

Driving the servos with L293D shield is as easy as pie.

The motor shield actually breaks out Arduino’s 16bit PWM output pins #9 & #10 to the edge of the shield with two 3-pin headers.

Power for the Servos comes from the Arduino’s on-board 5V regulator, so you don’t have to connect anything to the EXT_PWR terminal.

Wiring Servo Motor to L293D Motor Shield & Arduino
Wiring Servo Motor to L293D Motor Shield & Arduino

As we are using the onboard PWM pins, the sketch uses IDE’s built in Servo library.

#include <Servo.h> 

Servo myservo;	// create servo object to control a servo
int pos = 0;	// variable to store the servo position

void setup() 
{
	// attaches the servo on pin 10 to the servo object
	myservo.attach(10);   
}

void loop() 
{
	// sweeps from 0 degrees to 180 degrees
	for(pos = 0; pos <= 180; pos += 1) 
	{
		myservo.write(pos);
		delay(15);
	}
	// sweeps from 180 degrees to 0 degrees
	for(pos = 180; pos>=0; pos-=1)
	{
		myservo.write(pos);
		delay(15);
	}
}