Stepper Motor Interfacing With Microcontroller Tutorial
Stepper Motor Interfacing With Microcontroller Tutorial
Page (/wiki/Stepper_Motor_Tutorial)
Discussion (/mediawiki/index.php?title=Talk:Stepper_Motor_Tutorial&action=edit&redlink=1)
Edit
Nav
Stepper motors can be used in various areas of your microcontroller projects such as making robots,
robotic arm, automatic door lock system etc. This tutorial will explain you construction of stepper motors
(unipolar and bipolar stepper motors ), basic pricipal, di erent controlling types (Half step and Full step),
Interfacing Techniques (using L293D or ULN2003) and programming your microcontroller in C and
assembly to control stepper motor.
Table of Content
Step Sequence
Stepper motors can be driven in two di erent patterns or sequences. namely, Full Step Sequence
Bulleted list item
Half Step Sequence
A\
B\
The working of the full mode sequence is given in the animated gure below.
A\
B\
The working of the half mode sequence is given in the animated gure below.
Step Angle
Step angle of the stepper motor is de ned as the angle traversed by the motor in one step. To calculate
step angle,simply divide 360 by number of steps a motor takes to complete one revolution. As we have
seen that in half mode, the number of steps taken by the motor to complete one revolution gets doubled,
so step angle reduces to half.
As in above examples, Stepper Motor rotating in full mode takes 4 steps to complete a revolution, So step
angle can be calculated as...
Step Angle = 360 / 4 = 90
and in case of half mode step angle gets half so 45.
So this way we can calculate step angle for any stepper motor. Usually step angle is given in the spec sheet
of the stepper motor you are using. Knowing stepper motor's step angle helps you calibrate the rotation
of motor also to helps you move the motor to correct angular position.
A\
B\
+ve
-ve
-ve
-ve
-ve
-ve
+ve
-ve
-ve
+ve
-ve
-ve
-ve
-ve
-ve
+ve
The above polarity sequence can be interpreted in terms of logic levels for microcontroller by activating
one coil at a time as shown in the table below.
Step Sequence
Step
A\
B\
1. #include<REG2051.H>
2. #definestepperP1
3.
4. voiddelay()
5. {
6.
unsignedchari,j,k;
7.
for(i=0;i<6;i++){
8.
for(j=0;j<255;j++)
8.
for(j=0;j<255;j++)
9.
10.
for(k=0;k<255;k++);
11. }
12.
13. voidmain()
14. {
15.
while(1){
16.
stepper=0x0C;
17.
delay();
18.
stepper=0x06;
19.
delay();
20.
stepper=0x03;
21.
delay();
22.
stepper=0x09;
23.
delay();
24.
25. }
Assembly Programming
1.
org0H
2.
3.
stepperequP1
4.
5. main:
6.
movstepper,#0CH
7.
acalldelay
8.
movstepper,#06H
9.
acalldelay
9.
acalldelay
10.
movstepper,#03H
11.
acalldelay
12.
movstepper,#09H
13.
acalldelay
14.
sjmpmain
15.
16. delay:
17.
movr7,#4
18. wait2:
19.
movr6,#0FFH
20. wait1:
21.
movr5,#0FFH
22. wait:
23.
djnzr5,wait
24.
djnzr6,wait1
25.
djnzr7,wait2
26.
ret
27.
end
The working of the above code can be seen in the demo animation below.
while(1){
4.
stepper=0x08;
5.
delay();
5.
delay();
6.
stepper=0x0C;
7.
delay();
8.
stepper=0x04;
9.
delay();
10.
stepper=0x06;
11.
delay();
12.
stepper=0x02;
13.
delay();
14.
stepper=0x03;
15.
delay();
16.
stepper=0x01;
17.
delay();
18.
stepper=0x09;
19.
delay();
20.
21. }
Assembly Programming
Here also the main routine changes rest everything remains same.
1. main:
2.
movstepper,#08H
3.
acalldelay
4.
movstepper,#0CH
5.
acalldelay
6.
movstepper,#04H
7.
acalldelay
8.
movstepper,#06H
9.
acalldelay
10.
movstepper,#02H
11.
acalldelay
11.
acalldelay
12.
movstepper,#03H
13.
acalldelay
14.
movstepper,#01H
15.
acalldelay
16.
movstepper,#09H
17.
acalldelay
18.
sjmpmain
The working of the above code can be seen in the demo animation below.
1. voidmain()
2. {
3.
while(1){
4.
stepper=0x03;
5.
delay();
6.
stepper=0x01;
7.
delay();
8.
stepper=0x00;
9.
delay();
10.
stepper=0x02;
11.
delay();
12.
12.
13. }
Assembly Programming
1. main:
2.
movstepper,#03H
3.
acalldelay
4.
movstepper,#01H
5.
acalldelay
6.
movstepper,#00H
7.
acalldelay
8.
movstepper,#02H
9.
acalldelay
10.
sjmpmain
The working of the above code can be seen in the demo animation below.
while(1){
4.
stepper=0x08;
5.
delay();
6.
stepper=0x02;
7.
delay();
8.
stepper=0x04;
9.
delay();
10.
stepper=0x01;
11.
delay();
12.
13. }
Assembly Programming
1. main:
2.
movstepper,#08H
3.
acalldelay
4.
movstepper,#02H
5.
acalldelay
6.
movstepper,#04H
7.
acalldelay
8.
movstepper,#01H
9.
acalldelay
10.
sjmpmain