Figure 1. Stepper motor wiring diagrams. Use an ohm meter to determine type. For the hobbyist, one way to distinguish common wire from a coil-end wire is by measuring the resistance. Resistance between common wire and coil-end wire is always half of what it is between coil-end and coil-end wires. This is due to the fact that there is actually twice the length of coil between the ends and only half from center (common wire) to the end.

Using a Unipolar Stepper Motor with a Microcontroller

by Lewis Loflin

Here we will examine the basic operation of a unipolar stepper motor. I'll cover a bipolar stepper motor on a different page. A unipolar stepper motor has two windings per phase, one for each direction of magnetic field. Since in this arrangement a magnetic pole can be reversed without switching the direction of current, the commutation circuit can be made very simple (eg. a single transistor) for each winding. Typically, given a phase, one end of each winding is made common: giving three leads per phase and six leads for a typical two phase motor. Often, these two phase commons are internally joined, so the motor has only five leads. Others can have six leads.

A microcontroller or stepper motor controller can be used to activate the drive transistors in the right order, and this ease of operation makes unipolar motors popular with hobbyists. They are probably the cheapest way to get precise angular movements.

Bipolar motor: Bipolar motors have a single winding per phase. The current in a winding needs to be reversed in order to reverse a magnetic pole, so the driving circuit must be more complicated, typically with an H-bridge arrangement. There are two leads per phase, none are common. Stepper motors consist of a permanent magnet rotating shaft, called the rotor, and electromagnets on the stationary portion that surrounds the motor, called the stator. Controlling the sequence will cause the rotor to move. The electromagnets are energized by an external control circuit, such as a microcontroller.

 Figure 2. Basic stepper motor construction.

 Figure 3. Two phases on for more torque. When half stepping, the drive alternates between two phases on and a single phase on. This increases the angular resolution (less degrees per step), but the motor also has less torque at the half step position (where only a single phase is on). This may be mitigated by increasing the current in the active winding to compensate. The advantage of half stepping is that the drive electronics need not change to support it. In the examples below I only use two-phase single step for high torque. For more technical detail see Stepper Motor Basis from Microchip. (PDF file)

Schematic for this program.

Example stepper motor pictures here and here.

```
/* Program 1

Stepper motor demo for pf35t-48 and 55mod48 and Airpax
This particular stepper motor is 7.5 degrees per step
so it requires 48 steps for 360 degrees.

Another motor this circuit/program was tested on was a Teac motor
removed from a 5.25 inch floppy drive. At 1.8 degrees per step it
required 200 steps for 360 degrees. All wire colors were brown.

Depending on the type motor swap the four numbers below (8 - 11) until motor works.

The sequence below was derived at by experimentation.

Speed is controlled by a delay between each step.
The longer the delay the slower the rotation.
That delay value is obtained by reading and analog-to-digital
cover (AD0 in this case) which gives a value from 0 to 1023.
See diagram below.
The value is divided by 4 and add 10 for a delay
For faster speeds change 10 to say 2.
This is calculated between every step to vary speed while stepping.

The commands below will be compiled into machine code and uploaded
to the microcontroller.

Compiled size 1896 bytes.

*/

#define yellow 8  //Q1
#define orange 9  //Q2
#define brown 10  // Q3
#define black 11 // Q4

#define CW 3 // SW0 in schematic
#define CCW 4  //SW1 in schematic

void setup()  {

pinMode(CW, INPUT);
pinMode(CCW, INPUT);

digitalWrite(CW, 1); // pull up on
digitalWrite(CCW,1); // pull up on

pinMode(black, OUTPUT);
pinMode(brown, OUTPUT);
pinMode(orange, OUTPUT);
pinMode(yellow, OUTPUT);

// all coils off
digitalWrite(black, 0);
digitalWrite(brown, 0);
digitalWrite(orange, 0);
digitalWrite(yellow, 0);

}

void loop() {

forward(200);
all_coils_off();
}

reverse(200);
all_coils_off();
}

} // end loop

void all_coils_off(void)  {

digitalWrite(black, 0);
digitalWrite(brown, 0);
digitalWrite(orange, 0);
digitalWrite(yellow, 0);

}

void reverse(int i) {

while (1)   {

digitalWrite(black, 1);
digitalWrite(brown, 0);
digitalWrite(orange, 1);
digitalWrite(yellow, 0);
i--;
if (i < 1) break;

digitalWrite(black, 0);
digitalWrite(brown, 1);
digitalWrite(orange, 1);
digitalWrite(yellow, 0);
i--;
if (i < 1) break;

digitalWrite(black, 0);
digitalWrite(brown, 1);
digitalWrite(orange, 0);
digitalWrite(yellow, 1);
i--;
if (i < 1) break;

digitalWrite(black, 1);
digitalWrite(brown, 0);
digitalWrite(orange, 0);
digitalWrite(yellow, 1);
i--;
if (i < 1) break;
}

}

void forward(int i) {

while (1)   {

digitalWrite(black, 1);
digitalWrite(brown, 0);
digitalWrite(orange, 0);
digitalWrite(yellow, 1);
i--;
if (i < 1) break;

digitalWrite(black, 0);
digitalWrite(brown, 1);
digitalWrite(orange, 0);
digitalWrite(yellow, 1);
i--;
if (i < 1) break;

digitalWrite(black, 0);
digitalWrite(brown, 1);
digitalWrite(orange, 1);
digitalWrite(yellow, 0);
i--;
if (i < 1) break;

digitalWrite(black, 1);
digitalWrite(brown, 0);
digitalWrite(orange, 1);
digitalWrite(yellow, 0);
i--;
if (i < 1) break;
}
}

```

```

/* Program 2

This program works much the way program 1 does. In this case
we send the delay time between steps to the subroutine and
not read the analog in every time.

Thus we can program an unlimited combinations for speed, steps,
and direction.

Stepper motor demo for pf35t-48 and 55mod48 and Airpax
This particular stepper motor is 7.5 degrees per step
so it requires 48 steps for 360 degrees.

Another motor this circuit/program was tested on was a Teac motor
removed from a 5.25 inch floppy drive. At 1.8 degrees per step it
required 200 steps for 360 degrees. All wire colors were brown.

Compiled size 1756 bytes.

*/

#define black 8 // Q1
#define brown 9  // Q2
#define orange 10  //Q3
#define yellow 11  //Q4
#define CW 3  // SW0 in schematic
#define CCW 4  // SW2 in schematic

void setup()  {

pinMode(black, OUTPUT);
pinMode(brown, OUTPUT);
pinMode(orange, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(CW, INPUT);
pinMode(CCW, INPUT);

digitalWrite(CW, 1);  // pull up on
digitalWrite(CCW,1); // pull up on

digitalWrite(black, 0);
digitalWrite(brown, 0);
digitalWrite(orange, 0);
digitalWrite(yellow, 0);

}

void loop() {

// this will speed up
forward(200, 50);
reverse(200, 40);
forward(200, 30);
reverse(200, 20);
forward(200, 10);
reverse(200, 5);
all_coils_off();
}

// random
reverse(28, 100);
forward(100, 50);
reverse(200, 5);
reverse(75, 150);
forward(50, 50);
reverse(125, 20);
all_coils_off();
}

} // end loop

void all_coils_off(void)  {

digitalWrite(black, 0);
digitalWrite(brown, 0);
digitalWrite(orange, 0);
digitalWrite(yellow, 0);

}

void reverse(int i, int j) {

while (1)   {

digitalWrite(black, 1);
digitalWrite(brown, 0);
digitalWrite(orange, 1);
digitalWrite(yellow, 0);
delay(j);
i--;
if (i < 1) break;

digitalWrite(black, 0);
digitalWrite(brown, 1);
digitalWrite(orange, 1);
digitalWrite(yellow, 0);
delay(j);
i--;
if (i < 1) break;

digitalWrite(black, 0);
digitalWrite(brown, 1);
digitalWrite(orange, 0);
digitalWrite(yellow, 1);
delay(j);
i--;
if (i < 1) break;

digitalWrite(black, 1);
digitalWrite(brown, 0);
digitalWrite(orange, 0);
digitalWrite(yellow, 1);
delay(j);
i--;
if (i < 1) break;
}

}

void forward(int i, int j) {

while (1)   {

digitalWrite(black, 1);
digitalWrite(brown, 0);
digitalWrite(orange, 0);
digitalWrite(yellow, 1);
delay(j);
i--;
if (i < 1) break;

digitalWrite(black, 0);
digitalWrite(brown, 1);
digitalWrite(orange, 0);
digitalWrite(yellow, 1);
delay(j);
i--;
if (i < 1) break;

digitalWrite(black, 0);
digitalWrite(brown, 1);
digitalWrite(orange, 1);
digitalWrite(yellow, 0);
delay(j);
i--;
if (i < 1) break;

digitalWrite(black, 1);
digitalWrite(brown, 0);
digitalWrite(orange, 1);
digitalWrite(yellow, 0);
delay(j);
i--;
if (i < 1) break;
}

}

```

You Tube Arduino Microcontroller Video Series March 2012:

Arduino demos:

Added January 2012: PICAXE Micro-controller Projects!

The PICAXE series of micro-controllers rank as the easiest and most cost effective way to use Microchip processors. I wanted an easier and less expensive way to introduce my students to the "PIC" micro-controller. Here I hope to get those starting out past poorly written literature and lack of simple working code examples.