Unipolar Stepper Motor

Learn how to drive a unipolar stepper motor commonly found in old floppy drives!

Introduction

This page shows two examples on how to drive a unipolar stepper motor. These motors can be found in old floppy drives and are easy to control. The one we use has 6 connectors of which one is power (VCC) and the other four are used to drive the motor sending synchronous signals.

The first example is the basic code to make the motor spin in one direction. It is aiming those that have no knowledge in how to control stepper motors. The second example is coded in a more complex way, but allows to make the motor spin at different speeds, in both directions, and controlling both from a potentiometer.

The prototyping board has been populated with a 10K potentiometer that we connect to an analog input, and a ULN2003A driver. This 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 ATMEGA8 from our Arduino board can offer.

Goals

The goals of this tutorial are:

  • Learn about stepper motors
  • Learn how to drive a unipolar stepper motor
  • Explore different sketches to drive it

Hardware & Software Needed

  • Arduino Board
  • Unipolar stepper motor (Can be found in old floppy drives)
  • ULN2003A driver
  • 10k Ω potentiometer
  • Jumper wires
  • Arduino IDE (online or offline).

Stepper Motors

Stepper motors are different from DC motors, in that you make them move by sending them phased pulses of current, making the motor move in "steps." In order to do this, you need to send pulse trains of varying polarity to multiple windings, the speed of the motor being determined by the frequency of the pulses, and the direction of motor motion being determined by the phasing between the pulses being applied to the various windings. As a result, you get more-precise control over motor motion, but at the cost of some control circuitry complexity.

If you want to know more about stepper motors, the link below may be useful for you to understand the underlying theory behind them:

Picture of a protoboard supporting the ULN2003A and a potentiometer
Picture of a protoboard supporting the ULN2003A and a potentiometer

Programming the Board

We will now get to the programming part of this tutorial.

Open the editor of your choice, and make sure you have your board installed. Select your board and port, and upload any of the two examples below.

If you need help setting up your environment, you can use our interactive getting started guide, found on the hardware documentation page.

Code

Example 1: Simple Example

The first example is the basic code to make the motor spin in one direction.

1/* Stepper Copal
2 * -------------
3 *
4 * Program to drive a stepper motor coming from a 5'25 disk drive
5 * according to the documentation I found, this stepper: "[...] motor
6 * made by Copal Electronics, with 1.8 degrees per step and 96 ohms
7 * per winding, with center taps brought out to separate leads [...]"
8 * [http://www.cs.uiowa.edu/~jones/step/example.html]
9 *
10 * It is a unipolar stepper motor with 5 wires:
11 *
12 * - red: power connector, I have it at 5V and works fine
13 * - orange and black: coil 1
14 * - brown and yellow: coil 2
15 *
16 * (cleft) 2005 DojoDave for K3
17 * http://www.0j0.org | http://arduino.berlios.de
18 *
19 * @author: David Cuartielles
20 * @date: 20 Oct. 2005
21 */
22
23int motorPin1 = 8;
24int motorPin2 = 9;
25int motorPin3 = 10;
26int motorPin4 = 11;
27int delayTime = 500;
28
29void setup() {
30 pinMode(motorPin1, OUTPUT);
31 pinMode(motorPin2, OUTPUT);
32 pinMode(motorPin3, OUTPUT);
33 pinMode(motorPin4, OUTPUT);
34}
35
36void loop() {
37 digitalWrite(motorPin1, HIGH);
38 digitalWrite(motorPin2, LOW);
39 digitalWrite(motorPin3, LOW);
40 digitalWrite(motorPin4, LOW);
41 delay(delayTime);
42 digitalWrite(motorPin1, LOW);
43 digitalWrite(motorPin2, HIGH);
44 digitalWrite(motorPin3, LOW);
45 digitalWrite(motorPin4, LOW);
46 delay(delayTime);
47 digitalWrite(motorPin1, LOW);
48 digitalWrite(motorPin2, LOW);
49 digitalWrite(motorPin3, HIGH);
50 digitalWrite(motorPin4, LOW);
51 delay(delayTime);
52 digitalWrite(motorPin1, LOW);
53 digitalWrite(motorPin2, LOW);
54 digitalWrite(motorPin3, LOW);
55 digitalWrite(motorPin4, HIGH);
56 delay(delayTime);
57}

Example 2: Stepper Unipolar Advanced

The second example is a bit more advanced, it allows the motor to spin at different speeds, in both directions with the use of a potentiometer.

1/* Stepper Unipolar Advanced
2 * -------------------------
3 *
4 * Program to drive a stepper motor coming from a 5'25 disk drive
5 * according to the documentation I found, this stepper: "[...] motor
6 * made by Copal Electronics, with 1.8 degrees per step and 96 ohms
7 * per winding, with center taps brought out to separate leads [...]"
8 * [http://www.cs.uiowa.edu/~jones/step/example.html]
9 *
10 * It is a unipolar stepper motor with 5 wires:
11 *
12 * - red: power connector, I have it at 5V and works fine
13 * - orange and black: coil 1
14 * - brown and yellow: coil 2
15 *
16 * (cleft) 2005 DojoDave for K3
17 * http://www.0j0.org | http://arduino.berlios.de
18 *
19 * @author: David Cuartielles
20 * @date: 20 Oct. 2005
21 */
22
23int motorPins[] = {8, 9, 10, 11};
24int count = 0;
25int count2 = 0;
26int delayTime = 500;
27int val = 0;
28
29void setup() {
30 for (count = 0; count < 4; count++) {
31 pinMode(motorPins[count], OUTPUT);
32 }
33}
34
35void moveForward() {
36 if ((count2 == 0) || (count2 == 1)) {
37 count2 = 16;
38 }
39 count2>>=1;
40 for (count = 3; count >= 0; count--) {
41 digitalWrite(motorPins[count], count2>>count&0x01);
42 }
43 delay(delayTime);
44}
45
46void moveBackward() {
47 if ((count2 == 0) || (count2 == 1)) {
48 count2 = 16;
49 }
50 count2>>=1;
51 for (count = 3; count >= 0; count--) {
52 digitalWrite(motorPins[3 - count], count2>>count&0x01);
53 }
54 delay(delayTime);
55}
56
57void loop() {
58 val = analogRead(0);
59 if (val > 540) {
60 // move faster the higher the value from the potentiometer
61 delayTime = 2048 - 1024 * val / 512 + 1;
62 moveForward();
63 } else if (val < 480) {
64 // move faster the lower the value from the potentiometer
65 delayTime = 1024 * val / 512 + 1;
66 moveBackward();
67 } else {
68 delayTime = 1024;
69 }
70}

Testing It Out

After you have uploaded the code from the first example, the motor should now start to spin in one direction. If you upload the second example, you should be able to control the motor using the potentiometer.

Troubleshoot

If the code is not working, there are some common issues we can troubleshoot:

  • Stepper motor or potentiometer are not wired correctly.
  • The motor is broken.
  • Make sure that no
    {}
    brackets are missing.

Conclusion

In this example, we tested out different sketches that allows us to drive a stepper motor that can be found in old floppy disks. Electronic components such as the stepper motor can be salvaged from old or broken devices. So next time something breaks you might be able to harvest some nice components and create cool projects with them!