Connecting Stepper Motors to Microcontrollers Tutorial

Microcontroller Topics
User avatar
Reginald
Sergeant Major
Sergeant Major
Posts: 29
Joined: Mon Aug 03, 2009 9:54 pm
Location: Mount Lavinia

Connecting Stepper Motors to Microcontrollers Tutorial

Post by Reginald » Thu Sep 17, 2009 1:06 am

Introduction
This section of tutorial will explain you everything that you need to know about stepper motors. 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 principle, different 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.

1. Unipolar stepper motor
The unipolar stepper motor has five or six wires and four coils (actually two coils divided by center connections on each coil). The center connections of the coils are tied together and used as the power connection. They are called unipolar steppers because power always comes in on this one pole.

2. Bipolar stepper motor
The bipolar stepper motor usually has four wires coming out of it. Unlike unipolar steppers, bipolar steppers have no common center connection. They have two independent sets of coils instead. You can distinguish them from unipolar steppers by measuring the resistance between the wires. You should find two pairs of wires with equal resistance. If you've got the leads of your meter connected to two wires that are not connected (i.e. not attached to the same coil), you should see infinite resistance (or no continuity).

As already said, we will talk mostly on "Unipolar stepper motors" which is most common type of stepper motor available in the market. A simple example of 6 lead step motor is given below and in 5 lead step motor wire 5 and 6 are joined together to make 1 wire as common.
stepper-coils.jpg
stepper-coils.jpg (25.31 KiB) Viewed 18647 times
Working of Stepper Motor
Now lets discuss the operation principle of a stepper motor. When we energize a coil of stepper motor, The shaft of stepper motor (which is actually a permanent magnet) align itself according to poles of energized coil. So when motor coils are energized in a particular sequence, motor shaft tend to align itself according to pole of coils and hence rotates. A small example of energizing operation is given below.
working.gif
working.gif (13 KiB) Viewed 18647 times
You can see in the example, when coil "A" is energized, A north-south polarity is generated at "A+A\" as shown in the figure above and magnetic shaft automatically align itself according to the poles generated. When the next coil is energized the shaft again align itself and take a step. Hence the working principle.
working1.gif
working1.gif (13.57 KiB) Viewed 18647 times
We have seen that to make the stepper motor work, we need to energize coil in a sequence. The explanation and generation of the sequence is explained in the next section of the tutorial.

Stepper motors can be driven in two different patterns or sequences. namely,
  • Full Step Sequence
  • Half Step Sequence
we will go through these sequences one by one.

Full Step Sequence
In the full step sequence, two coils are energized at the same time and motor shaft rotates. The order in which coils has to be energized is given in the table below.
seq.JPG
seq.JPG (8.5 KiB) Viewed 18647 times
The working of the full mode sequence is given in the animated figure below.
full-step.gif
full-step.gif (68.57 KiB) Viewed 18647 times
Half Step Sequence
In Half mode step sequence, motor step angle reduces to half the angle in full mode. So the angular resolution is also increased i.e. it becomes double the angular resolution in full mode. Also in half mode sequence the number of steps gets doubled as that of full mode. Half mode is usually prefer over full mode. Table below shows the pattern of energizing the coils.
seq2.JPG
seq2.JPG (13.44 KiB) Viewed 18647 times
The working of the half mode sequence is given in the animated figure below.
half-step.gif
half-step.gif (90.38 KiB) Viewed 18647 times
Step Angle
Step angle of the stepper motor is defined 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.

Step Sequence for 2-wire control of Unipolar stepper motor
As seen in above explanation, In every step of the sequence, two wires are always set to opposite polarities. Because of this, it's possible to control steppers with only two wires instead of four, with a slightly more complex circuit. The stepping sequence is the same as it is for the two coils A and B, and the opposite polarity value is given to A\ and B\. The sequence is given in the table below:
tbl1.JPG
tbl1.JPG (5.88 KiB) Viewed 18647 times
Step Sequence for Bipolar stepper motor
Bipolar motor has simpler construction. It has two windings with no center taps and a permanent magnet at the center just like unipolar stepper motors. Being simpler in construction, the stepping sequence is a little complex, as the power for both the coils has to be controlled in such a way that the polarity of the poles get reversed. This polarity sequence is shown in the table below.
tbl2.JPG
tbl2.JPG (9.37 KiB) Viewed 18647 times
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.
tbl3.JPG
tbl3.JPG (8.28 KiB) Viewed 18647 times
We have now learn most of the necessary things regarding a stepper motor. In the next section we will discuss about the various techniques to interface a stepper motor.

Connecting Unipolar Stepper Motor
There are actually many ways you can interface a stepper motor to your controller, out of them the most used interfaces are:
  1. Interface using L293D - H-Bridge Motor Driver
  2. Interface using ULN2003/2004 - Darlington Arrays
We will discuss both connection techniques one by one. The above mentioned methods need 4 controller pins for interface.

Connecting Unipolar stepper using L293D
l293d-stepper.gif
l293d-stepper.gif (6.92 KiB) Viewed 18647 times
As you see in the circuit above the four pins "Controller pin 1",2,3 and 4 will control the motion and direction of the stepper motor according to the step sequece programmed in the controller.

Connecting Unipolar stepper using ULN2003/2004
uln2003-stepper.gif
uln2003-stepper.gif (5.8 KiB) Viewed 18647 times
As already discussed in case of L293D, Here in this circuit too the four pins "Controller pin 1",2,3 and 4 will control the motion and direction of the stepper motor according to the step sequence sent by the controller.

2-wire connection for Unipolar Stepper Motor
We have seen the generally used 4-wire connection method for interfacing unipolar stepper motor, but we can simplify the design to make controller use less pins with the help of 2-wire connection method. The circuit for 2-wire connection is shown below.
unipolar_stepper.gif
unipolar_stepper.gif (8.26 KiB) Viewed 18647 times
Connecting Bipolar Stepper Motor
As we have studied that, Bi-polar stepper motors has 2 different coils. The step sequence for Bipolar stepper motor is same as that of unipolar stepper motors. The driving circuit for this require an H-Bridge as it allows the polarity of the power applied to be controlled independently. This can be done as shown in the figure below:
bipolar_stepper.gif
bipolar_stepper.gif (6.56 KiB) Viewed 18647 times
Now we have seen the methods for connecting stepper motors with your microcontroller. So keeping these circuits in mind,we will now look at the programming of microcontroller to control stepper motors. This is discussed in the next section of the tutorial.

Full step Sequence

Connect the motor from Port 1.0 to Port 1.3. Adjusting the delay will increase/decrease the speed.

Code: Select all

#include <REG2051.H>
#define stepper P1
void delay();

void main(){
        while(1){
                stepper = 0x0C;
                delay();
                stepper = 0x06;
                delay();
                stepper = 0x03;
                delay();
                stepper = 0x09;
                delay();
        }
}

void delay(){
        unsigned char i,j,k;
        for(i=0;i<6;i++)
                for(j=0;j<255;j++)
                        for(k=0;k<255;k++);
}
Assembly Code

Code: Select all

        org 0H

stepper equ P1

main:
        mov stepper, #0CH
        acall delay
        mov stepper, #06H
        acall delay
        mov stepper, #03H
        acall delay
        mov stepper, #09H
        acall delay
        sjmp main

delay:
        mov r7,#4
wait2:
        mov r6,#0FFH
wait1:
        mov r5,#0FFH
wait:
        djnz r5,wait
        djnz r6,wait1
        djnz r7,wait2
        ret
        end
Code will work as in the following illustration.
stepper.gif
stepper.gif (7.72 KiB) Viewed 18649 times
Half step Sequence

Code: Select all

void main(){
        while(1){
                stepper = 0x08;
                delay();
                stepper = 0x0C;
                delay();
                stepper = 0x04;
                delay();
                stepper = 0x06;
                delay();
                stepper = 0x02;
                delay();
                stepper = 0x03;
                delay();
                stepper = 0x01;
                delay();
                stepper = 0x09;
                delay();
        }
}
Assembly Code

Code: Select all

main:
        mov stepper, #08H
        acall delay
        mov stepper, #0CH
        acall delay
        mov stepper, #04H
        acall delay
        mov stepper, #06H
        acall delay
        mov stepper, #02H
        acall delay
        mov stepper, #03H
        acall delay
        mov stepper, #01H
        acall delay
        mov stepper, #09H
        acall delay
        sjmp main
Code will work as in the follows.
stepper1.gif
stepper1.gif (10.86 KiB) Viewed 18649 times
2-wire connection of Unipolar Stepper Motor

Code: Select all

void main(){
        while(1){
                stepper = 0x03;
                delay();
                stepper = 0x01;
                delay();
                stepper = 0x00;
                delay();
                stepper = 0x02;
                delay();
        }
}
Assembly Code

Code: Select all

main:
        mov stepper, #03H
        acall delay
        mov stepper, #01H
        acall delay
        mov stepper, #00H
        acall delay
        mov stepper, #02H
        acall delay
        sjmp main
Have a look at the following illustration.
stepper2.gif
stepper2.gif (12.96 KiB) Viewed 18649 times
Bipolar Stepper Motor

Code: Select all

void main(){
        while(1){
                stepper = 0x08;
                delay();
                stepper = 0x02;
                delay();
                stepper = 0x04;
                delay();
                stepper = 0x01;
                delay();
        }
}
Assembly Code

Code: Select all

main:
        mov stepper, #08H
        acall delay
        mov stepper, #02H
        acall delay
        mov stepper, #04H
        acall delay
        mov stepper, #01H
        acall delay
        sjmp main
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Re: Connecting Stepper Motors to Microcontrollers Tutorial

Post by Nandika » Sun Dec 18, 2011 7:05 pm

Very nice tutorial.
thank u

[ Post made via Mobile Device ] Image
sthn
Corporal
Corporal
Posts: 9
Joined: Sat Jan 05, 2013 9:14 am

Re: Connecting Stepper Motors to Microcontrollers Tutorial

Post by sthn » Sat Feb 16, 2013 8:34 pm

how to write pic code to control 3 steppers of a 3 axis cnc machine.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Connecting Stepper Motors to Microcontrollers Tutorial

Post by Neo » Sat Feb 16, 2013 11:05 pm

In a CNC, normally there will be 4 motors. 3 for controlling x, y and z axis. And one for the spindle (usually a drill). Normally the spindle is rotating in a constant RPM controllable by a variable resistor (You can use PWM here).

The 3 motors we used for x, y, z are high precision stepper motors. However there is no importance of RPM, just the accuracy. For a command given to move the spindle to (10, 5, 45), the motors should be able to move to the right location. Speeder is better.

Simultaneous movement of x, y, z motors are not essential. But you can add some simple mathematics to move the motors quickly with diagonal movements. To start with, you can move one motor at a time. So a function like this would make sense.

Move_Motor1 (location);
Move_Motor2 (location);
Move_Motor3 (location);

Or else and index based addressing...

Move_Motor(A, location); where A could be 0, 1 and 2 for x, y and z respectively.

There are many codes in this forum written to move motors, so I'm not going to comment on that. I just explained you the simple mechanism.
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

Re: Connecting Stepper Motors to Microcontrollers Tutorial

Post by SemiconductorCat » Sat Feb 16, 2013 11:37 pm

I think you could do that 3-axis CNC control using ladder easily.Why do you want to go into
PIC programming there? You could use simple proximity sensors or limit switches to get feedback.
In Printing machines we use limit switches.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Connecting Stepper Motors to Microcontrollers Tutorial

Post by Neo » Sun Feb 17, 2013 1:02 am

Normally the spindle is rotating in a constant RPM controllable by a variable resistor (You can use PWM here).
Sandun pointed out one of the statements which could be misinterpreted. Thank you Sandun.

It is not that I mean a variable resistor controls the speed of the spindle directly. It is the general technique to control speed of a motor using a microcontroller. We connect the variable resistor to a ADC pin of the micro and based on that control the PWM output. From there the PWM output controls the DC motor driver (L298 for example for DC motors with 4A).

General design of this could be found here https://robot.lk/viewtopic.php?f=92&t=2509
sthn
Corporal
Corporal
Posts: 9
Joined: Sat Jan 05, 2013 9:14 am

Re: Connecting Stepper Motors to Microcontrollers Tutorial

Post by sthn » Sun Feb 17, 2013 9:23 am

Simultaneous movement of x, y, z motors are not essential. But you can add some simple mathematics to move the motors quickly with diagonal movements
I think this method works only for the cnc drill machine, in order to build a cnc router/milling machine X and Y steppers should drive simultaneously, Z axis can be operated separately. I think I am right. If that so how can I do that. Do I have to use TDM or something.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Connecting Stepper Motors to Microcontrollers Tutorial

Post by Neo » Mon Feb 18, 2013 1:43 pm

I think this method works only for the cnc drill machine
No, that's the principle. Basically the accuracy is limited with the step-angle. Based on the step-angle of stepper motors, you can find the x, y movements in mm on the board. For example, say for one step of the stepper motor, it moves 1mm on the board, then if you make 10 steps, you get 10mm moved on the table.

For router and milling, with about example, you can get a cut with an accuracy of 1mm.
1.png
1.png (1.13 KiB) Viewed 15190 times
If you consider above example, to cut a line with 45 degree angle, with 1mm accurate steppers, you will get the cut as on the top image. When accuracy is higher (step angle is lower), you will get a cut as bottom image. But what you have to understand is, the motors are instructed one after the other.

To cut the 45 degree line as on above image, the instructions would be as below.

MoveX(1);
MoveY(1);
MoveX(1);
MoveY(1);
MoveX(1);
MoveY(1);
MoveX(10);

The g-code will be coming to you by one instruction after the other. There is no specification that I have seen for simultaneous motor operations.

When you are clear on the above operation, you can move on to next steps. Do not try to implement all the g-code operations. Just start with simple stuff. When it is working, you can make improvements.
sthn
Corporal
Corporal
Posts: 9
Joined: Sat Jan 05, 2013 9:14 am

Re: Connecting Stepper Motors to Microcontrollers Tutorial

Post by sthn » Mon Feb 18, 2013 7:26 pm

Thanks for the good explanation.
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

Re: Connecting Stepper Motors to Microcontrollers Tutorial

Post by SemiconductorCat » Tue Feb 19, 2013 1:40 am

Neo wrote:
I think this method works only for the cnc drill machine
No, that's the principle. Basically the accuracy is limited with the step-angle. Based on the step-angle of stepper motors, you can find the x, y movements in mm on the board. For example, say for one step of the stepper motor, it moves 1mm on the board, then if you make 10 steps, you get 10mm moved on the table.

For router and milling, with about example, you can get a cut with an accuracy of 1mm.
1.png
If you consider above example, to cut a line with 45 degree angle, with 1mm accurate steppers, you will get the cut as on the top image. When accuracy is higher (step angle is lower), you will get a cut as bottom image. But what you have to understand is, the motors are instructed one after the other.

To cut the 45 degree line as on above image, the instructions would be as below.

MoveX(1);
MoveY(1);
MoveX(1);
MoveY(1);
MoveX(1);
MoveY(1);
MoveX(10);

The g-code will be coming to you by one instruction after the other. There is no specification that I have seen for simultaneous motor operations.

When you are clear on the above operation, you can move on to next steps. Do not try to implement all the g-code operations. Just start with simple stuff. When it is working, you can make improvements.

IDEA:

You may be refering to the bersenham line algorithm in computer graphics.
It's taught on computer graphics , but I think it's applicable here.So simply you could modify the algorithm
to MoveX MoveY. Or send appropriate g-code to machine as neo told.

You could draw/trace circles/elipses/splines or bezier curves using the same modified version of the algorithm.

But there are already g-code generators like these,http://replicat.org/generators
Those are seems to be simple python scripts no more than 15KLOC. So I think it's not a much deal to
review the code and make appropriate modifications.

^sorry about the above post. I didn't realized what you actually looking for. It's about to implement
G-code instructions right?


I don't have any experience with g-code, suggesting this idea to use bersenham. But isn't that algorithms like
bersenham are supported in G-code level? So then you may still need to implement bersenham in your firmware.
Post Reply

Return to “Microcontrollers”