Monday, 17 September 2012

How to drive a stepper motor with a PIC microcontroller

We've made a few posts about this before, while investigating how stepper motors work, but with our recent CNC drilling machine competition entry, we're getting a few questions once again about how it works. So here's a brief outline of what's happening when you control your stepper motor.

If we were to build our stepper driver from discrete components, it'd look something like this:

Stepper Darlington


The stepper motor has a simple operation. By energising coils in a specific sequence, we can make the motor turn clockwise or anti-clockwise. (energise them out of sequence, however, and the motor simply chatters or makes a buzzing noise!)

What often confuses people is that we're not switching the power rail, but the 0v/ground rail. Let's take a look at the layout above.

Ignore the diodes. We'll come on to those in a minute.
Those funny transistors are basically one transistor feeding the gate of another. It allows teeny-tiny currents to be amplified quite a lot. Basically, it means we can power really beefy loads (motors) with really small signals (microcontroller outputs). Such transistors are often called darlington transistors.

Now, every coil is permanently connected to the power rail. It's only by allowing current to flow through each transistor connecting the 0v/ground wire to ground that the coil can be energised. So when we make PORTB.7 high on our micrcontroller, it allows current to flow from the power rail, through coil 4 and down to ground (through Q1), effectively energising coil four. If we send PORTB.7 low and bring PORTB.6 high, current can now flow from the power rail, through coil 1 and down to ground (through Q2). This energises coil one. This causes the stepper motor to start to rotate anti-clockwise. Triggering the other coils in the correct sequence causes the motor to rotate.

Now, those diodes. What are they for?
When a coil is energised, it creates an electro-magnetic field. When you turn the coil off and the magnetic field collapses, it can create what's known as "back-emf" in the wires. Basically, we could get a spike of voltage - if we leave this hanging around, it could easily zap some of our sensitive components. So we use a diode to direct any harmful back-emf safely to ground.

Luckily, we're using a ULN2003A darlington array.
Unfortunately, this is often were the confusion comes in!


Those free-wheeling diodes shown on the datasheet can cause confusion. Trying to work out how to wire them up without a reference schematic like the one above can hurt your brain, but the trick is to remember that power is always connected and we're switching the 0v rail.

So the common pin is not 0v/ground as would be expected, but the power supply. The internal "free-wheeling" or feedback diodes simply ensure that any back-emf is handled safely.
So to connect a microcontroller to a stepper motor:


Following this logic, we've connected two stepper motors and two servos to our PIC to create a CNC drill controller board:

CNC drill schematic

No comments:

Post a Comment