Help Microstepping!

Hi everyone,

I'm using Atmega328 and ULN2003A to drive a 5-wire unipolar stepper motor with a resolution of 200 steps per revolution.

The resolution is too small for my desired application, I've read that I can use Microstepping to increase the resolution.

How can I possibly do this? Can I do it with just some programming/algorithm or do I need to wire some components? What components do I need to add?

I need your opinions. I'm quite confused with the posts that I have read.


You can do half-stepping just by changing your stepping sequence table.

Full step:

Half step:

You can do microstepping, but you need all 4 coils to be driven off PWM pins, and then you vary the PWM duty cycle to get as many steps as you want. Step rate will be limited by the speed of your processor and PWM frequency.

Hi BetterSense ,

How can I possibly change the stepping sequence using the stepper library? I'm using a 4-pin configuration on a 5wire unipolar stepper motor.

Don't use the stepper library, or rewrite it.

You mean, it's better to force load values to control the motor instead of using the library if I'm planning to do halstepping or microstepping?

How can I rewrite the stepper library?

I would not bother rewriting the Stepper library; it wouldn't be worth rewriting it to be microstepping.

It is very simple to write half-stepping control. You don't need to use the library, just use the sequence I posted above. You can either use an array to step through the steps or you can use a switch statement. You could write a "step" function like this pseudocode. This is something like a state machine.

/* step sequence:
0: 0001
1: 0010
2: 0100
3: 1000
void step(bool dir){  //dir: 1=clockwise, 0=anticlockwise
    static int state;
    if (dir){state++;}
    else if (!dir){state--;}
    state=state%4;     //I don't remember if this works w/negative numbers in C
    switch state{
    case 0: PORTB=0b00000001
    case 1: PORTB=0b00000010
    case 2: PORTB=0b00000100
    case 3: PORTB=0b00001000

If you want to make microstepping, you do a similar thing, only you need to pick 4 PWM pins, set up PWM on them at a frequency at least 4 times higher than the highest microstepping frequency you are interested in (you will not be able to use analogWrite), and then adjust the duty cycle of each pin with each step, to follow a sinusoidal pattern. I drive a unipolar stepping with a 3200 step/revolution microstepping, but I don't have the source code with me right now.

Nice pseudocode Bettersense!
Try my code. It’s good to ±1/22 of a degree using half steps.
I tried microstepping with PWM, who would need 0.0004 degree steps?
0.0004 precision when stopping?
Can you think of an application for this?
Is there a narrow range of speeds where it would be smoother?
Under what mechanical circumstances would this matter?
I’d like to learn and add a feature to my library.
Can we see some example code?
I have seen that with a load, moving slow, with time between steps…
It gets stuck instead of moving 0.0004 degrees.
With less load it “jerks” moving nearly 1/11th of a degree at a time anyway, same as half steps.
Maybe someone might want 0.02 deg steps?
This would make it smoother at one specific speed only compared to half stepping.
I believe this technique is only useful with different hardware than 28BYJ.
Not sure what motor you have…

stepper2.pde (17.2 KB)

I tried microstepping with PWM, who would need 0.0004 degree steps? 0.0004 precision when stopping?

You are right; microstepping often does not achieve actual resolution equal to the number of steps. You are also right that half-stepping is about as high as you can go if you want repeatable positioning accuracy. If you need better, it is better to use full- or half-stepping and then hook the stepper up with a gear train.

In my case, I am using my stepper motor in such a way that I don't care about positioning accuracy; I just need the smoothest operation possible, and I have very low torque requirements. If you can drive a stepper with 2 sine waves it becomes basically a high-pole-count AC synchronous motor and there is no "stepping" action, so it runs with low vibration. Of course achieving a true sine wave would require high-power linear amp which is not worth it, so you use PWM and implement microstepping to approximate a sine wave. There is a reason stepper driver MFGs usually only offer steps down to 1/16th step or 3200steps/rev, because beyond this is diminishing returns. Actually I cannot detect much improvement between 1/8th step and 1/16th step in terms of smoothness.

Another way to achieve smooth motion is to of course use a normal motor and servo control, but it's just a tossup which is harder to implement.