L293d Motor Driver Circuit
One of the easiest and inexpensive way to control stepper motors is to interface L293D Motor Driver IC with Arduino. It can control both speed and spinning direction of any Unipolar stepper motor like 28BYJ-48 or Bipolar stepper motor like NEMA 17.
If you want to learn the basics of L293D IC, below tutorial is invaluable. Consider reading (at least skimming) through this tutorial first.
Control DC Motors with L293D Motor Driver IC & ArduinoL293D IC is a typical Motor Driver IC which allows the DC motor to drive on any direction. This IC consists of 16-pins which are used to control a set of two DC motors instantaneously in any direction. The L293D is a dual-channel H-Bridge motor driver capable of driving a pair of DC motors or one stepper motor. That means it can individually drive up to two motors making it ideal for building two-wheel robot platforms. For more details please refer below datasheet. L293d ic is same like an h bridge circuit with two channels. It has two half h bridge circuits residing in it. You can use it to drive uni polar, bi polar stepper motors, dc motors or even servo motors. The individual two channels can be use stand alone to drive solenoids/relays. A single channel can be used to drive a dc motor in forward(clock wise) or back word(anti clock wise) direction. May 16, 2018 L293D is a 16 pin motor driver IC consist of quadruple half H drivers. It can simultaneously control the direction and speed of two DC motors. L293d is a suitable device to use for stepper motors, gear motors etc. The IC has an operating voltage range from 4.5 V to 36 V.
As L293D IC has two H-Bridges, each H-Bridge will drive one of the electromagnetic coils of a stepper motor. By energizing these electromagnetic coils in a specific sequence, the shaft of a stepper can be moved forward or backward precisely in small steps.
Controlling a Stepper Motor With an H-Bridge
As L293D IC has two H-Bridges, each H-Bridge will drive one of the electromagnetic coils of a stepper motor.
By energizing these electromagnetic coils in a specific sequence, the shaft of a stepper can be moved forward or backward precisely in small steps.
However, the speed of a motor is determined by the how frequently these coils are energized.
Below image illustrates driving stepper with H-Bridge.
Driving Unipolar Stepper Motor (28BYJ-48)
In our first experiment, we are using 28BYJ-48 unipolar stepper rated at 5V. It offers 48 steps per revolution.
Before we start hooking the motor up with the chip, you will need to determine the A+, A-, B+ and B- wires on the motor you plan to use. The best way to do this is to check the datasheet of the motor. For our motor these are orange, pink, blue and yellow.
Note that we will not be using the common center connection(Red) in this experiment.
The center connection is merely used to energize either the left or right side of the coil, and get the effect of reversing the current flow without having to use a circuit that can reverse the current.
The connections are fairly simple. Start by connecting 5V output on Arduino to the Vcc2 & Vcc1 pins. Connect ground to ground.
You also need to connect both the ENA & ENB pins to 5V output so the the motor is always enabled.
Now, connect the input pins(IN1, IN2, IN3 and IN4) of the L293D IC to four digital output pins(12, 11, 10 and 9) on Arduino.
Finally, connect the stepper motor’s wires A+ (Orange), A- (Pink), B- (Yellow) and B+ (Blue) to the L293D’s output pins (Out4, Out3, Out2 & Out1) as shown in the illustration below.
Driving Bipolar Stepper Motor (NEMA 17)
In our next experiment, we are using NEMA 17 bipolar stepper rated at 12V. It offers 200 steps per revolution, and can operate at 60 RPM.
Before we start hooking the motor up with the chip, you will need to determine the A+, A-, B+ and B- wires on the motor you plan to use. The best way to do this is to check the datasheet of the motor. For our motor these are red, green, blue and yellow.
The connections are fairly simple. Start by connecting external 12V power supply to the Vcc2 pin and 5V output on Arduino to the Vcc1 pin. Make sure you common all the grounds in the circuit.
You also need to connect both the ENA & ENB pins to 5V output so the the motor is always enabled.
Now, connect the input pins(IN1, IN2, IN3 and IN4) of the L293D IC to four digital output pins(12, 11, 10 and 9) on Arduino.
Finally, connect the A+ (Red), A- (Green), B+ (Blue) and B- (Yellow) wires from the stepper motor to the L293D’s output pins (Out4, Out3, Out2 & Out1) as shown in the illustration below.
Arduino Code – Controlling Stepper Motor
The following sketch will give you complete understanding on how to control a unipolar or bipolar stepper motor with L293D chip 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.
The sketch starts with including Arduino Stepper Library. The stepper library comes packaged with the Arduino IDE and takes care of sequencing the pulses we will be sending to our stepper motor.
After including the library we define a variable named stepsPerRevolution
. As the name suggests it’s the number of steps per revolution that our motor is rated at. Change this parameter as per your motor’s specification. For example, for NEMA 17 set it to 200 and for 28BYJ-48 set it to 48.
Next, we create an instance of the stepper library. It takes the steps per revolution of motor & Arduino pin connections as parameter.
In setup section of code, we set the speed of stepper motor by calling setSpeed()
function and initialize the serial communication.
In loop section of code, we simply call step()
function which turns the motor a specific number of steps at a speed determined by setSpeed()
function. Passing a negative number to this function reverses the spinning direction of motor.
If you are planning on assembling your new robot friend, you will eventually want to learn about controlling DC motors. One of the easiest and inexpensive way to control DC motors is to interface L293D Motor Driver IC with Arduino. It can control both speed and spinning direction of two DC motors.
And as a bonus, it can even control a unipolar stepper motor like 28BYJ-48 or Bipolar stepper motor like NEMA 17.
Control Stepper Motor with L293D Motor Driver IC & ArduinoControlling a DC Motor
In order to have a complete control over DC motor, we have to control its speed and rotation direction. This can be achieved by combining these two techniques.
- PWM – For controlling speed
- H-Bridge – For controlling rotation direction
PWM – For controlling speed
The speed of a DC motor can be controlled by varying its input voltage. A common technique for doing this is to use PWM (Pulse Width Modulation)
L293d Motor Driver Circuit Diagram
PWM is a technique where average value of the input voltage is adjusted by sending a series of ON-OFF pulses.
The average voltage is proportional to the width of the pulses known as Duty Cycle.
The higher the duty cycle, the greater the average voltage being applied to the dc motor(High Speed) and the lower the duty cycle, the less the average voltage being applied to the dc motor(Low Speed).
Below image illustrates PWM technique with various duty cycles and average voltages.
H-Bridge – For controlling rotation direction
The DC motor’s spinning direction can be controlled by changing polarity of its input voltage. A common technique for doing this is to use an H-Bridge.
An H-Bridge circuit contains four switches with the motor at the center forming an H-like arrangement.
Closing two particular switches at the same time reverses the polarity of the voltage applied to the motor. This causes change in spinning direction of the motor.
Below animation illustrates H-Bridge circuit working. westell modem access
L293D Motor Driver IC
The L293D is a dual-channel H-Bridge motor driver capable of driving a pair of DC motors or one stepper motor.
That means it can individually drive up to two motors making it ideal for building two-wheel robot platforms.
For more details please refer below datasheet.
Power Supply
The L293D motor driver IC actually has two power input pins viz. ‘Vcc1’ and ‘Vcc2’.
Vcc1 is used for driving the internal logic circuitry which should be 5V.
From Vcc2 pin the H-Bridge gets its power for driving the motors which can be 4.5V to 36V. And they both sink to a common ground named GND.
Output Terminals
The L293D motor driver’s output channels for the motor A and B are brought out to pins OUT1,OUT2 and OUT3,OUT4 respectively.
You can connect two DC motors having voltages between 4.5 to 36V to these terminals.
Each channel on the IC can deliver up to 600mA to the DC motor. However, the amount of current supplied to the motor depends on system’s power supply.
Control Pins
For each of the L293D’s channels, there are two types of control pins which allow us to control speed and spinning direction of the DC motors at the same time viz. Direction control pins & Speed control pins.
Direction Control Pins
Using the direction control pins, we can control whether the motor spins forward or backward. These pins actually control the switches of the H-Bridge circuit inside L293D IC.
The IC has two direction control pins for each channel. The IN1,IN2 pins control the spinning direction of the motor A while IN3,IN4 control motor B.
L293d Motor Driver Circuit Schematic
The spinning direction of a motor can be controlled by applying either a logic HIGH(5 Volts) or logic LOW(Ground) to these pins. The below chart illustrates how this is done.
IN1 | IN2 | Spinning Direction |
Low(0) | Low(0) | Motor OFF |
High(1) | Low(0) | Forward |
Low(0) | High(1) | Backward |
High(1) | High(1) | Motor OFF |
Speed Control Pins
The speed control pins viz. ENA and ENB are used to turn ON, OFF and control speed of motor A and motor B respectively.
Pulling these pins HIGH will make the motors spin, pulling it LOW will make them stop. But, with Pulse Width Modulation (PWM), we can actually control the speed of the motors.
Wiring L293D motor driver IC with Arduino UNO
Now that we know everything about the IC, we can begin hooking it up to our Arduino!
Start by connecting power supply to the motors. In our experiment we are using DC Gearbox Motors(also known as ‘TT’ motors) that are usually found in two-wheel-drive robots. They are rated for 3 to 9V. So, we will connect external 9V power supply to the Vcc2 pin.
Next, we need to supply 5 Volts for the L293D’s logic circuitry. Connect Vcc1 pin to 5V output on Arduino. Make sure you common all the grounds in the circuit.
Now, the input and enable pins(ENA, IN1, IN2, IN3, IN4 and ENB) of the L293D IC are connected to six Arduino digital output pins(9, 8, 7, 5, 4 and 3). Note that the Arduino output pins 9 and 3 are both PWM-enabled.
Finally, connect one motor to across OUT1 & OUT2 and the other motor across OUT3 & OUT4. You can interchange your motor’s connections, technically, there is no right or wrong way. God eater burst iso.
When you’re done you should have something that looks similar to the illustration shown below.
Arduino Code – Controlling a DC Motor
The following sketch will give you complete understanding on how to control speed and spinning direction of a DC motor with L293D motor driver IC and can serve as the basis for more practical experiments and projects.
Code Explanation:
The arduino code is pretty straightforward. It doesn’t require any libraries to get it working. The sketch starts with declaring Arduino pins to which L293D’s control pins are connected.
In setup section of code, all the motor control pins are declared as digital OUTPUT and pulled LOW to turn both the motors OFF.
In loop section of the code we call two user defined functions at an interval of a second. These functions are:
- directionControl() – This function spins both motors forward at maximum speed for two seconds. It then reverses the motor’s spinning direction and spins for another two seconds. Finally it turns the motors off.
- speedControl() – This function accelerates both the motors from zero to maximum speed by producing PWM signals using analogWrite() function, then it decelerates them back to zero. Finally it turns the motors off.