Motor Controllers

 

In this tutorial I will introduce various motor controller circuits for DC motors and stepper motors (uni-polar and bi-polar). Where appropriate I will give an idea of guideline costs - based on prices at Digikey as of summer 2008. These costs will assume that you are adding the controllers to an existing robot project and so will exclude the costs of the motors, wiring, soldering equipment, circuit board etc and focus purely on the cost and capability of the crcuit.

 

Each controller has an Eagle schematic but the implementation ie strip board, matrix board, hand crafted PCB is up to you. For those unfamiliar with Eagle then I suggest that you download the free/light version.

 

Familiarity with the concept of 'H-Bridges' is assumed because it is explained very well by others, either on this forum or via Google or Wikipedia. In essence: it is a circuit that can push current through a motor coil in both directions (ie it can make a DC motor go 'forwards' or 'backwards'). Equally concepts such as Pulse Width Modulation (PWM) are assumed or see my tutorial

 

 

1 - Introduction

Why do I need a controller?

 

Micro-controllers have a number of I/O (input/output) pins which can be set to 'high' (+5v) or cleared to 'low' (0v) under software control. So you may assume that you can connect a dc motor directly to two such pins. Seting pin1 high and pin 2 low would make the motor go one way; setting pin 1 low and pin 2 high would make the motor go the other way. Theoretically this is okay. In practice it will fry your microcontroller. Why? Well the pins on your microcontroller are only capable of supplying a few milli-amps of current (they are designed for driving other logic chips) - but your motor (being a big physical 'thing') requires a lot of power to make it move. The purpose of a motor controller is to convert the small powered signals from your micro-controller into more powerful signals that can drive motors. Given that 'Power = Amps * Volts' and 'Volts' is a fixed number then the power required is proportional to the current required. The more current then the more power. But power generates heat. And this is why you will 'fry' your controller. It will try to generate lots of current in order to drive the motor but this current will create heat, and since your controller has no heatsink, then it will melt !!

 

Stall current

Motors will require more current when they are under stress. So if you hold your robot in the air and allow the motors to turn then, because there is no resistance, they will need the smallest amount of current,. Now put your robot on the floor: and the weight of the robot will create friction and so the motors require more power to overcome this. Now if your robot is going up a steep hill or, worse, has hit a solid wall and is therefore trying to turn the motors but the wall prevents it from doing so then the motors are under the maximum amount of stress. This latter scenario is known as the 'stall current' and you need to make sure that your circuit doesn't get fried by this peak requirement. You can measure this 'stall current' with your multimeter by connecting the motor to your battery so that it turns and then hold the axle to stop it from turning. This 'stall current' is a very important value to measure for DC motors as it dictates the maximum current that your motor driver needs to provide. Without it your robot may work fine until it hits a wall and then 'hey presto', the current goes up and your circuitry melts!

 

Alien

If the above is too technical then think about the Alien movie where Sigourney Weaver gets into that robot suit that makes her 'extra strong'. That suit is a 'motor controller' - it converts her puny (but lovely!) movements into a much more powerful movement.

 

Stages

Think of your electronics in this way:-

1. The micro-controller works at 5 volts and small current. It is enough to drive other 'chips' but not motors. I call this the logic stage - as all of the electronics will work in this world. The logical world.

2. Motors work in another world of high current, higher voltages, heat, current spikes etc. The real/physical world.

3. You need something that sits in-between these worlds that converts micro-controller signals into something more powerfull that can control a motor. This is a motor controller.

 

2 - The tri-state switch

The normal operations of a motor are:

 

1. Spin clockwise

2. Spin anti-clockwise

3. Free wheel (ie coast to a halt)

4. Brake (ie try to stop immediately)

 

Other variations are that we probably also want to control the speed of the motor as well as the direction (clockwise/anti-clockwise) so that its not just 'full steam ahead' or 'full steam in reverse'. This always creates a challenge - as the more control you require tends to increase the number of precious output pins that you require for each motor. So we need to maintain flexibility whilst minimising the number of processor pins.

 

As a result - here is my 'tri-state' switch which helps to minimise the number of pins required and is used by a number of the different motor controllers that follow.

 

 

This diagram shows that each motor requies a 2 pin connection to your micro-controller via JP1, and provides 3 outputs to the motor controller stages.

Pin 1 - is used to Enable or Disable the motor controller chip. Whenever the controller is disabled then it is in 'coast' mode. So if we ask the motor to go in a particular clockwise direction then we can use this pin to set the speed that it turns. If it aways high then the motor rotates at full speed. If it is always low then the motor is disconnected and so doesn't turn. By using PWM we can control the speed of the motor from 100% to 0% duty cycle. So pin 1 is the 'throttle' or 'accelerator'.

Pin 2 - Sets the direction of the motor - Forward or Reverse. If this input is high then Input 1 is high, which turns on the transistor so Input 2 is low. This makes the motor turn one way. If this input is low then Input 2 is low, the transistor is off and so input 2 is high, and the motor turns the other way. But the cool thing is that if this pin is disconnected then Input 1 and Input 2 are both in the same state which means that the motor will brake. How do you 'disconnect' a wire that is soldered in - all we do is change the micro-processor pin to be an input pin and the built in resistors make this wire 'disconnected'.

 

So here is a logic chart

Pin1 Pin2 Description
High Output High Rotate motor clockwise
High Output Low Rotate motor anti-clockwise
Low (Any) High Coast
Input Any Brake

 

 

 

The transistor Q1 does not have to be a BC108 it can be ANY general purpose NPN transistor.

 

This circuit is utilised by many of the forthcoming examples so lets come up a price list for ONE motor now:-

 

2 x 10k 0.25w resistors

1 x 1k 0.25w resistor

1 x BC108 or general purpose NPN transistor

 

Price wise:- its less than $1

 

3 - DC Motors

There now follows various circuits for driving DC motors. The circuit most suitable for your project will depend mainly on the current consumption of your motors.

 

 

3.1 - 600mA DC motor controller, up to 36v

The following circuit is based on the L293D which has several plus points for a minimal controller. The 'D' on the end of the 'L293D' means that it includes output diodes meaning that you need less additional components at the expense of a smaller current output.

 

 

For the L293D datasheet see http://www.st.com/stonline/books/pdf/docs/1330.pdf

 

This controller is limited by the abilities of the L293D:- it can provide 600mA per motor or a maximum of 1.2 amps for a micro-second.

 

The motor supply can be between 5 and 36 volts.

 

Each motor requires two pins from the micro-controller and this uses the 'tri-state switch' stage mentioned earlier.

 

Cost

This requires one tri-state switch per motor;

one L293D for every two motors. The L293D costs about $4.30

So about $6 in total - for driving two motors

 

NB I now recommend that you use an SN754410 instead of the L293D. This is a direct plug in replacement and has the advantage it can provide 1A rather than 600mA and it also costs about 98 cents !!!

 

 

 

3.2 - 1 Amp DC Motor Controller, up to 36v

There are two ways to do this:-

 

Either: use the previous 600mA circuit but replace the L293D with a Texas Instruments SN754410 which is a direct pin for pin replacement. This is the preferred solution as the chip costs 98 cents and you dont need the additional diodes.

 

Alternatively:- do the following.

 

This circuit is similar to the 600mA version except that:-

1. It uses the 'L293' rather than the 'L293D' See http://focus.ti.com/lit/ds/symlink/l293.pdf

2. This chip allows a higher current of 1 amp per motor but requires us to add the diodes at the output stage. These should be fast, ie Schottky, diodes that can cope with the 1 amp current and a reverse voltage greater than, or equal to, the voltage of your motors. The motor voltage is still limited to 36v.

 

Costs

This requires one tri-state switch per motor;

one L293 for every two motors. The L293 costs about $5.81

You will also need 4 diodes per motor. The cost of these will vary depending on the voltage of the motors, and the current, but lets say about 50 cents each - ie $2 per motor.

So about $12 in total - for driving two motors

3.3 - 2 Amp DC motor controller, up to 50v

This circuit is similar to the previous ones.

It uses the tri-state switch to control speed, direction, and braking.

Schottky output diodes are used that match the current and voltage for your motors.

The only 'real' difference compared with the 1 amp circuit is that we using an L298 rather than one of the L293 series. The L298 lends itself to mounting a heatsink, due to the higher current, but is a bit of a swine to solder onto strip board or matrix board and is more designed for PCBs. It uses a 'Mulitwatt 15' package - and comes with two strips of pins each using a standard 0.1" pin separation. But the problem is that the second bank of pins is offset by 0.05". So if you are using strip/matrix board then you will need to be very carefull when bending one set of pins to fit.

Cost

This requires one tri-state switch per motor;

one L298N for every two motors. The L298N costs about $3.57

four diodes per motor at about 50 cents per diode.

So about $11 in total - for driving two motors

 

 

 

 

4 - Stepper Motors

If you are unfamiliar with Stepper Motors then see my Stepper Motor tutorial http://www.societyofrobots.com/member_tutorials/node/120

 

This will show you how you can control each coil of a stepper motor and also introduces you to the different drive modes: wave, full-step, half-step etc and talks about the advantages/disadvantages of each mode. A working motor driver is disussed there. However: the draw back is that the above tutorial requires a large number of your I/O pins and it also requires your microcontroller to control these pins to create the required drivng mode. So, as a theoretical example, it helps you understand how the motors work but will refer you back to this page for a more 'practical' example.

 

Assuming that you now understand the difference between uni-polar and bi-polar motors, and the different drive/pulsing modes then lets see how we can improve the design.

 

Welcome to the L297 !!!!!!

 

The L297 chip is designed to make driving stepper motors easy! It allows us to remove the responsibility of coding the different outputs for the drive mode we want to use. This is GREAT as it means we can tell the L297 that we want to click the motor by another step- and the L297 is responsible for telling the motor how to do it. As we will see - the L297 can drive both bipolar and unipolar motors and so allows us (from the microcontroller end) to treat all stepper motors in the same way.

 

Check out this datasheet for the  L297

 

The L297 has its own oscillator (or heartbeat) so that it can generate all of the required drive modes. This is produced by a resistor and capacitor network (R3 and C1 in the later diagrams). Each L297 can drive a finite number of coils/motors and so additiomal L297s may be required if you need more motors - but they can all share the same oscillator. This can be done as follows: on the first (primary) chip connect the R3/C1 oscillator to pin 16 (as per diagram) - for other L297s connect their pin 16 to ground and their pin 1 to pin 1 of the primary chip.

 

Refering to the diagram you will note that pins 20 (RESET) and 10(ENABLE) have been tied high as it is doubtfulll you will want to waste further microcontroller pins to control them.

 

Pin 19 (H/F) allows you to choose whether the L297 will manipulate the output pins to produce Half step drive, or full step drive - ie it changes the sequence of the outputs.

 

Output pins A,B,C,D,INH1,INH2 are the output pins that send the correct drive mode signals to the motor driver logic.

 

 

This chip means you can forget about how to send pulses to the coils - it does it for you!

 

All you have to worry about are the two input pins:-

Pin 17 - determines if the motor rotates one way, or the other

Pin 18 - is the clock - each tick will move the motor by one step

 

NB This is very similar to my DC Motor tristate switch in that Pin 18 is a PWM accelerator pedal, and Pin 17 is forward or reverse. So now we can control DC Motors and/or stepper motors with the same microcontroller code !!

 

Chopper

Ok - so there is yet another cool thing about this chip. - it saves power !!

 

Going back to fundamentals - each coil in the stepper motor has a small resistance, and so requires a large current. But this current only moves the motor over a 'step' when other coils are energised at the appropriate time. Having moved a 'step' then any further current is 'a waste'. So think of it as - 'the motor requires a high current kick to move a step', but having done so then we can reduce the currrent to a minimum until the next 'kick'.

 

How is this done? Well, assume that you have a resistor in series with your motor. Since Volts=Amps x Resistance then the voltage is proportional to the current. So if we monitor this voltage (on pins 13 and 14) then we are monitoring the current through the motor coil. This voltage is called a sense voltage. When we ask the L297 to perform another step then: it applies voltage to the coils and as the current increases then so does the sensed voltage. This is compared against a reference voltage on pin 15.

 

When the current level triggers the "over current sense", the L297 goes into Chopper/PWM mode and adjusts the current to the maximum allowed (By Vref). It does so by modulating either the phase lines or the inhibit lines, depending on the logic level on Control (pin 11). Its a closed loop feedback system!! Since, at build time, we dont know what the value on pin 15 should be then its easiest to use a potentiometer/trimmer to allow us to adjust the value at runtime. This will then allow us to control the trade off between torque and power consumption.

 

Next we will look at practical circuits for both Bi-polar and Uni-polar Stepper motors.

 

 

 

 

 

 

4.1 - BiPolar Stepper Motor

This circuit is suitable for driving bi-polar stepper motors at up to 2A per winding at a voltage between 6v and 26v. This means it can also be used with uni-polar motors by ignoring their center tap and just using the connections to the end of their coils.

 

The main driving stage is provided by IC1 - an L298. This, in essence, changes the TTL levels at it inputs (pins 5,7,10,12) into high powered outputs (on pins 2,3,13,14).

 

Diodes D1 to D8 are fast Schotkky diodes. The exact part number will depend on the voltage you are supplying to the motor and the current passing through the coils of your motor.

 

Capacitors C2 and C3 filter out high frequency spikes and smooth out large current requirements from your motors.

 

I have also included 2 jumpers to allow the selection of various options. Of course you can dispose with the jumpers and just hardwire them. If you are breadboarding the circuit first then you can find the best jumper settings for your motors/situation and hardwire them accordingly. Otherwise use a 3 pin header and a jumper to make the selection at runtime.

J2 is used to set pin 19 of the L297 high or low and controls whether the chip emits a half-step or full-step drive.

J1 is used to set pin 11 of the L297 high or low and controls whether the 'chopper' circuitry works on the coil voltage phase outputs or on the inhibit outputs. For a discussion on this topic see page 10 of the L297 specification.

My diagram shows the output of these jumpers being passed on to subsequent L"97s if you have more than one motor - of course this is up to you - you may prefer to set the jumpers for each motor individually.

 

Approximate price to drive one motor:

1 x L297 @ $9.90

1 x L298 @ $3.57

8 * diodes at say 50 cents each $4.00

C1,R3,R4,R5,R6,C2,C3 you may have in your spares bin but lets say $1.00

Trimmer R7 say $0.30

R1, R2 - the main thing to note is the current going through your coils will go through these resistors. Remember Watts = Amps * Volts and Volts = Amps * Resistance. So if your motor requires 1A per coil. Remember the reference voltage on pin 15 can be a max of 2.5V so lets assume we go with 2v. Then the resistor value should be:-

Volts = Amps * Resistance

So Resistance = Volts / Amps = 2 / 1 = 2 Ohms

The resistor power value would be: Watts = Amps * Volts = 1 * 2 = 2 Watts

On this basis then 2 x 2 Ohm 2 Watt resistors would be about 30 cents each = $0.60

So a total of around $19 per motor.

 

 

4.2 - UniPolar Stepper Motor

This circuit is suitable for driving uni-polar stepper motors at up to 1.5A per winding at a voltage up to 50v.

 

The main driving stage is provided by IC1 - an ULN2075BN. This, in essence, changes the TTL levels at it inputs (pins 3,6,11,14) into high powered outputs (on pins 1,8,9,16) and provides the same 'sense' values as the L298 did for the bipolar driver. Since this chip has no concept of 'inhibit' then we have also added IC3 a Quad AND gate which uses the inhibit outputs from the L297 to disable the output from the L297 from the inputs to IC1.

 

Diodes D1 to D4 are fast Schotkky diodes. The exact part number will depend on the voltage you are supplying to the motor and the current passing through the coils of your motor. But you will notice that this requires half the number of diodes compared to a bipolar motor driver.

 

Capacitors C2 and C3 that were used on the bipolar circuit are effectively no longer needed - since the motor supply voltage is only applied to the center taps and has no other interaction with the circuitry.

 

I have also included a jumper to allow the selection of various options. Of course you can dispose with the jumpers and just hardwire them. If you are breadboarding the circuit first then you can find the best jumper settings for your motors/situation and hardwire them accordingly. Otherwise use a 3 pin header and a jumper to make the selection at runtime.

J1 is used to set pin 19 of the L297 high or low and controls whether the chip emits a half-step or full-step drive.

The bipolar motor driver had a second jumper on pin 11 but this is not required for the unipolar circuit and pin 11 is just tied low.

 

 

 

Approximate price to drive one motor:

1 x L297 @ $9.90

1 x ULN2075BN @ $2.85

1 x 4081N @ $0.45

4 * diodes at say 50 cents each $2.00

C1,R3,R4,R5,R6 you may have in your spares bin but lets say $1.00

Trimmer R7 say $0.30

R1, R2 - the main thing to note is the current going through your coils will go through these resistors. Remember Watts = Amps * Volts and Volts = Amps * Resistance. So if your motor requires 1A per coil. Remember the reference voltage on pin 15 can be a max of 2.5V so lets assume we go with 2v. Then the resistor value should be:-

Volts = Amps * Resistance

So Resistance = Volts / Amps = 2 / 1 = 2 Ohms

The resistor power value would be: Watts = Amps * Volts = 1 * 2 = 2 Watts

On this basis then 2 x 2 Ohm 2 Watt resistors would be about 30 cents each = $0.60

So a total of around $16.50 per motor.