Use this readily-available Integrated Circuit to control a stepper motor or two DC motors from your microcontroller.
In Issue 12, Sophie took a detailed look at how H-bridges work. We’re only going to take a summarised recap of that this time, so for more information, look up ‘Bridging the Gaps’ in Issue 12. What we are looking at is the L293D integrated circuit (IC) in detail. We’ll look at its pin-outs, parameters, limitations, and usability.
The L293D comes in a 16-pin Dual Inline Plastic (DIP) package for through-hole mount applications. ST Microelectronics produce a surface-mount version (designated L293DD) but we were unable to locate a maker-accessible source. The through-hole version is the one on the retail market, so that’s the one we will describe. Please be aware that if you have any other package, or even a different brand besides Texas Instruments or ST Microelectronics, specifications such as maximum current or temperature may be different.
WHAT IS A H-BRIDGE?
A H-bridge is a set of switches that can control how current flows through a motor. The switches can be set so that current is applied in one direction or the other, so that clockwise and anti-clockwise directions are achieved.
They can be used with other loads as well, such as solenoids. Sophie gave a great primer on this in issue 12. The L293D is just semiconductor switches and some support circuitry enclosed in a single case. By feeding certain signals into the IC, you can control a bipolar stepper motor or two DC motors (or other loads) from an Arduino, Raspberry Pi, or even a discrete circuit.
The datasheet for the L293 and L293D is common to both. Be careful though, as there are some significant differences in current capability between the two. The L293D, which we are focusing on because it’s available on retail shelves, has a continuous output current of 600mA. The L293, on the other hand, can cope with 1000mA continuously. Peak output current is also different, being 1200mA for less than or equal to one hundred microseconds for the L293D, but 2000mA for less than or equal to five milliseconds for the L293. Note that these figures are all maximum ratings and are calculated with the junctions of the driver transistors at 25°C, which they rarely are. Read on for more on this point.
The IC has some pin connections that most makers won’t encounter very often. The IC has four channels, or sets of switches. Each channel is referred to as a ‘half-H-bridge’. They are enabled in pairs, with one pin enabling channels 1 and 2, and the other enabling channels 3 and 4. For this reason, it makes sense to pair them that way, but you could run a motor with channels 1 and 3, and another with channels 2 and 4. You would just need both ‘enable’ pins driven high even if using only one motor. You cannot use any channel unless it is first enabled by its respective ‘enable’ pin, which is something to keep in mind if you’re used to devices where the inputs and outputs just work, as long as the IC has power.
There are also two supply voltage connections for the IC. While we have discussed dual-rail ICs in the past, this one takes two different supplies, both positive. They are labelled ‘Vcc1’ and ‘Vcc2’. The reason is that the internal logic runs from +5V, and is supplied by Vcc1. The motor outputs have their own supply, connected to Vcc2, which can be anywhere from 4.5V to 36V. This makes it easy to run 12V or even 24V motors from 5V logic such as our common microcontrollers or TTL discrete logic circuits.
The other main difference between this and other ICs that makers are likely used to, is the ground connection. There are four pins connected to ground, and all should be used. This enables the current from each channel to be returned to ground without overloading a single pin. The four pins are also used as a heatsink. The design notes for the IC recommend as large a copper ground plane be designed into the circuit board as possible, as close to the ground pins as possible, which acts as a heatsink. This is important to think about if you are using the IC on a breadboard. In this situation, there is no heatsinking at all from the board. Because the IC is designed to be heatsunk from the pins, the situation is more complicated than just sticking a heatsink to the case. The thermal resistance to ground from the junction of the drive transistors to the circuit board through the pins is 16.5°C per Watt of power dissipated, while junction-to-case thermal resistance is 22.5°C/W. Bear in mind also, that thermal resistance will increase with temperature. Check out Bob Harper’s Classroom on heatsinks in Issue 24. Of course, sticking a heatsink to the top of the case will make a difference, but it will in no way guarantee operation to maximum specifications. There is a suggestion in the datasheets that involves a heatsink which touches both the case top, and all four ground pins. Of course, you would have to make this yourself.
INPUTS AND OUTPUTS
There are two types of input to the L293D. Two pins are ‘enable’ pins as described above, while four are the inputs to actuate the ‘switches’. These actually control the base of internal NPN transistors, which activate internal constant current sources. These are fed to the base of darlington-arranged output transistor networks. On the L293D, the outputs have clamping diodes built into the outputs to slug the transients that occur in inductive loads when the input current ceases and the magnetic field collapses. The L293 lacks this feature, and failure to include the diodes can result in a destroyed IC. Perhaps it’s a blessing that only the lower-current but protected L293D is readily available from retailers.
USING THE L293D
The two most likely uses of the L293D for the maker are to drive two DC motors accurately, and to control bipolar stepper motors. The device is capable of controlling four DC motors in a single-direction format, where each is simply switched on or off, but MOSFET circuits seem to be the preference among makers for this. It could be used to drive four relays, and in doing so simplifies the inductive load issues encountered when connecting these to microcontrollers. The same can be said for solenoids. Four of these can be controlled with a L293D, or two push-pull solenoids.
RUNNING TWO DC MOTORS
Using two DC motors together is often a feature of robotics projects. Whether using wheels or tracks, this format allows easy steering capability by varying the speed and/or direction of the motors, and eliminates the need for differential gears and drive trains. The system was used in early tanks, which had separate engines. Many modern tracked military vehicles, particularly light ones, use the idea too. So does some earthmoving equipment, such as skid steers (which many people know by the brand name ‘Bobcat’) and some bulldozer designs. Zero-turn ride-on lawnmowers also conform to this model. What most of these have in common is a diesel or petrol engine which runs a powerful hydraulic pump. Valves regulate the flow of hydraulic fluid to hydraulic motors on the wheels. By slowing the flow, a track or wheel slows down, and the vehicle turns. Motors can be reversed by reversing fluid flow, and braked by closing all valves. Some great online videos exist demonstrating and explaining this idea, and some utilise real dismantled equipment.
The principle is the same with two electric motors, and the current flow mimics the fluid flow in the hydraulic devices. Channel 1 connects to one terminal of the first motor, while channel 2 connects to its other terminal. With one channel on and the other off, the motor has current sourced from one and sunk by the other. If the on and off channels are reversed, the direction reverses. This process is repeated for channels 3 and 4 connected to a second motor. Using the input pins for each channel, the motors can be turned on and off, and their direction changed.
Here’s the fun part: Remember the enable pin? This can be used to feed a pulse-width modulated signal to the pairs of channels. So while the input pins control the state and direction of the motor, the ‘enable’ pin can control the speed, by turning a pair of channels on and off very rapidly. This simplifies coding, but also enables discrete circuits to be separate. One can control direction while another section of the circuit deals with speed. That’s a simplistic overview, but the idea carries.
You’ll have to be careful with the PWM behaviour on this circuit. On a regular PWM motor driver circuit, the power to the motor simply stops. The motor’s inertia will carry it forward a little between pulses. There isn’t much in this, but it may have a visible result in some situations. This means that a motor’s speed may be different for a given PWM setting, depending on how much load it’s under. If it can coast, it may appear faster than near maximum load, when the load resistance will slow the motor faster when it is not being powered for that instant. The visible effect may be small or not, depending on your circumstances.
However, when the ‘enable’ pin of a pair of channels is low during the low of the PWM cycle (or at any other time), the channels are not merely disconnected. They connect the motor terminals to form a high-impedance brake. The brake is applied in that short moment no matter whether the motor is loaded or free running. This will make output speed more consistent and less load-dependent, although as noted, the effect may not have been visible in the first place.
Driving the two motors from a code or other control source is not universal. Depending on the motor, connecting a terminal to positive and the other for negative may result in two different directions of rotation depending on magnet polarity and the way the motor windings are connected. So instead, we have a truth table to show the result of different input combinations. How to connect your motor to it will depend on the motor itself, meaning your motor may turn clockwise while ours turns anti-clockwise. Note that the truth table is just for one motor, but the same thing applies if you swap the labels to the second set.
Driving Bipolar stepper motors works in a very similar way, except that there are two windings in one motor, rather than one winding in each of two motors. Stepper motors have a permanent magnetic rotor and a series of coils as a stator, the reverse from a common DC motor. There will be many coils around the stator, but each alternate one is connected so that only two groups, or poles, exist. Explaining stepper motor operation is beyond this article, but the simplest work like this: While one pole is on, the next is off, so that the motor can index around by an amount dictated by the size of the coil. This is where it gets its ‘step’ from. This description is valid for bipolar stepper motors, but there are versions with different coil configurations. They all work a bit differently, and are for another time or your own reading.
To drive a stepper motor with the L293D, the input pins control the polarity of each pole, and the ‘enable’ pins control when to switch each pole on and off. Having both off brakes the motor, but having one pole on and one off will hold the motor in a given position, albeit at the cost of power consumption.
The limiting factor in the L293D is its current capacity. The maximum of 600mA continuous does not change with PWM situations. Remember, the 1.2A peak is only for one hundred microseconds, and is not repeatable in a short time frame. This really means your motor must be drawing well under 600mA in order to be used with the L293D. Unfortunately, things aren’t as simple as just boosting the output with bigger transistors. You need both high and low drivers, for current sourcing and sinking, as well as diodes and other support circuitry.
At this point, you’re better off building a high-current H-bridge from scratch, or sourcing a higher-current driver. These exist but are not widely available consistently across the domestic retail market, and so, we did not look at these. The DIY high-current version is for another day.
Having said that, there are still many projects with motors within the current range, and the use of smaller motors as concept development tools before going large-scale has significant value. Proof of concept and code development is much easier on a workbench with a small motor than it is on a full-scale machine, particularly if the end result is bigger than your whole workbench!
The L293D is still valid for other uses, from RGB LED controllers as described by Sophie in Bridging the Gaps. Despite its current limit, the device can drive more than many general purpose transistors, and has the added benefits of outputs protected against inductive loads, along with versatile input configurations.
Test Platform Build:
|Parts Required:||Jaycar||Altronics||Core Electronics|
|2 x Solderless Breadboard||PB8820||P1002||CE05102|
|1 x Pack Breadboard Wire Links||PB8850||P1014A||CE05631|
|10 x Plug to Plug Jumper Leads *||WC6024||P1016||PRT-12795|
|3 x Plug-to-Socket Jumper Wires *||WC6028||P1017||PRT-12794|
|1 x Arduino Uno or Compatible Board||XC4410||Z6240||CE05629|
|2 x 10kΩ Linear Potentiometers, 16mm||RP7510||R2243||ADA562|
|2m x Light Duty Speaker Wire||WB1702||W2100||-|
|1 x 2m Cat5e Network Cable||WB2020||W7100||FIT0116|
|1 x Motor Chassis, 2WD #||KR3160||K1090||-|
|1 x L293D IC||ZK8880||Z2900||ADA807|
* Quantity required, may only be sold in packs.
# We used the chassis, but you could choose two suitable motors
Note: Knobs are optional and are open to maker choice
This relatively straightforward build is an expansion of Sophie’s design from Issue 012. We’re going to use an Arduino compatible Uno and the L293D to drive two motors, which run from 5V DC at 150mA to 10V DC at 250mA. We chose to run at 5V for the whole system, both to reduce the maximum speed of the motors while they’re on the workbench, and to keep the current low. We have designed the circuit so that you can power it from 5V to 12V without change. The Uno can accept up to 12V on its VIN, and documentation suggests that input filtering is good enough to run motors on the supply rail without additional filtering. While this is common practice with Arduino, cheap, electrically noisy motors may still require capacitors across their coils and more on the power supply rails.
We have also included two 10kΩ potentiometers between the +5V rail from the Arduino and Ground. These form variable voltage dividers, connected to two of the analogue inputs on the Uno. One is for steering, the other for speed control.
The speed control one is fairly simple. It gives an analogue voltage between 0% and 100% of the 5V supply, and this value is used by the code to set the PWM duty cycle for the Enable pins on the L293D.
We built this test circuit on two breadboards - one on the chassis with the motors and the L293D connected, and one with the Uno, potentiometers, and power supply connections. We debated using prototyping PCBs for this build, and did an initial build ourselves. The challenge was that the versions with power rails, rows and columns, and other layout features, rather than straight strips or grids of pads, differ between suppliers. We couldn’t present an option that would readily work no matter where the boards are sourced from. This meant that we soldered a thick piece of copper wire to the top of the ground/heatsink pins on the L293D, to act as a heatsink in lieu of the ground rail tracks that would be used on the PCB.
The steering input gives a value between 0% and 100% of the supply voltage, but in this case, the code looks for the amount above or below 50%. If the potentiometer is less than 50% (which corresponds to being turned to the left of centre), the left hand motor slows down by that percentage difference. If the value is more than 50%, which corresponds to the potentiometer being turned to the right of centre, the right hand motor slows down by the difference.
In both cases, as the Uno uses the supply voltage to reference the upper limit of its analogue brains, we don’t need to read any reference voltage separately. Also in both cases, which direction (left or right) is slower or less than 50% depends on which ends of the potentiometer you connect the positive and ground to. You may need to switch your power connections to make the pot behave the way your hand-eye coordination wants it to.
We used a motor chassis for our build, but you can just use two motors if you’re developing for a different physical application. If you are using a chassis, assemble it now.
Solder any improvised heatsink you may be using to the relevant pins of the L293D, allow it to cool, then insert it into the board. Note that this heatsink is not included in the parts list, as you can use whatever you have on hand. Ours was a couple of lengths of speaker wire, stripped and twisted together.
You will need to solder pins onto the motor wires to insert them into the board. We used cut up wire links. Solder the pins on, and insert them along with the stripped Cat5e wires. You can choose your own colour code. Also in this step, add pins to the wires for the motor power supply, and connect one to ground, and the other to pin 8 of the L293D.
At the other end, install the potentiometers to the breadboard. Use plug-to-socket jumper wires for this task. The ends of the potentiometers connect to the upper power rail, while the wipers stay unconnected for now. You can also strip the ends of the Cat5e cable and insert them into the middle of the board. Solder wire links to the ends of the speaker wire which carries the motor current, and install them into the lower power rail.
Use plug-to-plug jumper wires to connect the Arduino pins to the Cat5e cable. Also add connections between the VIN and GND connections on the Uno, and the lower power rail on the breadboard. Two more jumpers connect the Gnd and 5V outputs to the upper power rail. The wires from the potentiometer wipers connect now too. They’re going to the first two analogue inputs.
There are two connections in the Cat5e cable so far unconnected. These connect to GND and Vcc1 at the L293D end, and the upper power rail on the other breadboard near the Uno. This will connect the Uno's 5V power output to the L293D's Vcc1 connections to power its internal logic.
Now you can upload a sketch and start testing. We have written a basic sketch for you, but we won’t explain it in detail here. So that we can update, improve, and modify it without rendering this article inaccurate, we have relied on comments in the sketch. If you’re writing your own sketch, the information above should tell you what pins need to do what.
One final note: It may look odd to some that we chose to end the Cat5e cores in the breadboard and jumper them to the Uno, rather than inserting directly to the Uno. We went this way for two reasons: The rigid solid-core Cat5e wire proved problematic when trying to change pin orders and such, as often three or four cores came out when we only wanted one. Additionally, the rigidity gave us problems when we had some cores in the Uno and others in the breadboard rails. You can give it a go if you want to try a different physical arrangement.
WHERE TO FROM HERE?
Our circuit cannot reverse or neutral turn as is. Neutral turning, also called zero turning, is where one motor moves forwards and the other backwards. This is better achieved with two potentiometers, one for each PWM pair of channels. Generally, the code would read each potentiometer. Both speed and direction are related to the 50% mark. Above 50% is forward and the percentage dictates the speed. Less than 50% is a reversed motor.
This may or may not be easier to achieve with the Arduino, but we avoided it for this reason: Using a potentiometer that moves left and right like a steering wheel is more intuitive to use for untrained users, as it relates to direction directly.
Two potentiometers would be harder to use unless you can source a lever mount like that used on radio-control transmitters or electronic throttles. Or, you could side-mount your potentiometers, connect a lever, and change the code to only monitor the percentage of the 270° of rotation that is now used. Lever-based control like this is used on most tracked plant equipment like excavators. Slider pots would work well if you can find a retail source of them.
The other option for reversing is to use a switch on another Arduino input. You could have the code monitor it and slow the PWM to nothing before changing the High/Low assignments on the L293D’s inputs, or just remember to slow it manually before flipping the switch. This option will give you reverse, but not neutral turning.