Controlling a uni-polar stepping motor


The project This simple little project requires only a small adaptor to make it more convenient to connect the stepping motor to the interface board. Obviously the motor wires could be connected directly to the interface. But it is easy to mix up six dangling wires and only one pair of wires swapped will lead to unpredictable behaviour - or indeed no behaviour at all!

The circuit diagram and pcb layout is below. C1 and C2 or the commons of the two motor coils and are connected to the positive supply. The four driving coils are connected to GPIO lines 24, 23, 18 and 15 of my 5in 16 out interface. If you use a different interface, simply change the relevant assignments in the program.

Circuit diagram and PCB layout


My interface uses ULN28903 chips. They can sink a maximum of 500 mA, which is not a lot for a motor. If the chip gets very warm whilst driving the motor, reduce the voltage of stop!

The wires of the motor must obviously be connected in the right sequence. A simple way to do this is to test them with a 9V battery or something similar. Connect the two commons together. These are usually fitted in the centre of the coil, but they can be identified with an ohmmeter. The centre tap has the same resistance against both halves of the coil, as shown below.

Testing the coils To determine the correct sequence of wires simply connect the two commons to one side of the battery and any one of the other four wires to the other side. You have a three in four chance that the motor will move by a single step either left or right. If this doesn't happen, try another wire.


Repeat the process with a second wire. If the motor moves again in the same direction, the sequence is correct. If it turns in the other direction, you got it wrong and you have to start again from the beginning. Generally a few tries will establish the correct sequence. Once established, I tend to fasten the wires in the correct order to a small connector, as seen in the photograph above.

A word about timing. To change the speed of the motor, a time wasting loop is needed. For various reason I picked a simple FOR -NEXT loop that does nothing. Depending on the motor in use, the fastest speed my be too much for your motor or, alternatively, your motor might be able to turn even faster. One of the beauty of BBC BASIC programs is that the relevant section can be change quickly and easily

 

The programsThe application

The archive provides two programs. A multi tasking application and a straight forward BASIC program.

The window of the application is on the right. It was originally written to demonstrate the basic principle of a stepping motor to my students. ON OFF buttons are top right. The arrows left of them  change the speed. Below that is the direction control. The single step buttons only work when the motor is OFF and will advance the rotor by one single step only. 

Below that is the selection for half step and full step. At the very bottom is a feature that allows you to turn the rotor a predetermined number of turns. This will only work if the motor is off to begin with and if you select the correct step angle for your motor. It can also of course be used to find that angle in the first place.

All this will of course only work if the coils of your motor (in the correct order) are connected to the GPIO pins 24, 23,18 and 15 in the first place.


Here is the link to the archive

Stepper archive


Below is a simple BASIC program which should be much easier to understand than the application above.

 
REM Bi-polar stepper driver
REM For the Raspberry Pi computerle
REM Jochen Lueg
REM http://roevalley.com
REM Limavady, November 2012
REM Version 1.0

ON ERROR PRINT ERL;"  ";REPORT$:END
PROCinit
OSCLI"RMensure GPIO 0.40 ERROR  Please inbstall the GPIO module"
PROCsetupGPIO
OFF

PRINT
PRINT " Motor controls"
PRINT
PRINT " Left       . . . . . .  Z"
PRINT " Right      . . . . . .  C"
PRINT " Stop       . . . . . .  X"
PRINT " Full step  . . . . . .  F"
PRINT " Half step  . . . . . .  H"
PRINT
PRINT " Fastest    . . . . . .  1"
PRINT " to"
PRINT " Slowest    . . . . . .  9"
PRINT
PRINT " Press 'Q' to leave the program"

REPEAT
  Key$=INKEY$(0) : REPEAT UNTIL INKEY(0)=-1
  IF Key$="c" OR Key$="C" Direction$="Right"
  IF Key$="z" OR Key$="Z" Direction$="Left"
  IF Key$="q" OR Key$="Q" Direction$="Finished"
  IF Key$="x" OR Key$="X" Direction$="Stop"
  IF Key$="h" OR Key$="H" Mode$="Half"
  IF Key$="f" OR Key$="F" Mode$="Full"
  IF Key$="1" T%=150*S%    :Speed$="Fast"
  IF Key$="2" T%=200*S%    :Speed$="Fast-1"
  IF Key$="3" T%=300*S%   :Speed$="Fast-2"
  IF Key$="4" T%=625*S%   :Speed$="Fast-3"
  IF Key$="5" T%=1250*S%   :Speed$="Half"
  IF Key$="6" T%=2500*S%  :Speed$="Slow+3"
  IF Key$="7" T%=5000*S%  :Speed$="Slow+2"
  IF Key$="8" T%=10000*S%  :Speed$="Slow+1"
  IF Key$="9" T%=20000*S%  :Speed$="Slow"
  IF Direction$="Right" AND Mode$="Full" THEN PROCfull_step_right
  IF Direction$="Right" AND Mode$="Half" THEN PROChalf_step_right
  IF Direction$="Left" AND Mode$="Full"  THEN PROCfull_step_left
  IF Direction$="Left" AND Mode$="Half"  THEN PROChalf_step_left
  IF Direction$="Stop" PRINTTAB(1,18)"Motor stopped                                 "
  IF Direction$="Stop" PROCall_off:Direction$="Wait"
  IF Direction$ <> "Wait" PRINTTAB(1,18);Mode$;" step. Turning ";Direction$;" with speed ";Speed$;"   "
UNTIL Direction$="Finished"
END


DEFPROCfull_step_right
LOCAL J%,
FOR J%=0 TO 3
  SYS"GPIO_WriteData",Full%(J%),1
  PROCall_off
NEXT
ENDPROC


DEFPROCfull_step_left
LOCAL J%,
FOR J%=3 TO 0 STEP -1
SYS"GPIO_WriteData",Full%(J%),1
  PROCall_off
NEXT
ENDPROC


DEFPROCall_off
LOCAL J%,Speed%
FOR Speed%=1 TO T%:NEXT
  FOR J%=0 TO 3
  SYS"GPIO_WriteData",Full%(J%),0
  NEXT
ENDPROC


DEFPROChalf_step_right
LOCAL J%
FOR J%=0 TO 7
  SYS"GPIO_WriteData",Half%(J%),1
  SYS"GPIO_WriteData",Half%(J%+1),1
  PROCall_off
NEXT
ENDPROC


DEFPROChalf_step_left
LOCAL J%,Speed%
FOR J%=8 TO 1 STEP -1
SYS"GPIO_WriteData",Half%(J%),1
SYS"GPIO_WriteData",Half%(J%-1),1
  PROCall_off
NEXT
ENDPROC


DEFPROCsetupGPIO
SYS"GPIO_EnableI2C",0
SYS"GPIO_ExpAsGPIO",2
ENDPROC


DEFPROCinit
W%=0
Direction$="Stop"
S%=800
T%=S%*150
Speed$="Fast"
Mode$="Full"
DIM Full%(3) : Full%()=24,23,18,15
DIM Half%(8) : Half%()=24,24,23,23,18,18,15,15,24
ENDPROC


DEFPROCerror

PRINT REPORT$;" at line ";ERL  :
END
ENDPROC

  
 


Return to interfacing index

Back to the start
Tudor with sign