Circuit drawing of Stepper Motor controller

PIC Circuits Gallery

Stepper Motor controller
On these pages, I will introduce a control circuit for stepper motor. The software of this project is adapted to Embedded Systems(Lab13) for 2002 of Cleveland State University.

Pattern drawing of Stepper Motor controller
(Wiring side)

Circuit explanation of Stepper Motor controller
Motor driving circuit
This is the circuit which drives the coil of stepper motor. There are circuits which drive coil, coil, coil and coil respectively. Darlington connection-type transistor is used for the drive of the coil. As for the Darlington connection, 2 stages of transistors are connected inside in series. The "hfe" of this transistor is the multiplication of the "hfe" of each transistor inside. In case of 2SD1209K which was used this time, the hfe is over 4000. Because the ratio of the input electric current and the output current is big, the rising edge and the falling edge of the control signal can be made sharp. The diode to be putting between the collector and the power is for the protection of the transistor. When the transistor becomes OFF from ON, the coil of the motor tries to continue to pass an electric current and generates high voltage. An electric current by this voltage is applied to the diode and the high voltage which applies over the transistor is prevented.

Speed control circuit
This is the circuit which controls the rotational speed of the motor. TR1 becomes ON condition when RB7 becomes H level. In this condition, the electric charge of capacitor C1 flows through the transistor and the voltage of the both edges of the capacitor becomes 0 V almost. When RB7 becomes an L level, the transistor becomes OFF condition. In this condition, the electric current flows through VR1 and R4 into capacitor C1 and the charging to the capacitor begins. The voltage of the both edges of the capacitor becomes high gradually as charging is done. As for the change of this voltage, refer to "Integration circuit". The voltage of the capacitor is detected by RB5. The software of PIC interrupts the control of the motor until it checks RB5 after making RB7 an L level and RB5 becomes H level. When making the value of VR1 small, the charging time of the capacitor is short and the control of the motor becomes quick. The control of the motor becomes slow when making VR1 big. The speed control range can be changed by changing the value of the capacitor.

Start/stop circuit
This is the circuit for the clockwise rotating, the counterclockwise rotating or stopping a motor. The baton switch of the non lock is used. Pull-up resistor is used for the port to become H level when the switch is OFF. The RB port of PIC16F84A has an internal pull up feature. However, because RB5 is used for the voltage detection of the capacitor at the circuit this time, an internal pull up feature isn't used. If using RA port for the voltage detection of the capacitor, the RB internal pull up feature can be used. The circuit this time put an external pull-up resistor in the relation of the pattern.

4-MHz resonator is used because the circuit this time doesn't need high-speed operation.

Power supply circuit
The purpose of this circuit is to keep power supply voltage to PIC to 5V when the power of the stepper motor is more than 5V. Because the operating voltage of the stepper motor to be using this time is about 5V, the power supply voltage is +5V. In this case, the voltage which is applied to PIC becomes less than 5V because of the voltage drop (about 1V) of the regulator. In case of PIC16F84A, the operation is possible even if the power falls to about 3V because the operating voltage range is from 2V to 5.5V. It is enough in the 100-mA type.

Stepper motor ( 42SPM-24DCZA )
This is a 2-phase unipolar PM type stepper motor with 48-poles. Step angle : 7.5 degrees ( 360 degrees/48 ) Drive voltage : 5 to 6V Coil current : 250mA(5V)/2 coils

The control of the stepper motor is done by this PIC. To have explained in the operation principle, coil, coil and coil are controlled by the software. coil,

Data sheet for PIC16F84A

3 terminal regulator ( 78L05 )
This is used to make the stable voltage of +5V. A 100-mA type is used. Data sheet for 78L05

Motor drive transistor ( 2SD1209K )
This is a Darlington connection-type transistor. At first, I used 2SD1590 ( 8A type ) as the transistor for the drive. However, in case of the motor which was used this time, the coil current was about 250 mA. So, I changed into the small type transistor. The maximum collector current of 2SD1209K is 1A and "hfe" is more than 4000. Data sheet for 2SD1209K

Transistor for the motor speed control ( 2SC1815 )
This is the transistor to control the speed of the motor.

Data sheet for 2SC1815

Diode for the transistor protection
This is the diode to protect a transistor from the back electromotive force which occurs with the coil of the stepper motor. It depends on the kind of the stepper motor but it is to be OK if it is possible to pass a hundreds-of-mA electric current.

Baton switch
This is a switch for the clockwise rotating, the counterclockwise rotating or making a motor stop.

IC socket
This is the socket to mount PIC16F84A.

This is a 4 MHz resonator.

Variable resistor for the speed control
B type is used.

It is to be OK at 1/8 W.

Resistor for pull up
This is the series resistor to be using for start/stop switch pull up.

Multilayer ceramic capacitor
This capacitor is used to bypass the high frequency noise of the input and output of the power supply.

Electrolytic capacitor
This capacitor is used as the ripple filter capacitor of the power circuit.

Printed board
This is an universal printed board with 15 x 25 halls.

Wiring terminal
This terminal is used to connect a power supply wire and the variable resistor for the speed control.

Connector for the stepper motor connection
A 6-pin connector is used to connect a stepper motor. The motor used this time, 5-pin connector is OK. Because the power of the X coil and the Y coil is connected inside the motor. However, a power line is divided by the kind of the motor. So, 6-pin are used for two power lines to be able to be connected.

This is used as the leg of the printed board.

Software flow chart of Stepper Motor controller

Source code file of Stepper Motor controller

001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056

;******************************************************** ; ; Stepper Motor controller ; ; Author : Seiichi Inoue ;******************************************************** list p=pic16f84a include __config _hs_osc & _wdt_off & _pwrte_on & _cp_off ;**************** Label Definition ******************** cblock h'0c' mode ;Operation mode ;0=stop 1=right 2=left count1 ;Wait counter count2 ;Wait counter(for 1msec) endc rb0 rb1 rb2 rb5 rb7 equ equ equ equ equ 0 1 2 5 7 ;RB0 ;RB1 ;RB2 ;RB5 ;RB7 of of of of of PORTB PORTB PORTB PORTB PORTB

;**************** Program Start *********************** org 0 ;Reset Vector goto init org 4 ;Interrupt Vector clrf intcon ;Clear Interruption reg ;**************** Initial Process ********************* init bsf status,rp0 ;Change to Bank1 clrf trisa ;Set PORTA all OUT movlw b'00100111' ;RB0,1,2.5=IN RB7=OUT movwf trisb ;Set PORTB movlw b'10000000' ;RBPU=1 Pull up not use movwf option_reg ;Set OPTION_REG bcf status,rp0 ;Change to Bank0 clrf mode ;Set mode = stop clrf count1 ;Clear counter clrf count2 ;Clear counter movlw b'00000101' ;Set PORTA initial value movwf porta ;Write PORTA bsf portb,rb7 ;Set RB7 = 1 btfsc portb,rb5 ;RB5 = 0 ? goto $-1 ;No. Wait start ;************* btfsc goto clrf goto check1 Check switch condition ***************** portb,rb1 ;RB1(stop key) = ON ? check1 ;No. Next mode ;Yes. Set stop mode drive ;No. Jump to motor drive

057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112

btfsc goto movlw movwf goto check2 btfsc goto movlw movwf

portb,rb2 check2 d'1' mode drive portb,rb0 drive d'2' mode

;RB2(right key) = ON ? ;No. Next ;Yes. Set right mode ;Save mode ;No. Jump to motor drive ;RB0(left key) = ON ? ;No. Jump to motor drive ;Yes. Set left mode ;Save mode

;******************** Motor drive ********************* drive movf mode,w ;Read mode bz start ;mode = stop bsf portb,rb7 ;Set RB7 = 1 btfsc portb,rb5 ;RB5 = 0 ? goto $-1 ;No. Wait movlw d'5' ;Set loop count(5msec) movwf count1 ;Save loop count loop call timer ;Wait 1msec decfsz count1,f ;count - 1 = 0 ? goto loop ;No. Continue bcf portb,rb7 ;Set RB7 = 0 btfss portb,rb5 ;RB5 = 1 ? goto $-1 ;No. Wait movf porta,w ;Read PORTA sublw b'000000101' ;Check motor position bnz drive2 ;Unmatch movf mode,w ;Read mode sublw d'1' ;Right ? bz drive1 ;Yes. Right movlw b'00001001' ;No. Set Left data goto drive_end ;Jump to PORTA write drive1 movlw b'00000110' ;Set Right data goto drive_end ;Jump to PORTA write ;------drive2 movf porta,w ;Read PORTA sublw b'000000110' ;Check motor position bnz drive4 ;Unmatch movf mode,w ;Read mode sublw d'1' ;Right ? bz drive3 ;Yes. Right movlw b'00000101' ;No. Set Left data goto drive_end ;Jump to PORTA write drive3 movlw b'00001010' ;Set Right data goto drive_end ;Jump to PORTA write ;------drive4 movf porta,w ;Read PORTA sublw b'000001010' ;Check motor position bnz drive6 ;Unmatch movf mode,w ;Read mode

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

drive5 ;------drive6

sublw bz movlw goto movlw goto movf sublw bnz movf sublw bz movlw goto

d'1' drive5 b'00000110' drive_end b'00001001' drive_end porta,w b'000001001' drive8 mode,w d'1' drive7 b'00001010' drive_end b'00000101' drive_end b'00000101' porta start

;Right ? ;Yes. Right ;No. Set Left data ;Jump to PORTA write ;Set Right data ;Jump to PORTA write ;Read PORTA ;Check motor position ;Unmatch ;Read mode ;Right ? ;Yes. Right ;No. Set Left data ;Jump to PORTA write ;Set Right data ;Jump to PORTA write ;Compulsion setting ;Write PORTA ;Jump to start *****************

drive7 ;------drive8

movlw goto movlw

drive_end movwf goto ;************* timer movlw movwf tmlp nop nop decfsz goto return

1msec Timer Subroutine d'200' count2 count2,f tmlp

;Set loop count ;Save loop count ;Time adjust ;Time adjust ;count - 1 = 0 ? ;No. Continue ;Yes. Count end

;******************************************************** ; END of Stepper Motor controller ;******************************************************** end

Listing file of Stepper Motor controller
MPASM 02.50.02 Intermediate PAGE 1 STEP.ASM 3-9-2001 23:52:45



00001 ;******************************************************** 00002 ; 00003 ; Stepper Motor controller 00004 ; 00005 ; Author : Seiichi Inoue 00006 ;******************************************************** 00007 00008 LIST P=PIC16F84A 00009 INCLUDE P16F84A.INC 00001 LIST 00002 ; P16F84A.INC Standard Header File, Version 2.00'(modify) 00134 LIST 2007 3FF2 00010 __CONFIG _HS_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF 00011 00012 ;**************** Label Definition ******************** 00013 CBLOCK H'0c' 0000000C 00014 MODE ;Operation mode 00015 ;0=stop 1=right 2=left 0000000D 00016 COUNT1 ;Wait counter 0000000E 00017 COUNT2 ;Wait counter(for 1msec) 00018 ENDC 00019 00000000 00020 RB0 EQU 0 ;RB0 of PORTB 00000001 00021 RB1 EQU 1 ;RB1 of PORTB 00000002 00022 RB2 EQU 2 ;RB2 of PORTB 00000005 00023 RB5 EQU 5 ;RB5 of PORTB 00000007 00024 RB7 EQU 7 ;RB7 of PORTB 00025 00026 ;**************** Program Start *********************** 0000 00027 ORG 0 ;Reset Vector 0000 2805 00028 GOTO INIT 0004 00029 ORG 4 ;Interrupt Vector 0004 018B 00030 CLRF INTCON ;Clear

Interruption reg 00031 00032 ;**************** ********************* 0005 0005 1683 Bank1 0006 0185 all OUT 0007 3027 ;RB0,1,2.5=IN RB7=OUT 0008 0086 0009 3080 up not use 000A 0081 OPTION_REG 000B 1283 Bank0 000C 018C stop 000D 018D counter 000E 018E counter 000F 3005 initial value 0010 0085 0011 1786 0012 1A86 0013 2812 00033 INIT 00034 00035 00036 00037 00038 00039 00040 00041 00042 00043 00044 BSF CLRF MOVLW MOVWF MOVLW MOVWF BCF CLRF CLRF CLRF MOVLW Initial Process ;Change to ;Set PORTA


;Set PORTB ;RBPU=1 Pull ;Set ;Change to ;Set mode = ;Clear ;Clear ;Set PORTA ;Write PORTA ;Set RB7 = 1 ;RB5 = 0 ? ;No. Wait

00045 MOVWF 00046 BSF 00047 BTFSC 00048 GOTO 00049 0014 00050 START MPASM 02.50.02 Intermediate STEP.ASM PAGE 2 LOC OBJECT CODE VALUE ***************** 0014 1886 key) = ON ? 0015 2818 0016 018C stop mode 0017 2821 motor drive 0018 0018 1906 key) = ON ? 0019 281D 001A 3001 right mode 001B 008C 001C 2821 LINE SOURCE TEXT 00051 ;************* 00052 00053 00054 00055 00056 CHECK1 00057 00058 00059 00060 00061 BTFSC GOTO CLRF GOTO BTFSC GOTO MOVLW MOVWF GOTO


Check switch condition PORTB,RB1 CHECK1 MODE DRIVE PORTB,RB2 CHECK2 D'1' MODE DRIVE ;RB1(stop ;No. Next ;Yes. Set ;No. Jump to ;RB2(right ;No. Next ;Yes. Set ;Save mode ;No. Jump to

motor drive 001D 001D 1806 key) = ON ? 001E 2821 motor drive 001F 3002 left mode 0020 008C ********************* 0021 0021 080C 0022 1903 2814 0024 1786 0025 1A86 0026 2825 0027 3005 count(5msec) 0028 008D count 0029 2062 002A 0B8D 0 ? 002B 2829 Continue 002C 1386 002D 1E86 002E 282D 002F 0805 0030 3C05 position 0031 1D03 283B 0033 080C 0034 3C01 0035 1903 2839 0037 3009 Left data 0038 2860 PORTA write 0039 0039 3006 data 003A 2860 PORTA write 003B 003B 0805 003C 3C06 position 003D 1D03 2847 003F 080C 0040 3C01 0041 1903 2845 0043 3005

00062 CHECK2 00063 00064 00065



;RB0(left ;No. Jump to ;Yes. Set ;Save mode

00066 MOVWF MODE 00067 00068 ;******************** 00069 DRIVE 00070 00071 00072 00073 00074 00075 00076 00077 LOOP 00078 00079 00080 00081 00082 00083 00084 00085 00086 00087 00088 00089 00090 00091 DRIVE1 00092 00093 MOVF BZ BSF BTFSC GOTO MOVLW MOVWF CALL DECFSZ GOTO BCF BTFSS GOTO MOVF SUBLW BNZ MOVF SUBLW BZ MOVLW GOTO MOVLW GOTO

Motor drive ;Read mode ;mode = stop ;Set RB7 = 1 ;RB5 = 0 ? ;No. Wait ;Set loop ;Save loop ;Wait 1msec ;count - 1 = ;No. ;Set RB7 = 0 ;RB5 = 1 ? ;No. Wait ;Read PORTA ;Check motor ;Unmatch ;Read mode ;Right ? ;Yes. Right ;No. Set ;Jump to ;Set Right ;Jump to


00094 ;------00095 DRIVE2 00096 MOVF 00097 SUBLW 00098 00099 00100 00101 00102 BNZ MOVF SUBLW BZ MOVLW

PORTA,W B'000000110' DRIVE4 MODE,W D'1' DRIVE3 B'00000101'

;Read PORTA ;Check motor ;Unmatch ;Read mode ;Right ? ;Yes. Right ;No. Set

Left data 0044 2860 00103 PORTA write MPASM 02.50.02 Intermediate PAGE 3 LOC OBJECT CODE VALUE 0045 0045 300A data 0046 2860 PORTA write 0047 0047 0805 0048 3C0A position 0049 1D03 2853 004B 080C 004C 3C01 004D 1903 2851 004F 3006 Left data 0050 2860 PORTA write 0051 0051 3009 data 0052 2860 PORTA write 0053 0053 0805 0054 3C09 position 0055 1D03 285F 0057 080C 0058 3C01 0059 1903 285D 005B 300A Left data 005C 2860 PORTA write 005D 005D 3005 data 005E 2860 PORTA write 005F 005F 3005 setting


DRIVE_END 3-9-2001

;Jump to 23:52:45

LINE SOURCE TEXT 00104 DRIVE3 00105 00106


B'00001010' DRIVE_END

;Set Right ;Jump to

00107 ;------00108 DRIVE4 00109 MOVF 00110 SUBLW 00111 00112 00113 00114 00115 00116 00117 DRIVE5 00118 00119 BNZ MOVF SUBLW BZ MOVLW GOTO MOVLW GOTO

PORTA,W B'000001010' DRIVE6 MODE,W D'1' DRIVE5 B'00000110' DRIVE_END B'00001001' DRIVE_END

;Read PORTA ;Check motor ;Unmatch ;Read mode ;Right ? ;Yes. Right ;No. Set ;Jump to ;Set Right ;Jump to

00120 ;------00121 DRIVE6 00122 MOVF 00123 SUBLW 00124 00125 00126 00127 00128 00129 00130 DRIVE7 00131 00132 BNZ MOVF SUBLW BZ MOVLW GOTO MOVLW GOTO

PORTA,W B'000001001' DRIVE8 MODE,W D'1' DRIVE7 B'00001010' DRIVE_END B'00000101' DRIVE_END

;Read PORTA ;Check motor ;Unmatch ;Read mode ;Right ? ;Yes. Right ;No. Set ;Jump to ;Set Right ;Jump to

00133 ;------00134 DRIVE8 00135 MOVLW 00136



0060 0060 0061 start

0085 2814

00137 DRIVE_END 00138 MOVWF 00139 GOTO 00140 00141 ;************* 00142 TIMER 00143 00144 00145 TMLP 00146 00147 00148 00149 MOVLW MOVWF NOP NOP DECFSZ GOTO RETURN


;Write PORTA ;Jump to

***************** 0062 0062 30C8 count 0063 008E count 0064 0000 0065 0000 0066 0B8E 0 ? 0067 2864 Continue 0068 0008 end

1msec Timer Subroutine D'200' COUNT2 ;Set loop ;Save loop ;Time adjust ;Time adjust ;count - 1 = ;No. ;Yes. Count


00150 00151 ;******************************************************** 00152 ; END of Stepper Motor controller 00153 ;******************************************************** 00154 00155 END MPASM 02.50.02 Intermediate STEP.ASM 3-9-2001 23:52:45 PAGE 4 Label list has been deleted. MEMORY USAGE MAP ('X' = Used, '-' = Unused)

0000 : X---XXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX 0040 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXX---------------------2000 : -------X-------- ---------------- ------------------------------All other memory blocks unused. Program Memory Words Used: Program Memory Words Free: Errors : Warnings : Messages : 0 0 reported, 0 reported, 102 922

0 suppressed 0 suppressed

Processing explanation of Stepper Motor controller
Label definition
;**************** Label Definition ******************** cblock h'0c' The data area is automatically assigned from 0ch by CBLOCK directive. ENDC is used for the ending of assignment.

The purpose of each data area is shown below. Label mode : Purpose This is the area which manages the condition of the motor control. 0=Stop, 1=Clockwise, 2=Counterclockwise This is the count area to make control waiting time. It counts 1 millisecond five times and 5 milliseconds are made.

count1 :

count2 : This is the counter to make 1 millisecond.

Program start
;**************** Program Start ***********************

Instruction is executed from Zero addresses of the program memory when making the power ON of the PIC. When there is interruption processing, processing is begun from the addresse 4. Because it isn't using interruption this time, there is not program execution from the addresse 4. It makes the interruption prohibition condition if the interruption occurs. It isn't necessary to do this processing.

Initialization process
;**************** Initial Process ********************* The following processing is done as the processing of being initialized after the turning on.

The initialization of the mode of port A All ports are set to output mode. The initialization of the mode of port B RB0,1,2 and 5 are set to input mode. And RB7 is set to output mode. Port B pull-ups are disabled (RPBU=1) Because RB5 is used as the high impedance input at the circuit this time, the RB pull up feature should not be used. Setting of a stop mode Immediately after turned on, it sets a motor to the stop mode. When there is not this step, the original value of mode becomes 0. It is put for the safety. Counters for the control waiting time are initialized There is not a problem even if there is not these processing. They are put for the safety. Port A initialization It sets 0101 as the initial state of port A. Because it drives with the transistor, the logic reverses. It is in the condition, =H =L =H =L, from the bit on the left. Discharging of the capacitor for the speed control It makes RB7 H level and it makes TR1 ON and discharging in the electric charge of the capacitor for the speed control. The end of the discharge is confirmed by RB5.

Switch condition confirmation process
;************* Check switch condition ***************** It detects the ON condition of the stop switch, the RRC switch, the RLC switch. A condition is set to mode according to the kind of the switch which was made ON. The order of the detection is a stop, a RRC, a RLC. When more than one switch is pushed at the same time, the switch which detected ON earlier is effective. This processing is done every time it controls 1 step of motor.

;******************** Motor drive ********************* A stop mode is checked first. In case of the stop mode, it doesn't drive the motor and it jumps to the switch condition confirmation process. In case of not being a stop mode, the following process is done.

Motor drive process

Discharging of the capacitor for the speed control Discharging the capacitor as the preparation to make the timing of the speed control. The wait processing of 5 milliseconds In the high-speed control, the rotor doesn't follow the change of the magnetic pole and the step motor doesn't rotate normally. It sets a timer value to turn a full speed normally. In case of the motor which was used this time, it doesn't rotate normally when making less than 5 milliseconds. The charging of a capacitor for the speed control and the confirmation process It makes RB7 an L level and it begins charging the capacitor. It confirms that the charging completes by RB5. It is completion if RB5 becomes H level. Correctly, it is not charging completion and it is the fact that the voltage of the capacitor became above the threshold voltage of RB5. The motor drive process After the speed control timing, a motor is driven. The control state of the motor is confirmed first. This is done by reading the condition of port A. Next, whether it is a clockwise mode or a counterclockwise mode is judged. The following control state which should drive a motor by the result is set to port A. Because there are four conditions, processing is done in each condition. After the motor drive process, it jumps again to the switch condition confirmation process.

Operation confirmation of Stepper Motor

The stationary torque of the stepper motor is large. However, as the turn becomes fast, the turn torque falls. The stepper motor can not do a high-speed turn. A stepper motor is made to control a turn position correctly. It is to control a turn position correctly like the drive motor of the printer and so on. The circuit this time controlled the number of rotations of the motor by the charging of the capacitor but can control a turn angle in the drive number of times (the step) if remodeling software. By making this controller, I found the following. The turn torque falls extremely when the revolution rises. It shivered and the motor didn't rotate when making the high-speed side when adjusting control timing. When grasping an axis of rotation with the hand when turning at high speed, the turn has stopped immediately. The torque in case of stop is large. I don't find how much power it is because I don't have a measurement receptacle. However, it doesn't move when not applying considerable power when the motor stops. A measurement result with number of rotations is shown below.

The most low speed : 27 rpm The full speed : 128 rpm
The most low speed can make the turn which was more slowly if it makes the value of the capacitor for the speed control big. Because the full speed is the speed that the turn stops when applying a few loads, it isn't practical. Because the mass of rotor of the motor is related, this is the speed limitation of this motor.