12,074,485 members (64,684 online)
Add your own
alternative version

80.6K views
674 downloads
65 bookmarked
Posted

# Motor Primer and the L293D Quad Half-H Driver

, 1 Feb 2011 CPOL
 Rate this:
Please Sign up or sign in to vote.
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

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.

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.

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.

```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.

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:

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.

### 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

#### 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

#### 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 H L L L H L L L H H L L L H L L L H H L L L H H L L H H L L L

## 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.

## License

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

## About the Author

 Retired United States
I'm old and I have nothing more to say!

## Comments and Discussions

 First Prev Next
 My vote of 5 manoj kumar choubey22-Sep-13 20:45 manoj kumar choubey 22-Sep-13 20:45
 Re: My vote of 5 Mike Hankey22-Sep-13 20:58 Mike Hankey 22-Sep-13 20:58
 My vote of 5 Mohammad A Rahman18-Apr-12 21:42 Mohammad A Rahman 18-Apr-12 21:42
 My vote of 5 CPallini8-Mar-12 5:05 CPallini 8-Mar-12 5:05
 Congratulations Mike! Marcelo Ricardo de Oliveira30-Apr-11 1:48 Marcelo Ricardo de Oliveira 30-Apr-11 1:48
 Re: Congratulations Mike! Mike Hankey30-Apr-11 3:36 Mike Hankey 30-Apr-11 3:36
 My vote of 5 Julian Nicholls22-Mar-11 1:32 Julian Nicholls 22-Mar-11 1:32
 My vote of 5 Tieske814-Feb-11 2:22 Tieske8 14-Feb-11 2:22
 My vote of 5 Abhinav S9-Feb-11 3:59 Abhinav S 9-Feb-11 3:59
 Question JimD.99992-Feb-11 5:08 JimD.9999 2-Feb-11 5:08
 Re: Question Mike Hankey2-Feb-11 5:37 Mike Hankey 2-Feb-11 5:37
 For starters I am not an EE but a humble programmer. JimD.9999 wrote:"wire it as bipolar so we don't have to use another power supply." My meaning was simply that if I were using a 12V motor the center taps would need a 12V supply as well as the 5V for the chip. JimD.9999 wrote:There could also be a big advantage to using unipolar, for example, a 12V motor that you were driving with 5V, you would have half the impedance and get more torque. Thanks for the tip I'm always up to learning new tricks. Even a blind squirrel gets a nut occasionally. http://www.hq4thmarinescomm.com[^] [My Site]
 Re: Question JimD.99992-Feb-11 5:48 JimD.9999 2-Feb-11 5:48
 Re: Question Mike Hankey2-Feb-11 6:47 Mike Hankey 2-Feb-11 6:47
 Great Article, but can not download the code. JimD.99991-Feb-11 5:05 JimD.9999 1-Feb-11 5:05
 Re: Great Article, but can not download the code. Mike Hankey1-Feb-11 5:17 Mike Hankey 1-Feb-11 5:17
 Re: Great Article, but can not download the code. JimD.99991-Feb-11 5:38 JimD.9999 1-Feb-11 5:38
 Re: Great Article, but can not download the code. Mike Hankey1-Feb-11 5:41 Mike Hankey 1-Feb-11 5:41
 My vote of 5 Oakman31-Jan-11 6:42 Oakman 31-Jan-11 6:42
 Re: My vote of 5 Mike Hankey31-Jan-11 10:42 Mike Hankey 31-Jan-11 10:42
 Re: My vote of 5 Oakman31-Jan-11 11:21 Oakman 31-Jan-11 11:21
 Re: My vote of 5 Mike Hankey31-Jan-11 11:26 Mike Hankey 31-Jan-11 11:26
 My vote of 5 Bob Carter31-Jan-11 5:07 Bob Carter 31-Jan-11 5:07
 Re: My vote of 5 Mike Hankey31-Jan-11 10:44 Mike Hankey 31-Jan-11 10:44
 My vote of 5 Yusuf30-Jan-11 2:39 Yusuf 30-Jan-11 2:39
 Re: My vote of 5 Mike Hankey30-Jan-11 5:10 Mike Hankey 30-Jan-11 5:10
 Handy DaveAuld29-Jan-11 23:36 DaveAuld 29-Jan-11 23:36
 Re: Handy Mike Hankey30-Jan-11 2:34 Mike Hankey 30-Jan-11 2:34
 My vote of 5 Norm .net 29-Jan-11 23:04 Norm .net 29-Jan-11 23:04
 Re: My vote of 5 Mike Hankey30-Jan-11 2:34 Mike Hankey 30-Jan-11 2:34
 My vote of 5 Abu Mami29-Jan-11 21:25 Abu Mami 29-Jan-11 21:25
 Re: My vote of 5 Mike Hankey30-Jan-11 2:35 Mike Hankey 30-Jan-11 2:35
 My vote of 5 Luc Pattyn29-Jan-11 17:25 Luc Pattyn 29-Jan-11 17:25
 Re: My vote of 5 Mike Hankey30-Jan-11 2:41 Mike Hankey 30-Jan-11 2:41
 Last Visit: 31-Dec-99 19:00     Last Update: 11-Feb-16 7:17 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.160208.1 | Last Updated 1 Feb 2011
Article Copyright 2011 by Mike Hankey
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid