The Arduino L293D motor driver shield guide is a robobtics project that involves driving various types of motors. The most common types used for robotic applications include DC, servo, and stepper motors.
However, these motors typically cannot be driven directly by Arduino or another microcontroller. This is because of their higher current and power ratings, so motor shields or driver ICs are used instead. These shields or ICs isolate a motor’s power supply and use control logic from the microcontroller circuitry.
One of the most popular motor driver shields used with Arduino is the L293D. The full-featured L293D motor driver shield can control up to four bi-directional DC motors with 8-bit speed selection, two stepper motors, and two servo motors.
An L293D walkthrough
The L293D motor driver shield includes two L293 motor driver ICs and a 74HC595 shift register IC. The shield has several important components.
The motor driver:The L293D is a dual-channel H-bridge motor driver that can control two DC motors or a stepper motor at one time. As there are two L293D ICs on the shield, it’s technically capable of controlling a total of four DC motors.
This is ideal for two and four-wheel robot platforms. The IC consists of two H-bridge to control the motors, each delivering p to 0.6A to a motor.
The shift register:The 74HC595 is an 8-bit serial input and serial/parallel output shift register. It’s used to extend four Arduino GPIO (or another microcontroller) to eight direction control pins for two of the L293D motor driver ICs.
The power supply:The power supply to the shield can be used for both the shield and Arduino or the two can use separate power supplies. If sharing a common power supply, a power jumper must be placed on the shield. The power supply can be provided through Arduino’s USB port, its DC jack, or from the shield’s 2-pin EXT-PWR block.
If separate power supplies are used, it’s important to avoid placing the power jumper on the shield. The shield’s 2-pin EXT-PWR block should be used and Arduino can still be powered by connecting to its USB or DC jack.
If a separate power supply is used, the shield’s power must be input at the 2-pin EXT-PWR block.
Ideally, it’s recommended to keep the power supply of Arduino and the shield separate. But if a common supply is used, the voltage should never exceed 12V.
The shield comes with an on-board LED indicator that lights up if the motor supply is sufficient. It also has a pull-down resistor array that keeps the motor switched off when the power is on.
The output terminals:To connect the DC motors, two five-pin screw terminals are at the edges of the shield. These terminals are labeled as M1, M2, M3, and M4. Those DC motors with voltage ratings of between 4.5 to 25V can be connected to these terminals and will deliver power up to 600 mA.
The same 5-pin screw terminals can also be used to connect two stepper motors by using the M1-M2 and M3-M4 motor ports. The ground terminal at the 5-pin screw headers can connect unipolar stepper motors via the motor’s center taps and the GND terminals.
To connect two servo motors, use the two 3-pin headers at the upper left edge of the shield.
The reset button: The shield has a reset button at its bottom left corner. It is only the Arduino’s reset button brought up at the top of the shield for use
But when the shield is fitted with Arduino, only Arduino’s reset button (which is brought up at the top of the shield) should be used.
DC motors
DC motors are commonly used in robotics and electronics projects. They have two terminals. The motor rotates in one direction when voltage is applied in one polarity and rotates in the other direction when the polarity is reversed.
These motors require a minimum voltage to start rolling and their speed will increase as the applied voltage increases. However, once the maximum rated voltage is reached, any voltage will decrease its speed. A motor driver is used to supply sufficient voltage to the motor, controlling its speed and direction.
Stepper motors
Stepper motors are used to rotate a shaft by a set number of steps. The complete rotation can be divided into a pre-determined number of equal steps that can be programmed into Arduino. The amount of rotation per step depends on the structure of the motor. These motors are used in devices including CNC machines, 3D printers. and scanners.
Servo motors
Servo motors are DC motors with position control. A servo can be rotated to a specific angle by applying the appropriate PWM signal. These motors are suitable for applications such as a robotic arm.
Using the L293 with Arduino
Arduino’s L293D motor driver shield is compatible with Arduino Mega and UNO. The shield can be fitted on top of the Arduino board.
In such cases, six analog pins including pins 2 and 13 remain free for use.
To control the servo motor, use:
- Pins 2, 9, and 10.
To control the DC motors, use:
- Pin 11 for the motor port M1
- Pin 3 for the motor port M2
- Pin 5 for the motor port M3
- Pin 6 for the motor port M4
- Pins 4, 7, 8, and 12 may also all be used
To control the stepper motors, use:
- Pins 11 and 3 for the motor port M1-M2
- Pins 5 and 6 for the motor port M2-M3
- Pins 4, 7, 8, and 12 may also all be used
Controlling the DC motor
The DC motor can be connected to any of the four motor ports. Let’s use the port M4.
To control the DC motor using the L293D motor driver shield, the AFMotor library is useful.
Here’s an example sketch that controls the DC motor using the L293D motor driver shield…
#include <AFMotor.h>
AF_DCMotor motor(4, MOTOR12_64KHZ)
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);
}
The Arduino sketch begins by including the AFMotor.h library. The next line creates an object of the class AF_DCMotor. The instantiation of this object requires mentioning the port, where the motor is connected, and the second parameter, which is the motor speed control frequency.
The second argument can be MOTOR12_2KHZ, MOTOR12_8KHZ, MOTOR12_8KHZ, and MOTOR12_8KHZ for the motors’ port 1 and 2. It can be MOTOR12_8KHZ, MOTOR12_8KHZ, and MOTOR12_8KHZ for the motors’ port 3 and 4.
If left unchecked, it is 1KHZ by default. To connect multiple motors with the shield, separate objects for each motor must be created.
For example…
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
The setSpeed(speed) function sets the speed of the motor. The speed ranges from 0 to 255 (‘0’ = off and 255 = full throttle).
The run(cmd) function sets the run-mode of the motor. Valid values for the cmd are FORWARD to run the motor in the forward direction, BACKWARD to run the motor in the backward direction, and RELEASE to stop the motor.
The motor shield does not implement dynamic braking so the motor can take some time to stop from motion.
Controlling the servo motor
The motor shield breaks out Arduino’s 16-bit PWM output pins 9 and 10 to the edge of the shield, with two 3-pin headers. The servos are powered from Arduino’s onboard 5V regulator so there’s no need to connect an external power source to the EXT_PWR terminal. A servo can be connected to any of these headers.
To control the servo motors, the servo library is helpful. Here’s an example sketch for controlling a servo motor using the L293D motor driver shield.
#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);
}
}
The Arduino sketch begins by importing the servo library. The next line creates an object of the servo class. The method here assigns the servo control pin. The write method is used rotate the servo to set angles.
Controlling the stepper motor
The most popular stepper motors are the 28BYJ-48 unipolar stepper and the NEMA 17 bipolar stepper.
The 28BYJ-48 unipolar stepper is rated at 5V and offers 48 steps per revolution. To connect these motors, an external 5V of power must be supplied to the EXT_PWR terminal.
The NEMA 17 bipolar stepper is rated at 12V and offers 200 steps per revolution. To connect these motors, an external 12V of power must be supplied to the EXT_PWR terminal.
Remember to remove the power jumper on the shield. A stepper motor can be connected to either of the motor terminals’ M1-M2 or M3-M4.
To control the stepper motors, the AFmotor library is useful. Here’s an example sketch for controlling a stepper motor using the L293D motor driver shield.
#include <AFMotor.h>
// Number of steps per output rotation
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);
}
The Arduino sketch begins by including the AFMotor.h library. The next line instantiates an object from the class AF_Stepper. This object requires steps-per-revolution from the motor and a port number to which the motor is connected as parameters.
The setSpeed(rpm) function sets the speed of the motor, where the rpm indicates how many revolutions per minute you want the stepper to turn.
The step(#steps, direction, steptype) function is used to move the motor each time. The parameter #steps is the number of steps — with a direction of either FORWARD or BACKWARD.
The valid values for the stepstyle are:
- SINGLE for energizing one coil at a time,
- DOUBLE for energizing two coils at a time
- INTERLEAVE for alternating between single
- DOUBLE for creating a half-step in between
- MICROSTEP for energizing adjacent coils creating a number of micro-steps between each full step
Filed Under: Tech Articles
Tagged With: dc motor, DRIVER SHEILD, L293D, Motor Drive