How to use the L293D Motor driver shield with Arduino
Hello friends, welcome to the SriTu Hobby blog. Today we are going to talk about a motor control topic. We learned about the L298N motor driver in a previous post. If you have not read it please click this link and read it. That way we could only control two motors. But today we are going to talk about a more powerful motor driver board.
What is this board?
This board is made as a shield. That is, it’s designed to be connected to the Arduino UNO board. Boards made in this way are called shields. So we can easily use them for our projects. See the pictures below.
This motor driver shield mainly consists of two motor driver ICs and one shift register IC. This shield uses the L293D motor driver IC and 74hc595 shift register IC. We can see two terminals on either side to connect the motors. It can connect two servo motors, four gear motors, and two stepper motors. This motor drive shield can be called a more powerful board. We can see an additional terminal to add extra power to this. For that, remember to provide a voltage lower than 12VDC. Ok, let’s check these motors separately. The required components are given below.
- Arduino Uno board x 1 — Our Store / Amazon
- Motor driver shield x 1 — Our Store / Amazon
- Servo motor x 1 — Our Store / Amazon
- Gear motor x 2 — Our Store / Amazon
- Stepper motor x 1– Our Store / Amazon
- Jumper wires x 5(Male to Male) — Our Store / Amazon
Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.
Servo motor test
To do this, connect the components as follows.
Through this shield, we can control two servo motors. For that, we can use D9 and D10 PWM pins. Look at the code below.
- The complete program of this project – Download
#include <Servo.h>
Servo servo1;
void setup() {
servo1.attach(10);
}
void loop() {
for (int a = 0; a <= 180; a++ ) {
servo1.write(a);
delay(20);
}
for (int b = 179; b > 0; b--) {
servo1.write(b);
delay(20);
}
}
The first time we must include the servo library. Use this path for that. Sketch > Include Library > Servo.
After creating a servo object and I’m it’s named “servo1”.This void settings function notifies the library of a PIN we use. The code of this void loop function causes the motor to turn left and right. Click this link for more information on servo motor control. After, we choose the board and the port. Then upload this code.
Gear motor test
Connect two gear motors to the motor control shield. But we can control four-gear motors. Connect four-gear motors if you want. To do this, use the circuit diagram below.
Ok, let’s look at the code below.
- The complete program of this project – Download
#include <AFMotor.h>
AF_DCMotor M1(1);
AF_DCMotor M2(2);
void setup() {
M1.setSpeed(150);
M2.setSpeed(150);
}
void loop() {
M1.run(FORWARD);
M2.run(FORWARD);
delay(2000);
M1.run(BACKWARD);
M2.run(BACKWARD);
delay(2000);
M1.run(RELEASE);
M2.run(RELEASE);
delay(500);
}
For that, we first need a library to control the motor shield. Download it using this link. Insert this library into the Arduino IDE using the steps below.
Next, we need to name the two motors we are using and set up the motor control port. I have named M1 and M2.
AF_DCMotor M1(1);
AF_DCMotor M2(2);
After, we need to give the motor rotation speed.
void setup() {
M1.setSpeed(150);
M2.setSpeed(150);
}
Now we can control these motors with the void loop function.
void loop() {
M1.run(FORWARD);
M2.run(FORWARD);
delay(2000);
M1.run(BACKWARD);
M2.run(BACKWARD);
delay(2000);
M1.run(RELEASE);
M2.run(RELEASE);
delay(500);
}
If we want to rotate the motor forward, use “FORWARD” and if we want to rotate the motor backward, use “BACKWARD”. Use the “RELEASE” keyword to stop this motor. Now select the Arduino board and port. After, click the upload button.
Stepper motor test
We can control two stepper motors using this motor driver shield. But today we use only one stepper motor. Connect the stepper motor using the circuit diagram below.
Let’s look at the code below.
- The complete program of this project – Download
#include <AFMotor.h>
AF_Stepper motor1(48, 2);
void setup() {
motor1.setSpeed(10);
}
void loop() {
//Single coil steps
motor1.step(100, FORWARD, SINGLE);
motor1.step(100, BACKWARD, SINGLE);
//Double coil steps
motor1.step(100, FORWARD, DOUBLE);
motor1.step(100, BACKWARD, DOUBLE);
//Interleave coil steps
motor1.step(100, FORWARD, INTERLEAVE);
motor1.step(100, BACKWARD, INTERLEAVE);
//Micrsostep steps
motor1.step(100, FORWARD, MICROSTEP);
motor1.step(100, BACKWARD, MICROSTEP);
}
This also requires the Motor Driver Shield Control Library. We included this library in the previous code. The first time we need to talk to the stepper motor. Use any name for it. I’m named “motor1”. Next, we need to give the step value and motor port.
AF_Stepper motor1(48, 2);
After, we must set the stepper motor rotation speed.
void setup() {
motor1.setSpeed(10);
}
This void loop code initiates the rotation of the stepper motor.
void loop() {
//Single coil steps
motor1.step(100, FORWARD, SINGLE);
motor1.step(100, BACKWARD, SINGLE);
//Double coil steps
motor1.step(100, FORWARD, DOUBLE);
motor1.step(100, BACKWARD, DOUBLE);
//Interleave coil steps
motor1.step(100, FORWARD, INTERLEAVE);
motor1.step(100, BACKWARD, INTERLEAVE);
//Micrsostep steps
motor1.step(100, FORWARD, MICROSTEP);
motor1.step(100, BACKWARD, MICROSTEP);
}
Next select the board, port, and upload this code to the Arduino board.
Okay, today we discussed three advantages of this motor driver shield. I think you learned that. Please comment below on your issues. Below is the full video of this project. Please watch it and apply this knowledge to your projects. So, we will meet in the next post. Have a good day.
How to use the L293D Motor driver shield with Arduino