Stepper Motors (2)

This tutorial will discuss:-

1. Why, or why not, choose stepper motors
2. The physics of stepper motors (ie how they work)
3. The electronics to control a uni-polar / bi-polar stepper motor
4. The software to drive the hardware

01 - Scenario

Rather than being a purely theoretical discussion we will follow my practical experience of buying, and controlling, a stepper motor. To make this more of a ‘closed loop’ tutorial I will try to explain how my findings would change my next purchase of a stepper motor and where I, now, think that they are better/least suited. Of course your experience or requirement may be very different from mine so ‘viva la difference’ – my only objective is to try and help understanding!! However, some of the discussion may help you to select a stepper motor that is fit-for-purpose.

02 - Why, or why not, choose stepper motors

So you may currently have experience of the following kinds of drive motors:-

1. DC Motors

Apply some kind of voltage and they spin in one direction or another at some kind of speed.

2. Servos

Apply a pulse between about 1ms and 2ms (or thereabouts) and the motor will spin one way or the other at some kind of speed.

Notice how these last two statements were rather vague? That’s because each motor is different from the next (i.e. the manufacturing process is not perfect), and the response may vary depending on your battery voltage, how much current your battery can provide, even temperature! So the way the motor behaves can change with time and environment.

The classic ‘tell tale sign’ is – connect either 2 servos to the same pulse, or 2 DC motors to the same battery, put the robot on the floor, and see if it goes in an exact straight line….’no it doesn’t’ and a \$100 fine to anybody who says that it does!

Lets forget extra problems like slightly different wheel circumferences, wheel slip on a smooth surface etc – basically any 2 motors never, quite, perform in the same way. Fact. And these errors get worse with time – i.e. after 10 seconds your robot may have moved 4 inches ‘off the straight and narrow’ – imagine where it might be after 60 minutes!!! So this kind of control is called ‘open loop’ – as there is no feedback from the output back to the input that changes behaviour.

The traditional method to fix this issue is to add Encoders. These allows you to work out how fast the motors are actually rotating so that you can change the value sent to your motor so that they can be used for a more reliable sense of position or direction. This introduces other topics such as PID. These are ‘closed loop’ systems since something about the output (from the encoders) is being feed back to the input to change future outputs and achieve a desired result. This feedback ‘closes the loop’.

Why all this complexity in a non-stepper environment? Well its basically because:-

1. You know what you want your motors to do

2. You tell the motors what to do

3. As all good teenage motors do - they don’t quite do what you ask them to

4. So you use encoders to find out what they actually did do

5. So that you can go back to step 2 and give them a good telling off to make sure they do what you want

“Yeah – I know all that – I came here to find out about stepper motors.”

If you apply a voltage to DC motors they spin, apply a pulse to a modified servo then they spin – apply either to a stepper motor then it doesn’t spin at all!! The principals will be discussed in the next section – but, in basic terms, you need to supply pulses to different parts of a stepper motor in a given sequence. Each of these pulses will make the motor step a given number of degrees – no more and no less. So if your motor has a 10-degree step then 36 ‘steps’ or ‘clicks’ will make it move through one revolution (ignoring gearing – see later).

So why is this different? For a DC motor we gave it a voltage and used an encoder to find out how fast it was running. For a 10-degree stepper motor we can issue 36 steps and know that it has done one revolution, say.

So in basic terms: with a DC motor use an encoder to find out what the motor did. With a stepper motor then it only does what you asked it to do so you don’t need the encoder (maybe?).

Of course – all motors and encoders lie – ie if your robot is going full steam ahead and you immediately ask it to go full steam in reverse then a DC motor encoder may lie (as it doesn’t realise you’ve just made a 6ft skid mark whilst the motor was changing) and the stepper motor may also have been rotated against its will due to momentum and since you never asked it to do this then you are also unaware.

Depending on the speed, acceleration, weight (ie momentum) of your robot then a stepper motor may do a much better job in an ‘open loop’ system than a DC or servo motor.

Why not use a stepper motor?

Stepper motors are ‘slower’ than DC motors and servos. As we will see in the next section each ‘step’ creates a physical movement. This movement has to finish before you can generate another step. So there is a maximum frequency at which you can generate steps. If you exceed this frequency then it will probably mean that the motor just fails to turn. So before you buy a stepper motor then make sure you know its ‘frequency’ as this will dictate the maximum speed (in revolutions per minute) that you can run the motor– see my practical example later.

You may, by now, think that a stepper motor means you can do without an encoder – but you still need to consider errors caused by skidding, or momentum, etc as mentioned earlier.

03 - Physics of an imaginary stepper motor

The physical operation of a stepper motor is performed by magnetism – yes, magnets! So here are some basics to do with magnetism:

1. Magnets have two poles – just like the earth – called North and South

2. ‘Opposite poles attract’ – ie the North of one magnet will create a physical attraction (pull) for the South pole of another magnet. And vice-versa.

3. ‘Similar poles repulse’ – ie the North poles of two magnets will create a physical repulsion (push) for the North pole of another magnet. The same goes applies for South and South.

4. Some magnets are ‘permanent’ – ie they are always magnets with North and South being in the same place. Remember those horseshoe shaped magnets you had as a child or used at school to mess around with iron filings?

5. Take a metal bar and wrap some wire around it. Putting current through the wire will turn the bar into a magnet. This is called an electro-magnet. Depending on which direction the current is moving through the wire will decide which end of the bar is North and which is South.

So lets create a simplistic model of a stepper motor (NB this is very simplistic so don’t try it – it may not work in practice - its just a theoretical example!).

1-Lets take a bar magnet (ie a metal bar that is a permanent magnet with one end always being North and the other always being South). Lets call this ‘Bar A’.

2-Now take a non-magnetised metal bar, wrap some wire around it to create an electro-magnet and call it ‘Bar B’.

3-Lets bang a nail through the middle of Bar A so that it can spin – ie it drives the wheel of our robot

4-Lets superglue Bar B to our work surface with one end of the bar pointing at the nail in Bar A.

So now we have something like this:-

So now we turn on the supply across Bar B so a current runs through it and it becomes a magnet. Lets assume that the left end of Bar B becomes South. So now we have:-

So now we have two South poles facing each other and we know that ‘similar poles repulse’. Bar B is super glued to the table so only Bar A can move – and that has a nail in it so it can only spin. So Bar A starts to spin. Whilst the South pole of Bar A spins away from Bar B then this ‘repulsion’ becomes less. But wait a moment – this means that North pole of Bar A gets closer to the South pole of Bar B and we know that opposite poles ‘attract’. So this will ‘pull’ the North pole of Bar A towards the South pole of Bar B

Until, eventually, Bar A has spun a complete 180 degrees.

At this point: Bar A will stop spinning because its North is ‘pulled’ by the South pole of Bar B.

We have just made one ‘step’ – but, now what! Well, we reverse the voltage in the wire around Bar B so that the magnetism of its poles get ‘flipped’ – so we now have:-

So now we have two North poles facing each other and we know that ‘similar poles repulse’ So, just like before, Bar A will do another spin through 180 degrees until, after our second ‘step’, we get:-

Reverse the current in Bar B, and hence the polarity, and we are back to where we started:-

and so we keep on going.

Hey we have a motor ! Even though it is super glued and nailed to our dinning room table!

So how do we control the speed of the motor and what limits the maximum?

In our simplistic example, then the quicker we change the polarity of Bar B then the quicker the motor (Bar A) will turn. However, in practice, this is not the case since the switch in current on Bar B has to co-inside with the position of Bar A so that the necessary ‘push’/’pull’ is applied at the correct time. Hence this depends on how fast Bar A can rotate and, since this is a physical movement, it does take a minimum finite amount of time. So assuming that we have:

and Bar A starts spinning (because South repels South). But then we change the polarity of Bar B almost immediately. Then Bar A is attracted back to where it came from and so the result is ‘No Movement’ even though you have sent ‘2 steps’. This physical movement duration therefore dictates the maximum frequency at which you can issue new step commands. A typical motor frequency may be 400Hz – ie you can do a maximum of 400 steps per second.

So what have we learned? We have a basic understanding of how stepper motors work and we have created a motor that turns 180 degrees for each step – so 2 steps make Bar A spin though a complete 360 degrees. We have also learned that a motor has a maximum number of steps per second.

04 - Physics of a real stepper motor

So lets now leave our imaginary world and look at how this extends to real stepper motors.

Our rotating ‘Bar A’ only had two poles – one at each end of the bar – and so we only got two steps per revolution. A real stepper motor may have, for example, 48 steps per revolution and each step would therefore move through 360/48 or 7.5 degrees per step. How does this happen? Well, rather than it being a bar, then think of it as a cylinder where every 7.5 degrees the polarity is either North or South but it always alternates from one to the other. That’s why stepper motors always have an ‘even’ number of steps. Here’s what it may look like if we look down on it and there are only 8 steps:-

Now instead of one Bar B we actually have two. Each of these two electro-magnets will have a coil around them. To complicate things slightly there are two different ways that these coils are configured: bi-polar, and ‘uni-polar’.

We will look at Bi-Polar first because it is the simplest to understand whereas a Uni-Polar motor has other advantages and disadvantages but can, when required, be used as if it was a bi-polar motor. We will find out why later.

4.1 - Bi-polar stepper motors

A bi-polar stepper motor looks like this:

As you can see it has two coils and will normally therefore have 4 leads. If you have no documentation for your motor then you can just use an ohmmeter to work out each pair of pins. I.e. 1a and 1b will have a low resistance between them, and 2a and 2b will also have a low resistance. If you measure across two pins and have an infinite resistance then they are from different coils. So once you know which two pins are coil 1 and which are coil 2 then you will still need to find out which are ‘a’ and which are ‘b’ – but more on that later.

Bi-polar stepper motor driver

To drive the motor then let us consider one of the coils – say coil 1. In order to become an electromagnet we need to be able to change the direction of the current through the coil. So each side of the coil needs to be either or low.

If  ‘1a’ is high and ‘1b’ is low then we will have a current through the coil.

If  ‘1a’ is low and ‘1b’ is high then we will have a current through the coil in the opposite direction.

In the other two cases: where they are both high, or both low, then no current flows and so it is no longer a magnet.

A suitable circuit to do this as an H-Bridge. As you will see from that link – you need four switches in the H-Bridge to be able to give the four control states we have mentioned above.

4.2 - Uni-polar stepper motors

A uni-polar stepper motor looks like this:

The only difference is that each of the coils now has 3 wires and will normally there have 6 leads. The new wire on each coil is called a ‘centre tap’ and is connected to the middle of the coil. If you have no documentation on your motor then use your ohmmeter to check the connections. By checking for infinite resistance then you should be able to identify the leads that are for one coil, and the other leads for the other coil. Given a group of 3 leads you can tell which is the centre tap because, for coil 1, the resistance between ‘1a’ and the centre tap will be same as that between the centre tap and ‘1b’. The resistance between ‘1a’ and ‘1b’ will be double this value. NB the resistance tend to be very low, a few ohms, so you will need to select the appropriate resistance scale on your meter.

Uni-polar stepper motor driver

As mentioned earlier: you can drive a uni-polar motor ‘as if’ it was a bi-ploar motor. To do this you just ignore the centre tap and then use the other two leads per coil as if it was a bi-polar.

Otherwise: you want to use the centre tap and, assuming you connect it to ground, then you will need two switches to dictate which direction the current flows through the coil.

Note that this mode of operation means that you are only using half of the coil in each direction. This will mean the ‘half coil’ only has half of the total resistance. Using Volts = Amps x Resistance (and assuming your supply voltage is the same) then if the resistance is halved then the current drain has doubled.

So why would you choose uni-polar over bi-polar if it requires twice as much current?

Compared to the bi-polar H-Bridge driver, which requires ‘4 switches; per coil then the uni-polar circuit only needs ‘2 switches’ per coil. So less electronics!

The price you pay is that you may be using twice as much current and because you are using half of the coil at a time then you may only get half the torque. Despite its name (uni versus bi) it sounds as if it is less capable but remember you can always use a uni-polar motor as if it was a bi-polar by ignoring the centre tap. So a uni-polar can be thought of as a bi-polar with extra choices.

05 - Software driver

Previous discussions have described the way to wire up bi-polar and uni-polar motors. These configurations effect current drain, torque but don’t change the way they are programmed.

As we have already mentioned – unlike a DC motor – you cannot just apply a fixed current and get to the motor to spin. Instead you need to supply pulses to the coils in a certain order. There are various ways to0 do this and each one has trade-offs to do with torque vs current consumption. So now we will look at the possibilities:-

The most simple ‘pulsing’ is called ‘Wave Drive’ and sets the coils as follows:-

 T T + 1 T +2 T + 3 1a High Low Low Low 1b Low Low High Low 2a Low High Low Low 2b Low Low Low High

This method only energises one coil at a time. So current consumptions is low. But equally the amount of torque is also low.

The next ‘pulsing’ method is called ‘Full Step Drive’

 T T + 1 T +2 T + 3 1a High High Low Low 1b Low Low High High 2a Low High High Low 2b High Low Low High

This method energises both coils at the same time- so requires twice the amount of current but provides about 40% more torque compared to Wave Drive. The other benefit of this method is that 1a is always the ‘inverse’ of 1b, and likewise 2a is always the ‘inverse’ of 2b. So we could control the motor with two output pins from the controller and, in our motor driver hardware, we could use an inverter between 1a-1b and another between 2a-2b.

The last pulsing method is called ‘Half Step Drive’ or ‘Micro-stepping’.

 T T+1 T+2 T+3 T+4 T+5 T+6 T+7 1a High Low Low Low Low Low High High 1b Low Low High High High Low Low Low 2a High High High Low Low Low Low Low 2b Low Low Low Low High High High Low

This method alternates between energising 2 coils and 1 coil. So the current requirements, and torque delivered, sit half way between ‘Wave Drive’ and ‘Full Step Drive’. It also helps to avoidance something called ‘resonance’ which can occur when using ‘Full Step Drive’ at certain speeds. The other side effect is to double the number of steps per revolution – which helps accuracy – but halves the overall maximum speed.

What have we learned? There are at least 3 ways to drive a stepper motor depending on whether we want o optimise torque or minimise current consumption. Since these do not require hardware changes then we could dynamically change from one drive mode to another in software depending on circumstances. For example: on a flat surface we might choose ‘Wave Drive’ and when a tilt switch in the robot tells us we are going up a steep hill the then we could change to ‘Full Step Drive’. Since the choice of drive method effects current then you also need to make sure that the current requirement does not exceed the ability of your batteries or of your H-Bridge output stage components.

06 - Gearing

This is no different for a stepper motor than any other form of motor. Gearing is normally provided to increase torque at the expense of velocity.

Since we know that stepper motors are inherently slow then check the gearing ratio on your motor before buying!!

If you want it to drive the main wheels then finding out that the maximum rate you can send steps, plus the gering ratio, means that it takes 30 seconds to rotate your drive shaft through one revolution (even with incredibly high torque) may make the motor unfit-for-purpose.

07 - A Practical Example

So here is my experience: from purchasing to driving....

1 - Understanding what you’ve bought or are going to buy

First of all let me preface all of this by saying that I pretty much bought a motor at random (very cheap on eBay) so I’m not suggesting that this is a suitable motor for your project.

So here is what I purchased (at around \$11 for two)

http://docs-europe.electrocomponents.com/webdocs/0030/0900766b800305e5.pdf

http://docs-europe.electrocomponents.com/webdocs/001b/0900766b8001b258.pdf

which are less than informative. My motors arrived with no wiring/plugs etc so the colour charts were less than useful – at which I point I wondered if I would ever make them work!!

However:- this is what I could learn from the datasheets:-

• moves 7.5 degrees per step (ie  48 steps per revolution on the motor shaft)
• has 6 wires
• is a uni-polar motor
• has a 50:1 gearbox (so 50x48 or 2400 steps per revolution on the output shaft)
• holding torque = 100 Ncm
• working torque = 100 Ncm
• typical working torque = 65 Ncm
• Step rate at typical working torque=300Hz
• requires a 5V supply
• Resistance = 9.1 ohms per phase
• 550 mA per phase

So long as you have two of the last three parameters then you can calculate the other using the equation Volts =  Amps x Ohms

There is one potentially important statistic missing from these datasheets that can be calculated from the data given. If we can issue a maximum of 300 steps per second, (300Hz), and – due to the gearbox – we need to issue 2400 steps for a full revolution then the fastest we can turn the output shaft is (2400/300)  ie once every 8 seconds.  This may be rather slow for an output shaft if it is connected to wheels but could be fine if we are rotating the turret of a tank. So this is an important fact to ascertain before purchasing your motor.

So what have we learned?  The gearing, steps per revolution and the maximum frequency produce trade-offs between torque and speed. Whether the motor is uni-polar or bi-polar can also influence the required current draw, and torque, as discussed earlier.

2 - Connections to the motor

Assuming that the motors are suitable for the job then let’s move on. My motors have 6 pins and are uni-polar so I can use an ohmmeter to work out which 3 pins correspond to coil 1 and which 3 are for coil 2 as described earlier. I can also work out, for each coil, which lead is the centre tap but cannot decide which is one end of the coil versus the other (ie cannot tell which is 1a and which is 1b).

I’m guessing that if I get this wrong then all that will happen is that the motor my move in the wrong direction. Using a ‘breadboard’ should help me find out the correct connection. So I wired each coil of my motor to a plug with the centre tap in the middle and the other two wires connected to the outside. This means I can rotate the whole thing through 180 degrees if I’ve got it the wrong way around! The plug I’ve used is polarised so that once I know the correct connection then I can solder the matching header onto my circuit the correct way around. In order to connect my plug to the breadboard I made up a short length of 3 way wire with a male header at each end – which will come in handy in the future for connecting sensors, servos, etc to the breadboard.

So here’s a picture of my motor, with wires, plug and the cable to link it to the breadboard:-

3 - Wiring the motor driver

Since I’ve bought a uni-polar motor then I need two ‘switches’ per coil – as per my diagram earlier. However: I will forget about the center tap for now and just drive it as if it was a bi-polar motor. So I’ve chosen to use a L293D chip see http://focus.ti.com/docs/prod/folders/print/l293d.html

This contains four half H-Bridge ‘switches’ – so one chip can drive both coils on the same motor. One chip per motor.

I’m not going to use the ‘enable’ pins on the L293D so I’ve just connected them to pin 16 (the +5v logic supply). Each of the four input pins just maps to the equivalent (higher current) output pin.

Check the datasheet to make sure that the L293D can provide enough current for the motor you are using. NB if you need more current then the L293 supplies twice as much but you MUST then connect external diodes as shown in the datasheets. If that still isn’t enough then search this site, or Google, for other higher current H-Bridge circuits.

Here's how I connected the L293D

4 - Connecting the microcontroller

I’ve chosen to use my Baby Orangutan board because it can plug straight into the breadboard. This board uses an AVR ATMega168. If you want to use your \$50 Robot Board then that is fine as well:- just make sure that your \$50 Robot ground and +5v are also connected to the breadboard to drive pin 16 of the L293D. You can do this by connecting a lead from one of your unused analogue-to-digital ports and connecting the ground wire to the ground of your breadboard, and the +5v to pin 16 of your L293D. Obviously the 3rd pin of the connector (which would normally be used for the sensor input voltage) will be left un-connected.

Now we need to choose 4 output pins from the micro-controller that we will use to run the L293D.  I selected:-

Port C5 = 1a

Port C4 = 1b

Port C3 = 2a

Port C2 = 2b

5 - Check the first piece of code using LEDs

Before connecting the motor – I wanted to make sure that my code was, at the very least, doing what I thought it was. Just connecting the motor only to find out that nothing happens means you have to work out if there is a problem in your program, your hardware connections or, dare I say, if you’ve managed to fry some of the hardware. What better to give a clue as to what’s going on then our trusted friend the LED!!

So with the motor disconnected: so place a 1.6k resistor in series with an LED - which is then connected to ground -  on each of the output pins from the L293D - that is pins 3, 6, 11 and 14 - which we will call outputs 1a, 1b, 2a and 2b respectively.

The easiest sample code to write is for the ‘Wave Drive’, as discussed previously, which requires us to send pulses as follows:

 T T + 1 T +2 T + 3 1a High Low Low Low 1b Low Low High Low 2a Low High Low Low 2b Low Low Low High

So here’s some pseudo code:-

While(1){

Pin1a = high, Pin1b = low, Pin2a = low, Pin2b=low

Wait 1 second

Pin1a = low, Pin1b = low, Pin2a = high, Pin2b=low

Wait 1 second

Pin1a = low, Pin1b = high, Pin2a = low, Pin2b=low

Wait 1 second

Pin1a = low, Pin1b = low, Pin2a = low, Pin2b=high

Wait 1 second

}

This should make the LEDs flash in sequence: 1a, 2a, 1b, 2b.

Looking at my timing tables for the other two potential driving methods you should easily be able to adapt your test code to the other methods.

6 - Connecting the motor

Now that we have the LEDs flashing correctly we can connect the motor instead.

For each coil of the motor the centre tap is unused (as we are simulating a bipolar motor) and the ‘a’ and ‘b’ terminals are connected to the outputs of the L293D driver (instead of the LEDs).

We can now change our program so that instead of waiting 1 second between pulses we can send faster pulses. My motor has a frequency of 300Hz – so one pulse every 3.3ms. However: my timer only lets me wait for a given number of milliseconds so I shall round that up to waiting for 4ms.

Now turn on – and your motor should rotate. If it doesn’t then maybe you’ve got the frequency wrong. For example: if I try to ‘over-clock’ my motor by changing my delays to 3ms then it just hums and doesn’t rotate.

But what about reverse? Well if you’ve followed my wiring convention then you can rotate one of the motor connectors through 180 degrees to swap the a and b connections. That should work. Interestingly: I started by swapping the leads round for both coils – and it made no difference – so only reverse one of them. You could, of course, add additional circuitry to do this for you, but this would need an additional controller pin to select forward or reverse and, in my view, this is better done in software:-

While(1){

Pin1a = low, Pin1b = high, Pin2a = low, Pin2b=low

Wait 1 second

Pin1a = low, Pin1b = low, Pin2a = high, Pin2b=low

Wait 1 second

Pin1a = high, Pin1b = low, Pin2a = low, Pin2b=low

Wait 1 second

Pin1a = low, Pin1b = low, Pin2a = low, Pin2b=high

Wait 1 second

}

I encourage you to try out the other two driving methods: Full Step and Half Step. If you a meter then check out current consumption, see if the L293D gets hot (in which case you might need a heat sink), and look at other ‘Calculators’ on this site to estimate how much torque you might need.

My set up requires four output pins on the controller for each motor – which is a bit excessive. This can be halved by using ‘Full Step’ drive/ Looking at the table for this mode

 T T + 1 T +2 T + 3 1a High High Low Low 1b Low Low High High 2a Low High High Low 2b High Low Low High

you will see that the ‘a’ pins are always the opposite of the ‘b’ pins. So if the additional current usage of this configuration is acceptable, or is necessary in order to reduce the controller pins, then you can get away with only having controller pins for 1a and 2a. Then use a TTL inverter chip (they cost next to nothing and come in a package with 6 inverters on one chip) to connect 1a to 1b, and 2a to 2b - you will see an example diagram for this in the datasheet for the L293D I listed earlier.

8 - But what if you want to use the center tap of a uni-polar motor

If you want to use your uni-polar as a 'true' uni-polar motor then you need to use another hardware driver. The L293D contains 'half H Bridges' but what you actually need is effectively' a quarter'  - known as a 'Driver Array'. The ULN2003AN is a good choice. With the center tap connect to +ve supply you can make the current flow in the coil by connect the 'a' terminal of the coil to ground. To make it flow the other way then connect 'b' to ground. If both 'a' and 'b' then no current flows through the coil, and if both are low then the current flows both ways - which effectively cancels each other out. So we can do this by connecting one driver to the 'a' terminal and another to the 'b' terminal. So two drivers per coil makes four per motor. The ULN2003AN contains 7 drivers - so not quite enough to drive two motors from the one chip!!

08 - Practical Considerations

Check out the datasheets for your motor to make sure that your motor driver can cope with the stall current. This is the amount of current the motor will consume when it is trying to rotate but cannot move – ie it is trying to go through a wall or up a very steep hill. If you suddenly change the direction of the motor then you may also experience spikes that are twice this amount. Failure to check this info will lead to your motor driver (the L293D in this example) to melt.

If you are running in an open loop system (see earlier) and are reliant on each step issued by software leading to the motor physically turning one step then you must be aware of the following situations:-

1. Clocking the motor at, or close to, its maximum speed – and the motor experiences a hindrance (ie a hill or some other external force/torque) then it may cause the motor to skip a step. Obviously: the closer you get to the stall torque then the more likely this is to occur
2. Momentum will also play its part. If you try to go from full speed ahead to full speed reverse then either the motor will skip some steps, or the wheels may skid. Either way you’ve lost your accurate positional accuracy.

Stepper motors are slow!! Since each software ‘step’ requires a physical movement before the next step can be issued then even the slowest microcontroller will twiddle its thumbs between issuing steps. In my example: the fastest possible step rate was every 3.3ms which, even for an ATMega8 running at 1MHz, is once every 3300 instructions. So this makes the software control a good candidate for being interrupt driven. A good example of daisy-chaining additional code to a hardware timer is given in this post:

http://www.societyofrobots.com/robotforum/index.php?topic=541.0

This allows you to schedule jobs. In my example: schedule a job to run in 4ms, when it happens then issue the next step and re-schedule yourself to happen in another 4ms. Obviously the 4ms could be longer if we want the motor to run slower.

09 - Other cicuitry

From what we have covered you will notice that we need to use a large number of I/O pins per motor. So how can we reduce this?

I haven't tried it yet but a good alternative would be to use an L297

http://www.eu.st.com/stonline/products/literature/ds/1334.pdf

This handles the various different step modes in hardware rather than in your own software. Connection from the microcontroller just needs:-

1 pin to indicate Forwards/Reverse

1 pin for a clock waveform to dictate speed

1 pin to select full step or half step mode. However, if you are happy to keep with one mode then you wouldn't need this pin.

The other benefits of the chip is it uses a 'chopper' to make current control much more efficient.

This can then drive our L293 or a L298 for bi-polar use, or via a darlington array such as the ULN2003 for a uni-polar.

So this means that we can still keep the final output stage pretty much 'as-is' and just insert this chip between the microcontroller and the output stage to perform the step generation.

Haven't tried it myself yet - still waiting on some parts - but looks like a good choice.

UPDATE - see my new Motor Controller Tutorial  http://www.societyofrobots.com/member_tutorials/node/168

10 - Bibliography

Here is a good reference on stepper motor terminology:-

http://www.anaheimautomation.com/stepper_motor_glossary.aspx

Basic explanation of stepper motors:-

http://www.solarbotics.net/library/pieces/parts_mech_steppers.html

although the final chart has a bug for port 2a. The final blue bar should be moved one place to the right !! Man – debugging other peoples code……

Also:-

http://library.solarbotics.net/pdflib/pdf/motorbas.pdf

but they also have a bug on page 4, Table 1, the bottom 3 dots in the Half-step drive should all be one column to the left.