You are on page 1of 6

# UG108: Praxis II

## Asian Institute of Technology

Handout: Stepper Motor

January 2013
Instructor: Chaiyaporn Silawatchananai, Matthew N. Dailey

## Stepper Motor Tutorial

Introduction: In this tutorial we will learn how to run and control motion of a stepper motor using Arduino
Uno Rev3. By using stepper motor we can control the position or velocity of motor or rotational mechanic
without using any feedback sensors (open loop control).

Hardware Summary
Stepper Motor: is a motor that runs in step, its rotor turns through a specific number of degrees and
then stops. It converts electronic digital signals into mechanical motion in fixed increments. Each time an
incoming pulse is applied to the motor, its shaft turns or steps a specific angular distance. The shaft can
be driven in either direction and operated at low or very high stepping rates. Therefore the stepper motor
has the capability of controlling the velocity, distance, and direction of a mechanical load. It also produces
a holding torque at standstill to prevent unwanted motion or disturbance.
One attractive feature of the stepper motor is that it responds to digital signals. It can be controlled
by computers or a computer peripheral device. The stepper motor is typically used in an open-loop system
which position is determined by counting pulses. They are used in many practical applications such as
printer, CD players, floppy discs and X-Y positioning tables.
There are two types of stepper motor: unipolar motor and bipolar motor. We can distinguish bipolar motors
from unipolar motors by measuring the coil resistance. In bipolar motors we can find two wires with equal
resistance. Alternatively 4-wire steppers are strictly Bipolar, while 5 and 6 wire motors with center-taps
are Unipolar.
Unipolar motor contains centre tapped windings. Usually centre connection of coils are tied together
and used as the power connection. By using this arrangement a magnetic poles can be reversed without
reversing the direction of current. Thus the commutation circuit can be made very simple. This ease
of operation makes Unipolar Motor popular among electronics hobbyists.

## Figure 1: Unipolar Stepper Motor Windings, reproduced from http://mechatronics.mech.northwestern.

edu/design_ref/actuators/stepper_intro.html
Bipolar motors have no center tap connections. Current through a winding should be reversed to
reverse the magnetic poles. So the driving circuit should be more complicated. We can solve this by
using a H-bridge connection or by using IC-chips such as L293D or L298. A Bipolar motor is capable
of higher torque since entire coil(s) may be energized, not just half-coils.

## Figure 2: Bipolar Stepper Motor Windings, reproduced from http://mechatronics.mech.northwestern.

edu/design_ref/actuators/stepper_intro.html
The control sequences are shown below, using + and - symbols to indicate the polarity of the power
applied to each motor terminal:
Terminal
Terminal
Terminal
Terminal

1a
1b
2a
2b

+---+---+---+----+---+---+---+-+---+---+---+----+---+---+---+

ULN2003 IC: is a high-voltage high-current Darlington transistor arrays. Each consists of seven NPN Darlington pairs that feature high-voltage outputs with common-cathode clamp diodes for switching inductive
loads. The collector-current rating of a single Darlington pair is 500 mA. Applications include relay drivers,
hammer drivers, lamp drivers, display drivers (LED and gas discharge), line drivers, and logic buffers. This
IC-chip has a bunch of transistors embedded in a single housing. It allows the connection of devices and
components that need much higher current than the ones that the our Arduino board can offer.

## Simple stepper motor control

Components required
1. Arduino UNO R3
2. Proto-board
3. Jumper (Connecting wires)
4. IC ULN2003A
5. IC L293D
6. Stepper motor
7. 5-12V DC power supply
Arduino pin numbers
pin 8 as output for motor pin A
pin 9 as output for motor pin B
pin 10 as output for motor pin C
pin 11 as output for motor pin D

## Figure 4: Unipolar Stepper motor with Arduino

You can connect the circuit as in the Figure 3 for bipolar stepper motor control or Figure 4 for unipolar
stepper motor control. We will use 4 outputs pins from the Arduino to control the motion of stepper motor.
Code-1: Write your program to make the motor spin and control the direction by using serial communication, the code given below is not complete, you should be able to write a complete working code.
Pin assignments
int
int
int
int
int

motorPin1
motorPin2
motorPin3
motorPin4
delayTime

=
=
=
=
=

8;
9;
10;
11;
10;

## The setup function

pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
Serial.begin(9600);
The loop function
byte val;
if(Serial.available()){
switch(val){
case 'A':moveForward(); break;
case 'D':moveBackward(); break;
case 'W':delayTime--;Serial.println(delayTime); break;
case 'S':delayTime++;Serial.println(delayTime); break;
}
}
The moveForward function
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(delayTime);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(delayTime);

command stepper motor rotate and Press B to move in oppositive direction. When we send command
once, the stepper motor rotates for 4 steps according to moveForward function. So we can find the number
of steps per revolution. To change direction, change the control sequence. We can adjust the speed by Press
W or S which the delay time of each sequence is increased or decreased.
Code-2: Write your program to control the motor shaft to absolute position by using serial communication,
the code given below is not complete, you should be able to write a complete working code.
Pin assignment and defined variable
int motorPinS[] = {8,9,10,11};
//working variable
int delayTime = 10;
int steps = 200; //number of steps per rev
int gear = 1;
int step_counter = 0;
The setup function
int i = 0;
for(i=0; i<4; i++){
pinMode(motorPinS[i], OUTPUT);
}
Serial.begin(9600);
Define the function to tell motor to move a certain angle
void moveAngle(float angle){
int i;
int howmanysteps = angle/stepAngle();
if(howmanysteps<0){
howmanysteps = -howmanysteps;
}
if(angle>0){
for(i = 0;i<howmanysteps; i++){
Forward(i%4);
delay(delayTime);
}
}else{
for(i=0;i<howmanysteps;i++){
Backward(i%4);
delay(delayTime);
}
}
}
void moveToAngle(float angle){
moveAngle(angle-actualAngle());
}
// actual stepper motor angle
float actualAngle(){
return step_counter*stepAngle();
}
// angle made by each step

float stepAngle(){
return (360.0)/(steps*gear);
}
// backward step
void Backward(int coil){
int i;
int count2[] = {1, 2, 4, 8};
for(i=0; i<4; i++){
digitalWrite(motorPinS[3-i], count2[coil]>>i&0x01);
}
step_counter--;
}
// forward step
void Forward(int coil){
int i;
int count2[] = {1, 2, 4, 8};
for(i=0; i<4; i++){
digitalWrite(motorPinS[i], count2[coil]>>i&0x01);
}
step_counter++;
}
The loop function
byte val;
if(Serial.available()){
switch (val) {
case 'A': moveToAngle( 0); break;
case 'B': moveToAngle( 45); break;
case 'C': moveToAngle( 90); break;
case 'D': moveToAngle(135); break;
case 'E': moveToAngle(180); break;
case 'F': moveToAngle(225); break;
case 'G': moveToAngle(270); break;
case 'H': moveToAngle(315); break;
case 'W': delayTime--; break; //increase speed
case 'S': delayTime++; break; //decrease speed
}
Serial.print(val);
}
Assignment: Write Arduino Program to rotate stepper motor for 30 degrees in every 1 seconds.

References
http://arduino.cc/en/Tutorial/StepperUnipolar
http://mechatronics.mech.northwestern.edu/design_ref/actuators/stepper_intro.html
http://silveiraneto.net/2009/03/16/bumbabot-1
Industrial Control Electronics: Devices, Systems & Application by - Terry Bartelt.