Control DC, Stepper & Servo with L293D Motor Driver Shield & Arduino (2024)

If you intend to build your own robot, you’ll need to control various motors like DC motors, stepper motors, and servos, and there’s no better option than the L293D Motor Driver Shield. It is capable of controlling all of these motors; there is no need for additional modules.

The L293D motor driver shield has all the bells and whistles required for a wide variety of low- to moderate-complexity projects. It can control:

Control DC, Stepper & Servo with L293D Motor Driver Shield & Arduino (1)

Hardware Overview

Driver Chipset

The brains of the shield are two L293D motor drivers and a 74HC595 shift register.

Control DC, Stepper & Servo with L293D Motor Driver Shield & Arduino (2)

The L293D is a dual-channel H-Bridge motor driver that can control two DC motors or a single stepper motor. Because the shield includes two such motor drivers, it can control up to four DC motors or two stepper motors.

The 74HC595 shift register, on the other hand, extends the Arduino’s four digital pins to the eight direction control pins of two L293D chips.

Motor Power Connections

The shield supports a motor voltage range of 4.5 to 25 volts. This power can be shared with the Arduino or used separately. In order to choose between the two, a special jumper labeled PWR is provided near the two-terminal power connector.

Control DC, Stepper & Servo with L293D Motor Driver Shield & Arduino (3)

When the jumper is in place, power is supplied to the motors via the Arduino DC power jack. In this case, the motors and Arduino are not physically isolated from each other. This method makes the shield easier to use because it requires only one power supply; however, you can use this method only when the motor supply voltage is less than 12V.

When the jumper is removed, the motor power is disconnected from the Arduino, allowing the motors to be physically isolated from the Arduino. In this case, however, you must provide a separate motor power supply to the two-terminal power connector labeled EXT_PWR.

Warning:

Applying power to the EXT_PWR two-terminal power connector while the jumper is in place will short the two power supplies together, potentially damaging the motor shield as well as the Arduino!

DC Motor Connections

The output channels of both L293D ICs are broken out to the edge of the shield with two 5-pin screw terminals labeled M1, M2, M3, and M4. A total of four DC motors operating at 4.5 – 25V can be connected to these terminals.

Control DC, Stepper & Servo with L293D Motor Driver Shield & Arduino (4)

Each channel on the module can supply up to 600 mA (1.2A peak) current to the DC motor. The amount of current supplied to the motor, however, depends on the capacity of the motor power supply.

Stepper Motor Connections

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

Control DC, Stepper & Servo with L293D Motor Driver Shield & Arduino (5)

If you have a 5-wire unipolar stepper motor, connect the center tap wire to the center ground terminal.

Servo Motor Connections

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

Control DC, Stepper & Servo with L293D Motor Driver Shield & Arduino (6)

Unfortunately, the servo motors are powered directly from the Arduino’s 5V supply, which is generally a bad idea. Doing so can cause the Arduino’s on-board 5V regulator to overheat and can also introduce electrical noise on the 5V supply. On the plus side, it has a 100uF capacitor on these power pins, which helps a little.

So, if you want to use this feature, only use small servos like the SG90.

Bonus Features

Among the shield’s perks are the following:

Control DC, Stepper & Servo with L293D Motor Driver Shield & Arduino (7)

The shield includes a pulldown resistor array to keep motors off during power-up.

The on-board LED indicates that the motor power supply is working properly. The motors will not run if it is not lit.

The RESET button is nothing but Arduino’s reset button. It has been brought to the top for easy access.

Six analog pins (A0 to A5), as well as 5V and ground connections, are provided in the bottom right corner. You can populate these with headers, making them useful for connecting various sensors.

Arduino to Shield Pin Connections

For DC and stepper motor control, the shield makes use of pins D3, D4, D5, D6, D7, D8, D11, and D12.

D9 and D10 are used to control the servo motors. D10 is connected to Servo 1, while D9 is connected to Servo 2.

Please note that the shield does not use the D2 or D13 pins.

Installing AFMotor Library

To communicate with the shield, we must first install the AFMotor.h library. This will let us control DC, stepper, and servo motors with simple commands.

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

Filter your search by entering ‘motor shield’. Look for Adafruit Motor Shield library(V1 Firmware) by Adafruit. Click on that entry and then choose Install.

Control DC, Stepper & Servo with L293D Motor Driver Shield & Arduino (9)

Experiment 1 – Driving DC Motors with L293D Shield

Now that we’ve learned everything there is to know about the shield, we can start connecting it to the Arduino!

Wiring

Begin by mounting the motor shield on top of an Arduino.

Next, connect the motor power supply. In our experiment, we are using DC gearbox motors, also called “TT” motors, which are often found in two-wheel-drive robots. They are rated for 3 to 12V. So, we will connect the external 9V power supply to the EXT_PWR terminal.

Finally, connect the motor to one of the M1, M2, M3, or M4 terminals. We are connecting it to M4.

Control DC, Stepper & Servo with L293D Motor Driver Shield & Arduino (10)

Arduino Code

The sketch below will show you how to control the speed and spinning direction of a DC motor using the L293D motor driver shield and can serve as the basis for more practical experiments and projects.

The sketch accelerates the DC motor in one direction before slowing down to stop. After one revolution, the motor reverses its spinning direction and repeats the process.

#include <AFMotor.h>AF_DCMotor motor(4);void setup() {//Set initial speed of the motor & stopmotor.setSpeed(200);motor.run(RELEASE);}void loop() {uint8_t i;// Turn on motormotor.run(FORWARD);// Accelerate from zero to maximum speedfor (i=0; i<255; i++) {motor.setSpeed(i); delay(10);}// Decelerate from maximum speed to zerofor (i=255; i!=0; i--) {motor.setSpeed(i); delay(10);}// Now change motor directionmotor.run(BACKWARD);// Accelerate from zero to maximum speedfor (i=0; i<255; i++) {motor.setSpeed(i); delay(10);}// Decelerate from maximum speed to zerofor (i=255; i!=0; i--) {motor.setSpeed(i); delay(10);}// Now turn off motormotor.run(RELEASE);delay(1000);}

Code Explanation:

The sketch begins by including the AFMotor.h library.

The second line AF_DCMotor motor(motorPort#) creates a library object. Here, you must specify the motor port number to which the motor is connected. Write 1 for port M1, 2 for port M2, and so on.

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

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

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

  • setSpeed(speed) function controls the motor’s speed. The speed ranges from 0 to 255, with 0 being off and 255 being full throttle. In the program, you can change the speed whenever you want.
  • run(cmd) function controls the motor’s spinning direction. The following are valid cmd values:
    • FORWARD – spins the motor forward.
    • BACKWARD – spins the motor backwards.
    • RELEASE – This stops the motor and is equivalent to setSpeed(0). Because the motor shield lacks dynamic braking, the motor may take some time to stop spinning.

Experiment 2 – Driving Stepper Motors with L293D Shield

Our next experiment will involve connecting a stepper motor to the L293D shield. Begin by mounting the motor shield on top of an Arduino.

Wiring for the 28BYJ-48 unipolar stepper

28BYJ-48 unipolar stepper motors are rated at 5V and provide 48 steps per revolution. So, connect an external 5V power supply to the EXT_PWR terminal.

Don’t forget to remove the PWR jumper.

Finally, connect the motor to the stepper motor terminals M1-M2 (port #1) or M3-M4 (port #2). We are connecting it to M3-M4.

Control DC, Stepper & Servo with L293D Motor Driver Shield & Arduino (11)

Wiring for the NEMA 17 bipolar stepper

NEMA 17 bipolar stepper motors are rated at 12V and provide 200 steps per revolution. So, connect an external 12V power supply to the EXT_PWR terminal.

Don’t forget to remove the PWR jumper.

Finally, connect the motor to the stepper motor terminals M1-M2 (port #1) or M3-M4 (port #2). We are connecting it to M3-M4.

Control DC, Stepper & Servo with L293D Motor Driver Shield & Arduino (12)

Arduino Code

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

Before running the sketch, modify this parameter to match the specifications of your motor. For example, set it to 200 for NEMA 17 and 48 for 28BYJ-48.

#include <AFMotor.h>// Number of steps per output rotation// Change this as per your motor's specificationconst 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 begins by including the AFMotor.h library.

The second line AF_Stepper motor(48, 2) creates a library object. Here, you must specify the motor’s steps-per-revolution and the port number to which the motor is connected.

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

  • setSpeed(rpm) sets the motor’s speed, where rpm is the number of 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 the number of steps you want it to take. The direction can be FORWARD or BACKWARD, and the stepstyle can be any of the following:
    • 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 the speed is reduced by half due to the extra half-step.
    • MICROSTEP – Between each full step, adjacent coils are ramped up and down to create a number of micro-steps. This produces finer resolution and smoother rotation, but at the expense of torque.

Experiment 3 – Driving Servo Motors with L293D Shield

Our final experiment will involve driving a servo motor with the L293D shield.

Wiring

The shield brings out the 16-bit PWM output pins D9 and D10 to two 3-pin headers, which you can use to connect two servo motors. Keep in mind that D10 is connected to Servo 1, whereas D9 is connected to Servo 2.

The servo motors are powered directly from the Arduino’s 5V supply, so there is no need to connect anything to the EXT_PWR terminal.

Control DC, Stepper & Servo with L293D Motor Driver Shield & Arduino (13)

Arduino Code

This sketch is based on the standard sweep sketch found in the Arduino Examples under servo. It sweeps the shaft of a servo motor back and forth across 180 degrees.

#include <Servo.h> Servo myservo;// create servo object to control a servoint pos = 0;// variable to store the servo positionvoid setup() {// attaches the servo on pin 10 to the servo objectmyservo.attach(10); }void loop() {// sweeps from 0 degrees to 180 degreesfor(pos = 0; pos <= 180; pos += 1) {myservo.write(pos);delay(15);}// sweeps from 180 degrees to 0 degreesfor(pos = 180; pos>=0; pos-=1){myservo.write(pos);delay(15);}}
Control DC, Stepper & Servo with L293D Motor Driver Shield & Arduino (2024)
Top Articles
Starbucks Marketing Strategy: An In-Depth Look at What Makes Starbucks’ Marketing so Effective
2565 S Signal Butte Rd #37, Mesa, AZ 85209 - MLS 6745536 - Coldwell Banker
What Did Bimbo Airhead Reply When Asked
Devin Mansen Obituary
Public Opinion Obituaries Chambersburg Pa
Alpha Kenny Buddy - Songs, Events and Music Stats | Viberate.com
Pj Ferry Schedule
Moviesda Dubbed Tamil Movies
Cinepacks.store
Irving Hac
Oppenheimer & Co. Inc. Buys Shares of 798,472 AST SpaceMobile, Inc. (NASDAQ:ASTS)
Legacy First National Bank
Hallelu-JaH - Psalm 119 - inleiding
Es.cvs.com/Otchs/Devoted
Robert Malone é o inventor da vacina mRNA e está certo sobre vacinação de crianças #boato
Ivegore Machete Mutolation
Accuradio Unblocked
Hca Florida Middleburg Emergency Reviews
Walmart Windshield Wiper Blades
DoorDash, Inc. (DASH) Stock Price, Quote & News - Stock Analysis
Committees Of Correspondence | Encyclopedia.com
Jinx Chapter 24: Release Date, Spoilers & Where To Read - OtakuKart
Cta Bus Tracker 77
Ford F-350 Models Trim Levels and Packages
Doublelist Paducah Ky
‘The Boogeyman’ Review: A Minor But Effectively Nerve-Jangling Stephen King Adaptation
Craigs List Tallahassee
Redfin Skagit County
Apartments / Housing For Rent near Lake Placid, FL - craigslist
Does Hunter Schafer Have A Dick
Koninklijk Theater Tuschinski
Publix Near 12401 International Drive
Yale College Confidential 2027
Bfsfcu Truecar
Jamielizzz Leaked
Citibank Branch Locations In Orlando Florida
L'alternativa - co*cktail Bar On The Pier
Chapaeva Age
Daily Journal Obituary Kankakee
10 Most Ridiculously Expensive Haircuts Of All Time in 2024 - Financesonline.com
Imperialism Flocabulary Quiz Answers
Greater Keene Men's Softball
Ise-Vm-K9 Eol
Vons Credit Union Routing Number
Locate phone number
Florida Lottery Claim Appointment
Despacito Justin Bieber Lyrics
ACTUALIZACIÓN #8.1.0 DE BATTLEFIELD 2042
8776725837
Arcanis Secret Santa
Walmart Careers Stocker
Worland Wy Directions
Latest Posts
Article information

Author: Ouida Strosin DO

Last Updated:

Views: 6454

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Ouida Strosin DO

Birthday: 1995-04-27

Address: Suite 927 930 Kilback Radial, Candidaville, TN 87795

Phone: +8561498978366

Job: Legacy Manufacturing Specialist

Hobby: Singing, Mountain biking, Water sports, Water sports, Taxidermy, Polo, Pet

Introduction: My name is Ouida Strosin DO, I am a precious, combative, spotless, modern, spotless, beautiful, precious person who loves writing and wants to share my knowledge and understanding with you.