Computer Control of a Stepper Motor

[Direct connection to CPU] There are several methods of controlling a stepper motor from a computer. The simplest method is to use a direct connection to the parallel port, as shown in the schematic to the right.

Each of the four lines from the parallel port (pins 2, 3, 4 and 5) are used to switch an NPN power transistor that controls a coil in the motor. The transistors are protected from excessive current flow by the 1k resistors, and they are protected from the coils' backlash current by the shorting diodes.

All the parts needed to build this simple circuit are available at Radio Shack, but be aware that Radio Shack's prices for components are literally quadruple that of other electronics stores.

Part Type Qty. R.S. #
NPN pwr. transistor10W 4 4 x 276-2017
Diode 1A 4 2 x 276-1102
Resistor 1k 4 271-1321
Male connector DB-251 276-1547

[Animation of stepping sequence]The following BASIC program demonstrates how to drive the motor from the computer. It will run a stepper motor at any speed in either direction.

   10 REM Stepper Motor Controller
   20 PORT = &H3BC
   30 STATE = STATE + DIRECTION
   40 IF STATE > 4 THEN STATE = 1
   50 IF STATE < 1 THEN STATE = 4
   60 PRINT STATE,
   70 IF STATE = 1 THEN OUT PORT, 3:  REM 1100
   80 IF STATE = 2 THEN OUT PORT, 6:  REM 0110
   90 IF STATE = 3 THEN OUT PORT, 12: REM 0011
  100 IF STATE = 4 THEN OUT PORT, 9:  REM 1001
  110 FOR W = 1 TO DELAY: NEXT W
  120 X$ = INKEY$
  130 IF X$ = "<" THEN DIRECTION = -1:    REM Backwards
  140 IF X$ = ">" THEN DIRECTION = 1:     REM Forwards
  150 IF X$ = "?" THEN DIRECTION = 0:     REM Stop-locked
  160 IF X$ = "-" THEN DELAY = DELAY + 100: REM Slow down
  170 IF X$ = "+" THEN DELAY = DELAY - 100: REM Speed up
  180 IF X$ <> CHR$(27) THEN GOTO 30
  190 OUT PORT, 0: REM 0000 (Stop-free)

Up to three stepper motors may be attached to one parallel port in this manner. The second motor would be connected to the other half of the data register (pins 6, 7, 8 and 9). The third motor would use the parallel port's control register (pins 1, 14, 16 and 17).

-------------------------------------
Last modified: 18 July 1999