Instructables

Drive a Stepper Motor with an AVR Microprocessor

Step 6: I never half-step, because I'm not a half-stepper...

Quest lyric aside, half-stepping your motor is where it's at. You get more peak current, more instantaneous torque, and twice the angular resolution.

Half-stepping in a nutshell: Instead of Blue, Black, Red, Yellow, you drive the motor with Blue, Blue+Black, Black, Black+Red, Red, Red+Yellow, Yellow, Yellow+Blue. The upshot is that for half the time you're engaging both magnets at once.

And during the times that both sets are engaged, the motor points halfway between the two, shrinking the angle between "steps" and making the motor turn more smoothly. Can you tell from the video? I'm not sure...

Now the part of the code that does the half-stepping looks like this:
void halfStepping(uint16_t delay, uint8_t direction[]){  uint8_t i;  for ( i=0; i<=3; i++ ){	    PORTB = direction[i];	/* single-coil part */    _delay_ms(delay);        PORTB |= direction[i+1];	/* add in half-step */    _delay_ms(delay);  }}

The first PORTB command sets a single pole to positive and all the rest to negative. Then it waits. Then the second PORTB command sets a second pole (on the other winding) to positive, engaging both windings for 1.4x the torque (and 2x the current).

A full program listing is attached below. Two arrays are now defined (clockwise, counterclockwise) and both have 5 elements each to allow for the i+1 entry in the halfStepping function.

 
Remove these adsRemove these ads by Signing Up
TurboTronix6 years ago
Yes it works but not clean. I need to get the H-bridge going... Also can you by any chance give me an example code where I'll use this setup with an input pin (i.e. a photocell which will base the rotation side of the motor based on its input)? I tried but could not get it going well.
TurboTronix6 years ago
All worked until I used the code in your stepperMotor.c Nothing happens, the motor does not run anymore...
The Real Elliot (author)  TurboTronix6 years ago
First thing to check is the signal coming out of your AVR going to the H-bridge chip or the motor. Is it giving the right pulses at the right time? If you have some LEDs around, you can hook them up to the AVR pins instead to verify that the code is doing the right thing. If it is, and you're not using an H-bridge, you may not have enough current to step the motor. (Depends on the motor brand and what load you've got attached. Try without load to get it working first.) The solution is to get an H-bridge.