# Stepper Motors (on mbed, not arduino)

There is some information on chiphacker about using an arduino to control stepper motors (I'm driving Bipolar, 4 wire, motors), however I'm finding it hard to know how to control a stepper motor using my mbed device.

The fact that I'm using an mbed is really by-the-by, I've rigged up an H-Bridge so that I have 4 pinouts of my microcontroller/microprocessor going to the required pins of the H-Bridge What pulses should I send to which of the pins when? I can PwmOut if thats more useful. Like this:

Any advice would be very welcome, even if its just a URL to a reasonably detailed tutorial.

• Is your problem that you don't know how to use your mbed to send signals, or that you don't know what signals to send? Or both? :)
– davr
Commented Mar 15, 2010 at 19:44
• The second, I'm happy sending whatever I need to from mbed, but I'm not sure what pulses to send! I do need to work on the phrasing of these questions a bit :P
– JP.
Commented Mar 16, 2010 at 10:38

Check out "jones on stepping motors" a pretty complete source of information including pulse tables for a variety of motor types.

Microchip has a lot of information on motor control, including steppers:

To make that kind of stepper rotate, you need to produce what amounts to a two-phase (90deg apart) system of currents in the windings.

If your stepper has legs labeled A and B for one winding, and C and D for the other, you'd want to cycle through the following pattern of currents

+A -B  and  +C -D    .. where +A -B means current into A, returned on B, etc.
+A -B  and  -C +D
-A +B  and  -C +D
-A +B  and  +C -D


Each cycle through those four states will take the motor through one electrical revolution. Steppers usually have a large number of poles, so it will take some number of cycles before you get one complete mechanical revolution of the motor's shaft.

Now you have two H-bridges, one for the AB winding, the other for the CD winding. This is four controller bits per winding, eight in all. (It would not be premature to optimize this down to four bits right away, but I'm going to skip doing that for just a moment)

Referring to the high-side drive for leg A as AH, and the low side as AL, and so on, you can produce the above sequence of currents by turning on the drive transistors this way

AH AL  BH BL  CH CL  DH DL
1  0   0  1   1  0   0  1
1  0   0  1   0  1   1  0
0  1   1  0   0  1   1  0
0  1   1  0   1  0   0  1


By now it should be obvious that the H and L controls for a given half-bridge are always the negation of one another. In fact, if you ever drive the high and low sides of the same half-bridge on, you'll smoke the bridge, so it's safer to output just the one control bit per half-bridge, and use a hardware inverter to get the complementary control.

You should also notice that the sequence never turns on both high-side drives (or both low-side ones) for a given winding. The B drive is the complement of the A drive. So you could in fact get this down to a mere two bits.

I think if I had to drive this kind of a motor from a micro-controller, I might use a 7474 dual D flip-flop (D-FF) as a kind of external 2-bit register with dual rail outputs. Each data input of the 7474 would be hooked to its own micro output bit. A third bit from the micro would drive the clock. Then, Q1 would drive AH and BL, Q1* drives AL and BH, and similarly, Q2 feeds CH and DL, while Q2* goes to CL and DH. Each FF bit effectively sets the direction of the current in one of the windings.

Then, in software, you just output the two bits to set the current directions the way you want, and clock them into the FF by driving its clock low and then high again. Then your two-phase motor sequence is

clk AB CD
0   1  0    ; be sure to get D-FF inputs ready with the 0 state of the clock
1   1  0    ; b/c the 7474 will latch the values on the positive edge.
0   0  0    ; you want the data lines stable during the clock edge.
1   0  0
0   0  1
1   0  1
0   1  1
1   1  1


So, at a cost of three bits, you can step the motor, although there's always a current through both coils, so you can't free-wheel the motor.. unless you use a 4th bit to act as a global enable for both sides of both bridges.

• no on 2nd thought, i'd probably just use an inverter to get the complementary signals, and do away with the D-FF and the need to clock it. Commented Jun 12, 2010 at 3:45