Click here to Skip to main content
15,867,308 members
Articles / Internet of Things / Arduino

Motor Primer and the L293D Quad Half-H Driver

Rate me:
Please Sign up or sign in to vote.
4.96/5 (41 votes)
1 Feb 2011CPOL9 min read 135.2K   851   68   33
Learn to control DC and Stepper motors using a Micro Controller

Introduction

There are many ways to drive small current motors, those requiring 500mA or less but the L293H Quadruple Half-H driver is a versatile chip that was designed for use with motors, can very easily be controlled with a micro-controller and can be purchased [here]. Using this chip, we can drive either 2 DC motors or one Stepper motor and we will learn how to do both in this tutorial. The components needed to complete this exercise are an ATMega328p Micro-controller to interface with and control the L293D device but any controller may be used, the L293D component and one dc motor.

DC Motors

Image 1

This is not an article on motor theory, there are plenty of tutorials available [1], but a fundamental understanding of how they work is going to be helpful later when we start programming the software to control them. Most small dc motors that are used by hobbyists are of the brushless type and are comprised of two basic parts - the rotor, the moving part and the stator, the stationary part and generally have 3 poles as shown in the animation to the right. Stationary polarized permanent magnets are placed at opposing sides and the rotor interacts with the magnets by applying current to the windings of the poles in the manner shown in the animation thus driving the motor. The direction of the motor is determined by the polarity of the input voltage and the direction associated with a rotation as defined by the Right-hand rule.[2] When working with DC motors, there are two current ratings that you must pay attention to. The first is the operating current, this is the average amount of current that the motor will consume while running under normal conditions. The next is the stall current that can be best demonstrated if you hold the shaft of the motor with your fingers so that it cannot turn and apply power. The L293 can handle a continuous peak output current of 650mA so whatever motor you decide on should not draw more than this value under the most severe conditions.

For this exercise, I salvaged a small motor out of an old CD drive and was surprised to find any kind of data on this motor as the drive it came out of was very old. Listed below is the motor specifications that were found for this motor and as you will notice, the stall current for this motor is well under the current limit for the L293D we are well within the operating range.

Image 2

Another factor to be aware of is that because of inductance and momentum when reversing the direction of the motor while it is running will cause a brief voltage spike effectively doubling the operating voltage and cause the current to rise to just below the stall current.

Once the motor is running, it generates a voltage similar to moving a wire through a magnetic field to produce whats called back EMF. The way to counteract this is to place a general purpose diode across the leads of the motor allowing current to flow in only one direction thus counteracting the back Electro Motive Force. But one of the things that makes the L293D chip unique is that it has these diodes that are built into the chip leaving us with one less thing to worry about.

So enough theory. Let's get to wiring it up and watching it go. The schematic below shows how to wire the different components together and as can be seen, it is a very simple process involving only the controller, the driver chip and a motor of your choice. The chip takes two supply voltages; one is the standard 5V supply to drive the chip logic and the other, the motor supply voltage can vary from 4.5V to 36V depending on the specifications of your motor. In the schematic, there is only one motor shown but another may be wired in a similar fashion using the 2nd set of inputs, enable and outputs.

Image 3

From the schematic above, we see that the ports used to drive the motor are PC0-PC2 where PC0 and PC1 are the input control pins and P2 is the enable pin. Therefore to drive the motor, we need to refer to the following table that describes the various combination's possible.

Enable In1 In2 Result
H L H Motor turns CW
H H L Motor turns CCW
H L or H L or H Fast Stop
L L or H L or H Fast Stop

In addition to controlling the direction of the motor, the speed may also be controlled using the ATMega328p timer and the Pulse Width Modulation (PWM) feature, but that is beyond the scope of this article. Of note when using PWM to drive the motors wire the enable pin to VCC1 through a 10K pull-up resistor as it is slower than the input pins.

Concluding our discussion of DC motors and putting to use what we've learned, the listing that follows demonstrates how to program a DC motor.

C++
void DCMotor_cmd(DC_MOTOR_CMDS cmd)
{
	switch(cmd)
	{
		case forward:
			DATA_PORT = _BV(ENABLE1) | ~_BV(IN1) | _BV(IN2);
			break;
		case reverse:
			DATA_PORT = _BV(ENABLE1) | _BV(IN1) | ~_BV(IN2);
			break;
		case stop:
			DATA_PORT = ~_BV(ENABLE1) | ~_BV(IN1) | ~_BV(IN2);
			break;
	}
}

Stepper Motors

"In Theory, a Stepper motor is a marvel in simplicity. It has no brushes, or contacts. Basically it's a synchronous motor with the magnetic field electronically switched to rotate the armature magnet around."[3]

Stepper motors are used where more precision is needed than can be achieved using the DC motor discussed in the previous section. Some of the advantages of using a stepper motor as opposed to a DC motor are:

  • Speed can be controlled by the step rate.
  • Relative positioning is very precise: For a single step, you know exactly how much the shaft will turn for each step. If you purchase the motor, one of the specifications is the number of steps/rotation and if you salvaged it, it's easy enough to determine.
  • Motor holds position when power is applied and not stepping.

For this exercise, I'll be using a unipolar stepper motor I purchased from [here]. A portion of the specifications for this stepper is listed in the table that follows and is typical of the information for stepper motors, and if data is available for your device it should look similar.

Image 4

Even though there is information about the device I am using in this tutorial, a lot of times the device that you are using will have been salvaged out of an old printer, scanner or some other device and there likely as not won't be any information available. This isn't a problem and we'll go through the identification process now. There are two types of stepper motors available, the unipolar and the bipolar types. Unipolar motors have 5 or 6 wires whereas the bipolar motor will have either 4, 6 or 8 leads. For further information on identifying the different types of stepper motors, an excellent reference can be found on wimb.net.[4]

Now we need to determine what wires do what and for that we need a multimeter to measure the resistance across the coils. Start by drawing a table like the one below. Now using the multimeter, measure across each pair of wires noting the results as you progress. When you are done, you should end up with something similar to that shown in the table below:

Image 5
Image 6

If we cross reference the information we gathered with what we know about unipolar motors, we can see that the red and green wires are the common center taps and the other leads connect to the coils terminal points. Because this motor has separate center taps for each winding, we can wire it either as a unipolar or bipolar device and that being the case we will wire it as bipolar[5] [6] so we don't have to use another power supply. To do this we just use the Orange, Yellow, Black and Brown wires and leave the Red and Green wires floating. The code to control the motor is the same whether we are wired as unipolar or bipolar so that won't be an issue. 

One other problem we have to overcome is finding the proper wiring order. To do this, we need to get the components wired up and the software working. This is not real difficult. We just hook up the wires initially in an arbitrary order and switch first the end pairs then the middle pairs until the motor is running smoothly.

The Stepper motor is wired similar to the DC motor except we will now be using both channels as opposed to just a single as can be seen in the schematic that follows. For this exercise, I used two 10K pullup resistors for the enable lines because one I'm lazy and two there's really no need for them in this instance.

Image 7

Stepper Motor Modes of Operation

When programming the stepper motor, there are several options that we can use depending on our needs. The following section is divided into three sections each with a logic table that shows the order that the coils are to be activated and an animation displaying a representation of what is happening inside the motor. There is a fourth mode of operation referred to as Microstepping[7] that requires the driver to control the magnitude of the current in each winding but is beyond the scope of this tutorial.

Wave Drive Mode

The Wave drive mode is a single phase mode of operation and provides a Low-Torque. This means that only one coil at a time is energized therefore it is not the optimal mode since it only takes advantage of 25% of the total motor winding at any given time.

In1 In2 In3 In4
H L L L
L H L L
L L H L
L L L H
Image 8

Full Step Mode

In the Full Step Mode, two coils are being energized at any given time thus producing a higher torque and takes better advantage of the available winding. For unipolar motors, this mode utilizes 50% of available winding while bipolar is 100% efficient. The other difference is that each step will be offset by one-half of a full step as opposed to the Wave mode.

In1 In2 In3 In4
H H L L
L H H L
L L H H
H L L H
Image 9

Half Step Mode

The Half Drive mode combines both the Wave and Full Step modes and provides twice the number of steps that can be realized with the other two modes.

In1 In2 In3 In4
H L L
L H L L
H H L
L L H L
L H H
L L L H
H L L
H L L L
Image 10

Summary

When I started doing the research for this article, I thought I knew quite a bit about motors and how to drive them using the L293D chip but it turns out that the research unveiled a lot of information that I hadn't known so this turned out to be a learning experience for me too. 

References

  1. Marshall Brain, How Electric Motors Work
  2. McGraw-Hill Science & Technology Dictionary, Right-hand rule
  3. Advanced Micro Devices, Stepper Motor System Basics
  4. wimb.net, Stepper Motor Connections
  5. SteppermotorWorld.com, Stepper Motors and Control Part II - Bipolar Stepper Motor and Control
  6. Rickey's World, Stepper Motor interfacing with Microcontrollers
  7. Stepper Motor Microstepping Tutorial

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Retired
United States United States
Currently enjoying retirement and working on projects without pressure, deadlines or any kind of management.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey22-Sep-13 19:45
professionalManoj Kumar Choubey22-Sep-13 19:45 
GeneralRe: My vote of 5 Pin
Mike Hankey22-Sep-13 19:58
mveMike Hankey22-Sep-13 19:58 
GeneralMy vote of 5 Pin
Mohammad A Rahman18-Apr-12 20:42
Mohammad A Rahman18-Apr-12 20:42 
GeneralMy vote of 5 Pin
CPallini8-Mar-12 4:05
mveCPallini8-Mar-12 4:05 
GeneralCongratulations Mike! Pin
Marcelo Ricardo de Oliveira30-Apr-11 0:48
mvaMarcelo Ricardo de Oliveira30-Apr-11 0:48 
GeneralRe: Congratulations Mike! Pin
Mike Hankey30-Apr-11 2:36
mveMike Hankey30-Apr-11 2:36 
GeneralMy vote of 5 Pin
Julian Nicholls22-Mar-11 0:32
Julian Nicholls22-Mar-11 0:32 
GeneralMy vote of 5 Pin
Tieske814-Feb-11 1:22
professionalTieske814-Feb-11 1:22 
GeneralMy vote of 5 Pin
Abhinav S9-Feb-11 2:59
Abhinav S9-Feb-11 2:59 
GeneralQuestion Pin
JimD.99992-Feb-11 4:08
JimD.99992-Feb-11 4:08 
GeneralRe: Question Pin
Mike Hankey2-Feb-11 4:37
mveMike Hankey2-Feb-11 4:37 
GeneralRe: Question Pin
JimD.99992-Feb-11 4:48
JimD.99992-Feb-11 4:48 
GeneralRe: Question Pin
Mike Hankey2-Feb-11 5:47
mveMike Hankey2-Feb-11 5:47 
GeneralGreat Article, but can not download the code. Pin
JimD.99991-Feb-11 4:05
JimD.99991-Feb-11 4:05 
GeneralRe: Great Article, but can not download the code. Pin
Mike Hankey1-Feb-11 4:17
mveMike Hankey1-Feb-11 4:17 
GeneralRe: Great Article, but can not download the code. Pin
JimD.99991-Feb-11 4:38
JimD.99991-Feb-11 4:38 
GeneralRe: Great Article, but can not download the code. Pin
Mike Hankey1-Feb-11 4:41
mveMike Hankey1-Feb-11 4:41 
GeneralMy vote of 5 Pin
Oakman31-Jan-11 5:42
Oakman31-Jan-11 5:42 
GeneralRe: My vote of 5 Pin
Mike Hankey31-Jan-11 9:42
mveMike Hankey31-Jan-11 9:42 
GeneralRe: My vote of 5 Pin
Oakman31-Jan-11 10:21
Oakman31-Jan-11 10:21 
GeneralRe: My vote of 5 Pin
Mike Hankey31-Jan-11 10:26
mveMike Hankey31-Jan-11 10:26 
GeneralMy vote of 5 Pin
Bob Carter31-Jan-11 4:07
Bob Carter31-Jan-11 4:07 
GeneralRe: My vote of 5 Pin
Mike Hankey31-Jan-11 9:44
mveMike Hankey31-Jan-11 9:44 
GeneralMy vote of 5 Pin
Yusuf30-Jan-11 1:39
Yusuf30-Jan-11 1:39 
GeneralRe: My vote of 5 Pin
Mike Hankey30-Jan-11 4:10
mveMike Hankey30-Jan-11 4:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.