Bipolar stepper motor control with Picaxe and L293D chips

28 January, 2009

I’ve now got a schematic and program for running a bipolar stepper motor via a serial interface (just as for the unipolar case). This is important for the robot arm cause because two of the three steppers will be of the bipolar kind.
Where driving the unipolar stepper required only current ‘pushing’ (ie all in the same direction through a common ground), a bipolar motor requires current to flow in both directions. The following image shows the difference.


In theory the control of this is very easy with two half H-bridges (essentially four transistors) controlled by several Picaxe output lines). The basic concept is shown below.



The L293D chip has the requisite transistors and a nice interface (with lots of extra features that my DIY skills couldn’t match). I’ve also used it in the mobile robot experiment to control the direction of 2 motors.

As with anything like this though, one needs to think through the sequence and outline a table of output values corresponding to the steps, as below.

Direction Coil 1 Coil 2
N + 0
NE + +
E 0 +
SE +
S 0
W 0
NW +

At first I was using 6 output lines (!) – 4 for each of the transistor inputs and 2 for enabling or disabling the L293D’s 2 ‘half h-bridges’. In this configuration it could be reduced to 4 by using an external NOT gate (because 2 of the controls are always the opposite of another 2). Such a scheme would look like this:

Direction EN1-2 1A 2A EN3-4 3A 4A
N 1 1 0 0 0 0
NE 1 1 0 1 1 0
E 0 0 0 1 1 0
SE 1 0 1 1 1 0
S 1 0 1 0 0 0
SW 1 0 1 1 0 1
W 0 0 0 1 0 1
NW 1 1 0 1 0 1

But while trouble shooting this (I had swapped LSBs with MSBs so it probably did work after all!) I came across a site tying the enable lines Hi and using 4 control lines. At the poles one of the loops must have zero current flowing through it. In the above scheme this is done by setting the enable line Low and ignoring the A values. In the 4 line scheme this can be achieved by sending the same control output to both ends of the loop (ie the potential difference across the coil is 0). Then the table looks like this (including half stepping):

Direction 1A 2A 3A 4A
N 1 0 0 0
NE 1 0 1 0
E 0 0 1 0
SE 0 1 1 0
S 0 1 0 0
SW 0 1 0 1
W 0 0 0 1
NW 1 0 0 1

That makes the schematic as follows:


…and the program as follows. Note that this program is designed to wait until it detects a serial input on input 0 (pin 17) in the form of three bytes: The first is a qualifier – 85 uniquely identifies this stepper motor – the second is the number of steps – 0-255, and the third is the speed – 0-127 is backwards, 128-255 is forwards.

'Serial driven stepper motor by DMT195

symbol posrotor=b0
symbol numturns=b2
symbol speeddir=b7
symbol direc=b8
symbol pulsegap=b3
symbol counter=b4
symbol modrotor=b1
symbol outbyte=b5

posrotor = 1 'set starting position

start:    'main sequence - wait for command then move the motor
 numturns=0   'sets a default number of turns
 high 7    'reset ready flag
 serin 0,T2400,(85),numturns,speeddir 'get serial data
 gosub getspeed   'get the direction and speed
 gosub move       'perform number of steps
goto start

 if speeddir>128 then  'find direction and store in direc (1 is +ve, 0 is -ve)
 'gets the speed 0-127 negative, 128-255 positive (higher =faster)

 for counter=1 to numturns
  pause pulsegap      'wait before moving again (set by speed)
 if direc=1 then
  posrotor=posrotor+1 'increase/decrease the step by one
   gosub moverotor  'set the rotor position for this step
 next counter

 modrotor=posrotor//8    'find out where the rotor arm should be (1 of 8 positions)
 lookup modrotor, (0x08,0x0A,0x02,0x06,0x04,0x05,0x01,0x09), outbuyte
 '(%00001000,%00001010,%00000010,%00000110,%00000100,%00000101,%00000001,%00001001) is (0x08,0x0A,0x02,0x06,0x04,0x05,0x01,0x09) in hex
 'looks up the step from the sequence and applies it to the output pins

Please leave comments if you’ve found this useful or if you think I’ve made a mistake.

I have something to say about accessing all these devices through a single serial port too but that will have to wait for one or two more tests…

Update: Of course this can be done with a PICAXE 08(M)  with the 4 outputs and 1 serial input to spare. The pinouts would have to change as would the respective program. If there’s enough demand for me to document this I will. Let me know in the comments. Consider using this little board from Technobots.

Update 2: I’ve reduced the code length by using a lookup table instead of the select/case structure. I’ve also use hex to reduce the size of that line (but included the binary values in a comment below for those that are interested).

The easy way

13 January, 2009

I’m keen to get back into robotics now that my PhD is nearing an end (although it’s not over yet :-|). I’ve been researching a lot to work out the best thing to attack to make the most effective use of my time. The last thing I did was to buy some acrylic sheeting to start making stuff – possibly an arm.

Anyway, I came across this stepper motor driver on my travels. Sparkfun is quite a well known company in the hobbyist electronics market so I’m surprised I didn’t see it when I was researching my stepper motor controllers. It has three great things going for it: i) it’s tiny, ii) it looks really simple to get running with, and iii) it’s dirt cheap <$15 (or £10).


I thought I’d mention it if someone’s hunting for such things.

My First geeky YouTube video

9 April, 2008

It’ll be video podcasts next ;-)

YouTube – Unipolar stepper motor controller PCB with serial input

Committed to a PCB

6 April, 2008

So here is the unipolar stepper motor controller committed to a PCB. It’s my first PCB that I’ve made and populated from scratch. I used the toner-transfer method and then drilled out the holes with a small drill. I’ve got a programmer on there so I can upgrade the firmware if needed. The connector for power and a serial command is a bit of a hassle at the moment but will fit in with my grand scheme! Note: this need regulated power, which I’m assuming will come from a master board (the supplier of the serial). This makes sense as I’ll have multiple (modular) controller boards.Unipolar stepper motor controller PCB

PICAXE unipolar stepper motor controller

8 March, 2008

Here’s my stepper motor controller, based on an 18 pin PICAXE microcontroller. The idea is that this chip takes away the processing overhead associated controlling a stepper. A master CPU (of some kind, potentially a PC) can send a serial command to the PICAXE telling it how much to move and at what speed (and direction). A Darlington driver array controls the power and a 7-segment display shows the current coil powering. Here’s the schematic (PICAXE programming connector not shown):


The way I’ve programmed this is different to other approaches I’ve seen. These tend to complete a full rotation every time. I’m sure others have done controllers that manage sub-rotation control like this but maybe not the way I’ve done it. I basically have a single value which defines how far the motor has rotated. I then increase this number step wise and use modulo division (//) to find out what position the rotor should have.

Program and video of operation after the break…

Read the rest of this entry »