How Stepper Motors Work
Stepper motors consist of a permanent magnet rotating shaft, called the rotor, and electromagnets on the stationary portion that surrounds the motor, called the stator. Figure 1 illustrates one complete rotation of a stepper motor. At position 1, we can see that the rotor is beginning at the upper electromagnet, which is currently active (has voltage applied to it). To move the rotor clockwise (CW), the upper electromagnet is deactivated and the right electromagnet is activated, causing the rotor to move 90 degrees CW, aligning itself with the active magnet. This process is repeated in the same manner at the south and west electromagnets until we once again reach the starting position.You may double the resolution of some motors by a process known as "half-stepping". Instead of switching the next electromagnet in the rotation on one at a time, with half stepping you turn on both electromagnets, causing an equal attraction between, thereby doubling the resolution. As you can see in Figure 2, in the first position only the upper electromagnet is active, and the rotor is drawn completely to it. In position 2, both the top and right electromagnets are active, causing the rotor to position itself between the two active poles. Finally, in position 3, the top magnet is deactivated and the rotor is drawn all the way right. This process can then be repeated for the entire rotation.
Unlike our example motors which rotated 90 degrees per step, real-world motors employ a series of mini-poles on the stator and rotor to increase resolution. Although this may seem to add more complexity to the process of driving the motors, the operation is identical to the simple 90 degree motor we used in our example. An example of a multipole motor can be seen in Figure 3. In position 1, the north pole of the rotor's perminant magnet is aligned with the south pole of the stator's electromagnet. Note that multiple positions are alligned at once. In position 2, the upper electromagnet is deactivated and the next one to its immediate left is activated, causing the rotor to rotate a precise amount of degrees. In this example, after eight steps the sequence repeats.
First Stepper Circuit
Figure 4 is the schematic of our first test circuit. The PIC's output lines are first buffered by a 4050 hex buffer chip, and are then connected to an NPN transistor. The transistor used, TIP120, is actually a NPN Darlington (it is shown as a standard NPN). The TIP120's act like switches, activating one stepper motor coil at a time.The simplest way to operate a stepper motor with a PIC is with the full step pattern shown in Table 1. Each part of the sequence turns on only one transistor at a time, one after the other. After the sequence is completed, it repeats infinitly until power is removed.
Listing 1
' First stepper motor controller program
' Rotates motor at set speed forever
Symbol delay = B0
delay = 25
loop:
high 0
pause delay
low 0
high 1
pause delay
low 1
high 2
pause delay
low 2
high 3
pause delay
low 3
goto loop
delay = 25
loop:
high 0
pause delay
low 0
high 1
pause delay
low 1
high 2
pause delay
low 2
high 3
pause delay
low 3
goto loop
' use b0 as the delay variable
' set the delay to 25 ms
' turn on Q1
' wait 25 ms
' turn off Q1
' turn on Q2
' wait 25 ms
' turn off Q2
' turn on Q3
' wait 25 ms
' turn off Q3
' turn on Q4
' wait 25 ms
' turn off Q4
' forever
' set the delay to 25 ms
' turn on Q1
' wait 25 ms
' turn off Q1
' turn on Q2
' wait 25 ms
' turn off Q2
' turn on Q3
' wait 25 ms
' turn off Q3
' turn on Q4
' wait 25 ms
' turn off Q4
' forever
Listing 1
Second Basic Program
The first program was fine for demonstrating how to control a stepper motor with a PIC Micro, but was severely limited to the one mode of operation preprogrammed into it. In this next program, we will make the program controllable by external user input in the form of four switches: SW1 will incrementaly increase the delay variable, thereby slowing the rotation of the motor. SW2 does the opposite. SW3 halts all operation while the switch is pressed. SW4 reverses direction of the motor (CW to CCW or vice-versa) while it is closed (pressed).listing 2
' Second stepper motor controller program
' Responds to user input by changing speed or direction
Symbol delay = B0
delay = 100
forward:
high 0
pause delay
low 0
high 1
pause delay
low 1
high 2
pause delay
low 2
high 3
pause delay
low 3
goto check
reverse:
high 3
pause delay
low 3
high 2
pause delay
low 2
high 1
pause delay
low 1
high 0
pause delay
low 0
goto check
check:
if pin4 = 0 then timeup
if pin5 = 0 then timedn
if pin6 = 0 then halt
if pin7 = 0 then reverse
goto forward
timeup:
delay = delay + 5
pause 50
if delay > 250 then max
if pin4 = 0 then timeup
goto check
timedn:
delay = delay - 5
pause 50
if delay <20 then min
if pin5 = 0 then timedn
goto check
halt:
if pin6 = 0 then halt
goto check
max:
delay = 245
goto check
min:
delay = 25
goto check
delay = 100
forward:
high 0
pause delay
low 0
high 1
pause delay
low 1
high 2
pause delay
low 2
high 3
pause delay
low 3
goto check
reverse:
high 3
pause delay
low 3
high 2
pause delay
low 2
high 1
pause delay
low 1
high 0
pause delay
low 0
goto check
check:
if pin4 = 0 then timeup
if pin5 = 0 then timedn
if pin6 = 0 then halt
if pin7 = 0 then reverse
goto forward
timeup:
delay = delay + 5
pause 50
if delay > 250 then max
if pin4 = 0 then timeup
goto check
timedn:
delay = delay - 5
pause 50
if delay <20 then min
if pin5 = 0 then timedn
goto check
halt:
if pin6 = 0 then halt
goto check
max:
delay = 245
goto check
min:
delay = 25
goto check
' use b0 as the delay variable
' delay will begin at 100 ms
' turn on Q1
' wait for delay ms
' turn off Q1
' turn on Q2
' wait for delay ms
' turn off Q2
' turn on Q3
' wait for delay ms
' turn off Q3
' turn on Q4
' wait for delay ms
' turn off Q4
' check the status of switches
' turn on Q4
' wait for delay ms
' turn off Q4
' turn on Q3
' wait for delay ms
' turn off Q3
' turn on Q2
' wait for delay ms
' turn off Q2
' turn on Q1
' wait for delay ms
' turn off Q1
' check the status of switches
' if nothing pressed, continue
' increase delay by 5 ms
' pause for 50 ms
' check switches again
' decrease delay by 5 ms
' pause for 50 ms
' check switches again
'check switches again
' cap the delay at 245 ms
' check switches again
' limit the delay to 25 ms
' go back and check switches
' delay will begin at 100 ms
' turn on Q1
' wait for delay ms
' turn off Q1
' turn on Q2
' wait for delay ms
' turn off Q2
' turn on Q3
' wait for delay ms
' turn off Q3
' turn on Q4
' wait for delay ms
' turn off Q4
' check the status of switches
' turn on Q4
' wait for delay ms
' turn off Q4
' turn on Q3
' wait for delay ms
' turn off Q3
' turn on Q2
' wait for delay ms
' turn off Q2
' turn on Q1
' wait for delay ms
' turn off Q1
' check the status of switches
' if nothing pressed, continue
' increase delay by 5 ms
' pause for 50 ms
' check switches again
' decrease delay by 5 ms
' pause for 50 ms
' check switches again
'check switches again
' cap the delay at 245 ms
' check switches again
' limit the delay to 25 ms
' go back and check switches
End Listing 2
As you can see by looking at the schematic (Figure 7), the "switches" used in this circuit are pieces of jumper wire on a breadboard. The inputs (B0-B3) are kept high via Vcc through a 10K resistor. Theses switches are activated by grounding them via another piece of jumper wire.
Half Stepping
As previously stated, half-stepping doubles the resolution of the motor. In our case, we are using a motor with a 5 degree / per step resolution, requiring 72 steps to complete one rotation. By half stepping, we could double this to 2.5 degrees / pulse, requiring 144 steps to complete one rotation. Table 2 shows the step pattern.Speed Control via Potentiometers
The PIC Basic language has a command that allows you to read the value of potentiometers on a given scale. This is the "pot" command, and is used in thefollowing manner:
pot pin,scale,var
Where pin is the pin the potentiometer is connected to (pins B0-7), scale is the maximum value the command will return (in this case 245, being the maximum amount of ms we want in between pulses), and var is the variable the value will be placed in.
Using this command, we can determine the value of a potentiometer (we will be using a 50K ohm pot) and then use it to control the pulsewidth outputted to the UCN 5804. A schematic is shown in Figure 10.
Listing 4
' Controlling a stepper motor with the UCN 5804 and a potentiometer
Symbol delay = B0
low 1
start:
pot 2,245,delay
if delay < 25 min
goto turn
turn:
delay = delay * 1000
pulsout 0,delay
goto start
min:
delay = 25
goto turn
low 1
start:
pot 2,245,delay
if delay < 25 min
goto turn
turn:
delay = delay * 1000
pulsout 0,delay
goto start
min:
delay = 25
goto turn
' make a delay variable
' set the output enable low
' read pot at pin B2
' set min delay to 25
' if delay is over 25, start moving
' turn delay into microseconds
' send to UCN 5804
' go back to start
' bottom out the delay at 25
' start the motor
' set the output enable low
' read pot at pin B2
' set min delay to 25
' if delay is over 25, start moving
' turn delay into microseconds
' send to UCN 5804
' go back to start
' bottom out the delay at 25
' start the motor
End Listing 4
Not quite as complicated as you may have expected (thanks to the use of the UCN 5804). Notice again, the minimum delay set at 25 ms. If you would like to make it a lower number (thereby making the motor turn faster), simply change all occurances of "25" to the number desired.
Controlling Stepper Motors with a PIC Microcontroller
If your motor does not move at all when the program is active, then the most likely problem is that the diodes are facing the wrong direction. Check the schematic for proper wiring.If the motor is moving, but does so only sporadicaly or jiggles back and forth, then there can be a number of possible causes:
1. There is not enough power going to the motor. This is often the case when using batteries which cannot support the drain of the motor. It is reccomended that you use a wall transformer to power the motor in combination with a 7805 voltage regulator to power the circuitry.
2. You are not using TIP 120 transistors, or a variation that is not able to support the load of the stepper motor.
3. The stepper motor is not wired to the correct transistor. Check the coil resistance with an ohm meter and rewire properly. For motors purchased from us, the pinouts are available here.
4. The pulse frequency is higher than that which the motor can react to, causing a malfunction. The programs in this article use the delay variable to control pulse frequency. Try increasing the value to decrease pulse frequency
For more details visit: http://www.imagesco.com/articles/picstepper/02.html