Introduction: Tutorial:DC Motors-3/3 Steppers

This is a continuation of the tutorial started in DC Motors Tutorial-1/3: Continuous, H-Bridge, Gear. With about 88,000 views, many found Part 1 of the tutorial of interest. Hopefully, if you were one of the viewers of Part 1, or a new reader, you will find this last Part of the tutorial interesting and helpful.

Part 3 of the tutorial starts with this Introduction, then moves to Step 1, which could really be considered Step 8 of the previous tutorial and Step 14 of the tutorial begun in Part 1.

As noted in the introduction to Parts 1 and 2, DC motors are ubiquitous. They convert electrical/magnetic energy, produced by a wire carrying current in a magnetic field, to motion, and they appear in all sorts of appliances and applications, e.g., they are found in small fans, ceiling fans, air cleaners, solder fume extractors, quadcopters, small helicopters, and other drones, hand-held rotary tools, circular saws, drills, lathes, sanders, cars, robots (where they rotate tires, or move robotic arms, etc.), aquarium air pumps, Maker projects, and many other areas. This tutorial only discusses DC motors. The categorization of motors found here is not mutually exclusive, as there is overlap between what may initially seem like entirely different motors. Usually, this is due to the target function of these motors rather than the theory behind them.

This Part of the tutorial covers stepper motors.

Step 1: Stepper Motors

The purpose of a stepper motor, also known simply as a stepper, is to go to a specific angular position and stop and hold there. Each increment of a stepper motor is called a step, from which the stepper motor gets its name. The key parameters for a stepper motor are its angular resolution, and its torque. The more steps the greater the motor's resolution. Some stepper motors can be thought of as a class of BLDC inrunner motors. When stopped a stepper holds its position with its holding torque. In recent times, with the preponderance of digital electronics, stepper motors are seeing a significant increase in their use.

There are four common ways to drive stepper motors:

1. Wave Drive

2. Full Step

3. Half Step

4. Microstepping

These methods are fairly easy to understand in conjunction with the attached pictures. In the first four photographs a single magnet is placed in turn in one of four positions, to move a compass needle from 12 to 3 to 6 to 9 o'clock positions respectively. That is, the compass needle is turned 90 degrees at a time, representing four steps. In a “real world” stepper, such as the NEMA 17, shown in Part 1's Introductory Step, each 90 degree turn shown in the photographs attached here would actually be a step of 1.8 degrees.

This approach is also called a single, or one, phase method, as only one set of coils in the stepper would be turned on at any time. The angular rotation for a single step is sometimes referred to the basic step angle. In an actual stepper an electrical field produced by coils replaces the permanent Neodymium bar magnet used in the example photographs. The Wave Drive approach is not as commonly used as might be expected, as the rotor (or in our case the compass needle) is only held in place by a single magnetic field. Using a Wave Drive approach, the rotor would produce a relatively low torque. (Please see the table at the end of this step for resolution and torque parameters as applicable to the various types of stepper motors discussed here.)

In the Full Step approach (the second group of four pictures) two fields are used (i.e., two coils are energized together) to move the rotor (in our example the compass needle) between two magnetic fields. Here since two magnetic fields are used the rotor would have greater torque. The number of steps is the same as for the Wave Drive method, but the torque is greater. In fact, we can use vector mathematics, or simple geometry, to see that if we use two magnetic fields as we have, we would obtain an additional increase of torque, compared to the Wave Drive method, in excess of 40%.

This approach is known as a dual or two phase method, as two sets of coils are energized simultaneously. Again in a “real world” example, such as the NEMA stepper used as a generator in Part 1, each step would be 1.8 degrees.

If we combine the Wave method and the Full Step approach, e.g., by turning on a magnetic field at the 12 o'clock position, then two fields at the 12 and 3 positions, then one field at the 3 clock position, two fields at the 3 and 6 clock positions, etc. we would get twice the number of steps, i.e., the needle of the compass would move in half steps. We would obtain twice the number of steps we did with the Wave Drive or Full Step methods. For our compass example that would be eight steps instead of the four previously discussed. For the real world example cited above, this would mean each step would be ½ of 1.8 degrees, yielding greater positioning accuracy but with a reduction in torque are some points, compared to the Full Step approach.

Lastly, we have microstepping (where the steps can be smaller still). Microstepping is accomplished by activating the coils so they are not always fully energized. That is, we energize the coils incrementally. This is often done by sending sinusoidal signals to the coils as appropriate. The problem with using microstepping is that we lose torque, perhaps 30% or more. Also positioning as desired may prove difficult, and accurate positioning may require adding encoders that would further raise the price of the stepper motor.

Stepper motors can be categorized one of three ways (other categorizations are also possible): permanent magnet (PM), variable reluctance (VR), and hybrid (HY) also known as hybrid synchronous. PM steppers use permanent magnets mounted on their rotors. The rotors are located inside the stators, as is true for all inrunner motors. The stators for PM steppers contain coils/windings that are placed in slots. Each set of connected windings is referred to as a phase. The motion of the rotor is controlled by sending specifically ordered and timed signals to the windings.

Probably, the most popular stepper motor with makers is the 28BYJ-48. It is shown in the attached photographs. If this stepper is taken apart, one finds the motor connects to a gear train. This reduction gear train provides approximately 1:64 reduction gearing. (Please see attached photographs.) In fact, if you have an unknown stepper that has its shaft off-center, as does the 28BYJ-48, you likely have a geared stepper, as the off-center shaft provides room for gears to be placed between the rotor and the shaft. You may find it helpful to read, or re-read if needed, Part 1 of this tutorial where gear motors are discussed in more detail.

Not all stepper motors with the designation 28BYJ-48 are equal. I do not necessarily mean in quality, although clearly they are not all of the same quality, but in underlying motor parameters such as resolution, torque, gearing ratios, etc. That is not all 28BYJ-48 motors are the same, based on this author's experience, even though having the same designation might suggest otherwise.

When we look inside the 28BYJ-48 stepper motor shown in the attached photographs, we can see there is a permanent magnet rotor at the stepper’s center. At specific times the rotor is attracted to one of the 32 “teeth” shown in the attached photographs, eight of which are shown in the removed top tooth plate. The motor itself has two coils that are used to magnetize the teeth in sequence as required to position the stepper shaft to turn a step. We only need to count the steps taken by the motor to move to a specific angle.

A full step, for the 28BYJ-48 shown, is one over (32 times 64) of a full revolution, i.e. 1/2,048 of 360 degrees. If we were to use half-steps this would yield 1/4096 of a full revolution. Thus either full or half-steps provide us with excellent positioning capabilities at a relatively low price. I was able to purchase these motors for about $1.00 each in multiple quantities. Individually, they can be found for less than $1.25 apiece.

The variable reluctance (VR) stepper motor uses a non-magnetic rotor. There are more stator poles than rotor poles. Resolution can be increased as more poles are added to the rotor. These steppers typically have 3 to 6 stator windings that are energized in the opposite direction to what we do for permanent magnet rotors. The step angle is ½ what it would be for the same number of stator coils in a PM stepper, i.e., these steppers have greater resolution. The torque-speed ‘curve’ is slightly different from the curve shown previously, in that the curve for this type of stepper motor has a “knee” close to the torque axis, but is essentially linear after the knee.

The hybrid (HY) motor, as the name might imply, takes elements from PM and VR steppers. The rotor for a hybrid stepper has teeth and is a permanent magnetic, as in a PM motor. These motors have two “cups” on the rotor often designated as north and south, or cup 1 and cup 2, or front cup and rear cup. The teeth on one of the cups is 1 tooth displaced from the teeth on the other cup. The stator also has teeth. The stator teeth are energized to get the rotor to turn to the desired position.

The attached table can help in the selection of an appropriate stepper motor (it represents the typical parameters for the three categories of stepper just mentioned). As can be seen in the resolution-torque table, the goals sought for resolution and torque may help identify the category of stepper that might be chosen.

Remember that there is a trade-off between speed and torque. A small stepper, with high gear reduction can have exceptionally high torque but at the expensive of speed. So, while reduction gearing is an option, a small stepper typically must sacrifice speed to obtain high torque.

High resolution and high accuracy still require more expensive servo motors. However, less complex stepper motors will usually do the job needed at considerably lower cost. They are typically less expensive as they contain no mechanical commutator. Possibly, and in this author's experience, most maker steppers contain permanent magnets.

As can be seen in the attached photographs, it is difficult to tell whether a stepper is a PM, VR, or HY stepper from external appearance alone. However, if we disassemble the stepper, an examination of the rotor would easily, and quickly, allow us to identify the stepper category.

Step 2: An Arduino Project Using a Stepper Motor and the Wave Drive Method

Our first Arduino project with stepper motors, as well as some subsequent projects here, uses the inexpensive 28BYJ-48 stepper motor in conjunction with the ULN2003 stepper motor driver module.

In the first sketch below, following this Step, the full wave approach is used to rotate the stepper motor. That is, the field coils are energized one phase at a time. Looking at the disassembly of this stepper that was performed earlier, or using an ohmmeter, we find that the stator coils of the stepper are connected to wire pairs: blue - yellow, and pink - orange.

In the sketch that follows we first energize the blue and yellow coil, then the pink and orange coil, and then reverse current to the blue and yellow coil, and then the pink and orange coil to get the stepper to rotate by steps. A coil is energized and a 75 millisecond delay is used before the next coil is energized, so that the steps can be clearly seen as the rotor turns. This results in a rotation that is very slow, but it does allow us, as noted, to see each step taken by the rotor.

Step 3: Sketch for an Arduino Project Using Wave Drive Method

This sketch is used to drive a 28BYJ-49 stepper motor using the Wave Drive method and a ULN2003 stepper motor driver module.

To see the sketch exactly as written, with spaces as included, download the text file.

----------Sketch----------

// Sketch by R. Jordan Kreindler, written September 2016, to rotate a stepper motor

// using the Wave Drive Method

int aPin = 2; //IN1: first end of the blue and yellow stator coil

int bPin = 3; //IN2: first end of the pink and orange stator coil

int aPrimePin = 4; //IN3: second end of the blue and yellow stator coil

int bPrimePin = 5; //IN4: second end of the pink and orange stator coil

// We do not connect IN5, IN6, or IN7

int delay1 = 75;

void setup() {

// Set all pins as output to send output signals from the Arduino

// UNO to the coil windings of the stator

pinMode(aPin, OUTPUT);

pinMode(bPin, OUTPUT);

pinMode(aPrimePin, OUTPUT);

pinMode(bPrimePin, OUTPUT);

// Start with all coils off

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

}

void loop() {

// Send current to the apin, and then the bpin

// then reverse the current and send reversed current

// to aPrimePin and then bPrimePin.

// 1. Energize the blue yellow stator coil

digitalWrite(aPin, HIGH);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 2. Energize the blue yellow stator coil

digitalWrite(aPin, LOW);

digitalWrite(bPin, HIGH);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay 1 microseconds

// 3. Reverse current direction and energize the blue yellow stator coil

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, HIGH);

digitalWrite(bPrimePin, LOW);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 4. Reverse current direction and energize the pink orange stator coil

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, HIGH);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

}

Step 4: Sketch for an Arduino Project Using Full Step Method

Here the delay between subsequent activations is 150 milliseconds, i.e., longer than before, to show the steps even more clearly, and also to allow us to see the order of lighting of the LEDs, on the driver module board. These LEDs show which coils are being energized. Because the delay is longer, the stepper rotor moves more slowly. To see the sketch exactly as written, including whitespace, please download the text file.

----------Sketch----------

// Sketch by R. Jordan Kreindler, written September 2016,

// to rotate a stepper motor using the Full Step Method

int aPin = 2; //IN1: first end of the blue and yellow stator coil

int bPin = 3; //IN2: first end of the pink and orange stator coil

int aPrimePin = 4; //IN3: second end of the blue and yellow stator coil

int bPrimePin = 5; //IN4: second end of the pink and orange stator coil

// We do not connect IN5, IN6, or IN7

int delay1 = 150;

void setup() {

// Set all pins as output to send output signals from the Arduino

// UNO to the coil windings of the stator

pinMode(aPin, OUTPUT);

pinMode(bPin, OUTPUT);

pinMode(aPrimePin, OUTPUT);

pinMode(bPrimePin, OUTPUT);

// Start with all coils off

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

}

void loop() {

// Send current to

// 1. The aPin, and the bPin

// 2. Then to the bPin and the aPrimePin

// 3. Then to the aPrimePin and the bPrime Pin

// 4. Then the bPrimePin and the aPin.

// Thus producing steps using two coils activated at the same time

// to increase torque.

// 1. Energize the blue yellow, and pink orange stator coils

digitalWrite(aPin, HIGH);

digitalWrite(bPin, HIGH);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 2. Energize the pink orange, and in reverse current direction

// the blue yellow stator coil

digitalWrite(aPin, LOW);

digitalWrite(bPin, HIGH);

digitalWrite(aPrimePin, HIGH);

digitalWrite(bPrimePin, LOW);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 3. Energize in reverse current directions the blue yellow and

// pink orange coils

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, HIGH);

digitalWrite(bPrimePin, HIGH);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 4. Energize the blue yellow, and in reverse current direction

// the pink orange coils

digitalWrite(aPin, HIGH);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, HIGH);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

}

Step 5: Sketch for an Arduino Project Using Half Step Method

In this sketch the half step method is used. The delay between coil activations is 3 milliseconds, so a relatively smooth rotation occurs. The delay is so short that the LEDs appear to flicker rapidly, or some might even say the LEDs continue to stay on.

The hardware configuration is the same as used previously, only the sketch was changed to use the half step method for rotating the DC stepper motor.

To see the sketch exactly as written, with spaces as they were entered, please download the attached text file.

----------Sketch----------

// Sketch by R. Jordan Kreindler, written September 2016,

// to rotate a stepper motor using the Half Step Method

int aPin = 2; //IN1: first end of the blue and yellow stator coil

int bPin = 3; //IN2: first end of the pink and orange stator coil

int aPrimePin = 4; //IN3: second end of the blue and yellow stator coil

int bPrimePin = 5; //IN4: second end of the pink and orange stator coil

// We do not connect IN5, IN6, or IN7

int delay1 = 3;

void setup() {

// Set all pins as output to send output signals from the Arduino

// UNO to the coil windings of the stator

pinMode(aPin, OUTPUT);

pinMode(bPin, OUTPUT);

pinMode(aPrimePin, OUTPUT);

pinMode(bPrimePin, OUTPUT);

// Start with all coils off

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

}

void loop() {

// Send current to

// 1. The aPin

// 2. The aPin, and the bPin

// 3. The bPin

// 4. Then to the bPin and the aPrimePin

// 5. Then to the aPrimePin

// 6. Then to the aPrimePin and the bPrime Pin

// 7. Then to the bPrimePin

// 8. Then the bPrimePin and the aPin.

// Thus producing steps using the half step method

// 1. Set the aPin High

digitalWrite(aPin, HIGH);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 2. Energize aPin and bPin to HIGH

digitalWrite(aPin, HIGH);

digitalWrite(bPin, HIGH);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 3. Set the bPin to High

digitalWrite(aPin, LOW);

digitalWrite(bPin, HIGH);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 4. Set the bPin and the aPrimePin to HIGH

digitalWrite(aPin, LOW);

digitalWrite(bPin, HIGH);

digitalWrite(aPrimePin, HIGH);

digitalWrite(bPrimePin, LOW);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 5. Set the aPrime Pin to high

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, HIGH);

digitalWrite(bPrimePin, LOW);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 6. Set the aPrimePin and the bPrime Pin to HIGH

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, HIGH);

digitalWrite(bPrimePin, HIGH);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 7. Set the bPrimePin to HIGH

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, HIGH);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 8. Set the bPrimePin and the aPin to HIGH

digitalWrite(aPin, HIGH);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, HIGH);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

}

Step 6: Sketch for Stepper Turning Counter-Clockwise With Half Step Method

For a stepper, it is fairly easy to get the rotor to reverse direction. We do this by simply reversing the order in which the coils are activated. This is shown in the sketch below.

Here we use the same half step approach we did previously, but we reverse the order of the pin assignments, i.e., pin assignments 2, 3, 4, and 5 become pin assignments 5, 4, 3, and 2. The delay between steps is still 3 microseconds, so the motor rotates relatively smoothly. To see the sketch exactly as written, including whitespace, please download the text file.

----------Sketch----------

// Sketch by R. Jordan Kreindler, written September 2016, to rotate

// a stepper motor using the Half Step Method in a counter-clockwise

// direction, i.e., opposite from that in the previous sketch

// To do this, the order of the pin assignments is reversed.

int aPin = 5; //IN1: first end of the blue and yellow stator coil

int bPin = 4; //IN2: first end of the pink and orange stator coil

int aPrimePin = 3; //IN3: second end of the blue and yellow stator coil

int bPrimePin = 2; //IN4: second end of the pink and orange stator coil

// We do not connect IN5, IN6, or IN7

int delay1 = 3;

void setup() {

// Set all pins as output to send output signals from the Arduino

// UNO to the coil windings of the stator

pinMode(aPin, OUTPUT);

pinMode(bPin, OUTPUT);

pinMode(aPrimePin, OUTPUT);

pinMode(bPrimePin, OUTPUT);

// Start with all coils off

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

}

void loop() {

// Send current to

// 1. The aPin

// 2. The aPin, and the bPin

// 3. The bPin

// 4. Then to the bPin and the aPrimePin

// 5. Then to the aPrimePin

// 6. Then to the aPrimePin and the bPrime Pin

// 7. Then to the bPrimePin

// 8. Then the bPrimePin and the aPin.

// Thus producing steps using the half step method

// 1. Set the aPin High

digitalWrite(aPin, HIGH);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 2. Energize aPin and bPin to HIGH

digitalWrite(aPin, HIGH);

digitalWrite(bPin, HIGH);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 3. Set the bPin to High

digitalWrite(aPin, LOW);

digitalWrite(bPin, HIGH);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 4. Set the bPin and the aPrimePin to HIGH

digitalWrite(aPin, LOW);

digitalWrite(bPin, HIGH);

digitalWrite(aPrimePin, HIGH);

digitalWrite(bPrimePin, LOW);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 5. Set the aPrime Pin to high

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, HIGH);

digitalWrite(bPrimePin, LOW);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 6. Set the aPrimePin and the bPrime Pin to HIGH

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, HIGH);

digitalWrite(bPrimePin, HIGH);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 7. Set the bPrimePin to HIGH

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, HIGH);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 8. Set the bPrimePin and the aPin to HIGH

digitalWrite(aPin, HIGH);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, HIGH);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

}

Step 7: Stepper Motor Moving to Specific Angles and Taking Action

The number of steps which a stepper takes allows us to determine the angle rotated by the stepper. For the preceding examples we used a 28BYJ-48 stepper. We noted that this stepper, owing to its gearing and internal teeth takes approximately 2,048 steps to complete one full 360 degree rotation. We can use this information to find the angle through which the rotor of the stepper moves, and count the number of revolutions it makes.

We added a few additional lines of code to our earlier sketch using the Full Step method, and counted the number of steps taken to calculate the angles and revolutions made. The results are displayed on the Serial Monitor. We could just as well have used the Wave Drive method or the Half Step method, but in the later case we would have had to double the number of steps for a full 360 degree, i.e., approximately 6.28319 radians for one revolution.

As we are using a stepper, we do not have an absolute zero position, although we can set the starting position as zero and calculate the angle the stepper turns by keeping track of the number of steps, from that position.

Here we can use the information about degrees per step to move the stepper to specific angles and take some action at these angles. Specifically, here we pause 3 seconds (3,000 ms) after each 90 degrees of turn, and reverse stepper direction after each full 360 degree revolution.

To see the sketch exactly as written, including whitespace, please download the text file.

----------Sketch----------

// Sketch by R. Jordan Kreindler, written September 2016, to rotate a stepper motor,

// pause 3 seconds each 90 degrees of turn, and reverse direction after each full revolution

int aPin = 2; //IN1: first end of the blue and yellow stator coil

int bPin = 3; //IN2: first end of the pink and orange stator coil

int aPrimePin = 4; //IN3: second end of the blue and yellow stator coil

int bPrimePin = 5; //IN4: second end of the pink and orange stator coil

int direction = 0;

// We do not connect IN5, IN6, or IN7

int delay1 = 2; // The delay between each step

int delay2 = 3000; // The delay after each 90 degree turn

int count = 0; // The number of steps

int numberOfRotations = 0; // The number of times the rotor has

// turned 360 degrees

void setup() {

Serial.begin(9600);

Serial.print("Motor started \n");

// Set all pins as output to send output signals from the Arduino

// UNO to the coil windings

pinMode(aPin, OUTPUT);

pinMode(bPin, OUTPUT);

pinMode(aPrimePin, OUTPUT);

pinMode(bPrimePin, OUTPUT);

// Start with all coils off

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

}

void loop() {

// Send current to

// 1. The aPin, and the bPin

// 2. Then to the bPin and the aPrimePin

// 3. Then to the aPrimePin and the bPrime Pin

// 4. Then the bPrimePin and the aPin.

// Thus producing steps with two coils activated at the same time

// to increase torque.

// 1. Energize the blue yellow, and pink orange stator coils

digitalWrite(aPin, HIGH);

digitalWrite(bPin, HIGH);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 2. Energize the pink orange, and in reverse current direction

// the blue yellow stator coil

digitalWrite(aPin, LOW);

digitalWrite(bPin, HIGH);

digitalWrite(aPrimePin, HIGH);

digitalWrite(bPrimePin, LOW);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 3. Energize in reverse current directions the blue yellow and

// pink orange coils

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, HIGH);

digitalWrite(bPrimePin, HIGH);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 4. Energize the blue yellow, and in reverse current direction

// the pink orange coils

digitalWrite(aPin, HIGH);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, HIGH);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

count = count + 4;

Serial.print("Number of steps = ");

Serial.print(count);

Serial.print(" Angle Rotated = ");

Serial.print((count / 2048.00) * 360);

Serial.println(" Degrees");

if (count%512 == 0 ) delay(delay2);

if (count%2048 == 0 ) {

direction = numberOfRotations%2;

numberOfRotations = ++numberOfRotations;

Serial.print("numberOfRotations = ");

Serial.println(numberOfRotations);

Serial.println("\n");

count = 0;

Serial.print("\nDirection = ");

if(direction == 0) {

Serial.println("clockwise");

aPin = 5; //IN1: first end of the blue and yellow stator coil

bPin = 4; //IN2: first end of the pink and orange stator coil

aPrimePin = 3; //IN3: second end of the blue and yellow stator coil

bPrimePin = 2; //IN4: second end of the pink and orange stator coil

}

else {

Serial.println("counter-clockwise");

aPin = 2; //IN1: first end of the blue and yellow stator coil

bPin = 3; //IN2: first end of the pink and orange stator coil

aPrimePin = 4; //IN3: second end of the blue and yellow stator coil

bPrimePin = 5; //IN4: second end of the pink and orange stator coil

}

}

}

Step 8: Working With an Unknown Stepper

If you have a stepper that you do not have the datasheet for, the first step is to take your multimeter, set it to the ohms (resistance) scale, and measure the resistance between all pairs of leads (all pairs of colored wires). I have a number of steppers for which a datasheet is not available. I used one of them, a Nema 17 for this Step. This is the size of stepper commonly found in 3D printers. Larger steppers are more popular for other industrial uses. However, NEMA 17 only refers to the size of the stepper's faceplate, and not all NEMA 17 steppers are created equal. One will substitute, size-wise, for another, although they may not be electrically equivalent.

The resistance values I found, for the NEMA 17 I tested, can be seen in the attached matrix. Note that the matrix is symmetrical around the diagonal, going from the upper left to the lower right, so only part of this matrix is actually needed.

After taking resistance measurements as shown in the attached photographs, we are able to assign colors to coils. For the example here, we see that there are two coils. One we will call A-A’ (Yellow-Blue) with common tap Red, and the other we will call B-B’ (White-Orange) with common tap Black. As can be seen in the attached table, the greatest resistances were found between the Yellow-Blue and White-Orange pairs, other than the open wires. Thus, these were determined to be connected to the two coils present.

Smaller resistance were found for the Red-Yellow and Red-Blue pairs and the Black-white and Black-Orange pairs. Therefore, the common leads were determined to be the Red and Black ones. For a six wire stepper there will always be two groups of three wires that will show a small resistance. The wires with the greatest resistance are the coils, and the lead with these coils end colors, and with somewhat less resistance, are the common leads. The important point is that finding which colors (leads) go with which coils is central to being able to use a stepper.

The resistances found, as well as the number of leads, identifies this as a Unipolar stepper. Steppers come in two varieties: unipolar and bipolar. Unipolar steppers have 5-8 leads, typically 5, 6, or 8. If a center common is used to energize the coils, then clearly the unipolar motor will have less torque, as the magnetic fields will be produced by only half of a full coil. Thus, bipolar motors have more torque, since they use a full coil to produce their magnetic fields. If you come across a stepper with four leads, you have a bipolar two phase stepper.

So the process for dealing with an unknown stepper motor is to first determine the number of leads from the motor. Next take resistance measurements between all pairs of colors, and prepare a matrix of those measurements. In the example above, we had six leads with the colors: Red, Black, White, Yellow, Orange, and Blue. (Note some steppers may have leads that are all the same color, although this is not common. If that is the case, one end should be identified as the anchor point from which all lead resistances are measured.)

We used the leads colors to establish the rows and columns of the matrix. Finally, we filled in this matrix with the resistance measurement of the color pairs to allow the determination of the colors to assign to each coil and any common leads, if present. As previously noted, the pairs that have the greatest resistance are the coils, and the pairs with the lower resistance will, in the case of unipolar steppers have one of their colors in common and that color is a common lead.

This is the information needed to connect an unknown stepper to a stepper driver and an Arduino. In our example, all resistance readings were less than 10 ohms. This is likely to be the case with the steppers you come across, as we are just measuring the resistance of coils of wire. In fact, resistances that you measure in unknown steppers are likely to be considerably less than 10 ohms. However, it is possible to find steppers whose coils have a resistance greater than 10 ohms; although, these resistances are likely to be less than 100 ohms.

We can easily change a unipolar stepper to a bipolar stepper by not using the common wire(s). If we do, we have a four lead bipolar stepper. That is, a six wire stepper is the same as a four wire stepper, if we simply ignore the two common wires. In our example, we would just ignore the Red and Black leads.

Bipolar steppers are typically powered by an H-bridge module, e.g., an L293 or an L298N driver module. The L298N is usually the better choice, as it can handle currents as high as 2 amps (although even this may not be adequate for exceptionally large steppers).

In general it is not wise to try to power a stepper from an Arduino UNO directly as the UNO is likely to be damaged. Remember that when activating a stepper motor, a driver module is used to power the motor, and the stepper coils are not activated directly from the Arduino.

Also, remember that stepper motors are not like the previous motors we discussed. They do not rotate simply by applying the appropriate DC voltage. Instead their coils (leads) must be activated in a specific manner to control their angle of rotation, direction, and speed.

Step 9: L298N-Rotating the NEMA Stepper

The NEMA stepper, used in the previous step, can be rotated using the same Wave Drive approach we used earlier. Here we slightly modified a previous sketch to rotate the stepper a bit faster. A 12v 1.5 Amp wall adapter was used to provide power for the stepper. There was no attempt to power the stepper from the Arduino, as this would likely have damaged the UNO. You may want to connect the ground from the 12v wall adapter not only to the L298N driver module but to an Arduino ground so that you have a common ground. However, as shown here that is not a requirement to get the stepper to rotate.

To see the sketch exactly as written, including the embedded spaces, please download the text file.

----------Sketch----------

// Sketch by R. Jordan Kreindler, written September 2016,

// to rotate a NEMA 17 stepper motor clockwise using the Wave Drive Method

int aPin = 5; //IN1: first end of the blue and yellow stator coil

int bPin = 4; //IN2: first end of the white and orange stator coil

int aPrimePin = 3; //IN3: second end of the blue and yellow stator coil

int bPrimePin = 2; //IN4: second end of the White and orange stator coil

// We do not connect IN5, IN6, or IN7

int delay1 = 10;

void setup() {

// Set all pins as output to send output signals from the Arduino

// UNO to the coil windings of the stator

pinMode(aPin, OUTPUT);

pinMode(bPin, OUTPUT);

pinMode(aPrimePin, OUTPUT);

pinMode(bPrimePin, OUTPUT);

// Start with all coils off

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

}

void loop() {

// Send current to the apin, and then the bpin

// then reverse the current and send reversed current

// to aPrimePin and then bPrimePin.

// 1. Energize the blue yellow stator coil

digitalWrite(aPin, HIGH);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 2. Reverse current direction and energize the blue yellow stator coil

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, HIGH);

digitalWrite(bPrimePin, LOW);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 3. Energize the blue yellow stator coil

digitalWrite(aPin, LOW);

digitalWrite(bPin, HIGH);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 4. Reverse current direction and energize the white orange stator coil

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, HIGH);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

}

With the same configuration, it is easy to reverse the direction of rotation by simply reversing the order of coil activations as we did previously. That is, we simply change pin assignments 5, 4, 3, and 2 to pin assignments 2, 3, 4, and 5 as shown below.

----------Sketch----------

// Sketch by R. Jordan Kreindler, written September 2016,

// to rotate a NEMA stepper motor counter-clockwise using the Wave Drive Method

// The direction of the stepper is reversed by reversing the order of coil activation.

int aPin = 2; //IN1: first end of the blue and yellow stator coil

int bPin = 3; //IN2: first end of the white and orange stator coil

int aPrimePin = 4; //IN3: second end of the blue and yellow stator coil

int bPrimePin = 5; //IN4: second end of the white and orange stator coil

// We do not connect IN5, IN6, or IN7

int delay1 = 10;

void setup() {

// Set all pins as output to send output signals from the Arduino

// UNO to the coil windings of the stator

pinMode(aPin, OUTPUT);

pinMode(bPin, OUTPUT);

pinMode(aPrimePin, OUTPUT);

pinMode(bPrimePin, OUTPUT);

// Start with all coils off

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

}

void loop() {

// Send current to the apin, and then the bpin

// then reverse the current and send reversed current

// to aPrimePin and then bPrimePin.

// 1. Energize the blue yellow stator coil

digitalWrite(aPin, HIGH);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 2. Reverse current direction and energize the blue yellow stator coil

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, HIGH);

digitalWrite(bPrimePin, LOW);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 3. Energize the blue yellow stator coil

digitalWrite(aPin, LOW);

digitalWrite(bPin, HIGH);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 4. Reverse current direction and energize the white orange stator coil

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, HIGH);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

}

Step 10: LEDs to Show Rotation Direction, Pausing Every 180-Degrees

Using essentially the same physical configuration as used in the previous example, but adding two LEDs and slight modifications such as adding an experimental platform with breadboard, we produced the sketch provided below to rotate the NEMA 17 stepper, reverse its direction, show rotation direction with LEDs, and pause at each 180-degrees of rotation.

The NEMA 17 stepper used here has an attached label that specifies it has a step angle of 1.8-degrees. Thus, it takes 360/1.8 = 200 steps to complete one revolution. We use this information to determine when the rotor has gone 180-degrees, after which the rotor is paused for 1 second. We also count steps to determine a complete revolution, after which we change the direction of rotation. In this sketch, we use the Wave Drive method, so the number of steps in a full revolution is 200.

A red LED is used to show clockwise rotation, and a green one to show counter-clockwise rotation. The LEDs are connected to digital pins 6 and 7 on an Arduino UNO, Red to 6 and Green to 7. Two 470 ohm resistors were used in series with each LED. The LEDs, used here, were salvaged from an old smoke alarm, but any Red and Green 5mm or 3mm LEDs will give similar results.

To see the sketch exactly as written, with spaces as included, please download the text file.

----------Sketch----------

// Sketch by R.
Jordan Kreindler, written September 2016, to rotate

// a stepper motor, pause 1 seconds each 180 degrees of turn, reverse

// direction after each full revolution. Turn on red LED if clockwise

// rotation and green LED if counter-clockwise

int aPin = 2; //IN1: first end of the blue and yellow stator coil

int bPin = 3; //IN2: first end of the white and orange stator coil

int aPrimePin = 4; //IN3: second end of the blue and yellow stator coil

int bPrimePin = 5; //IN4: second end of the White and orange stator coil

int redLED = 6; // redLED on if rotor turning clockwise

int greenLED = 7; // greenLED on if rotor turning counter-clockwise

int count = 0; // The number of turns

int delay1 = 15; // Delay between steps

int delay2 = 1000; // Pause at 180 degrees

int direction = 2; // Direction flag: 0 = clockwise; else = counter-clockwise

int numberOfRotations = 0;

// We do not connect IN5, IN6, or IN7

void setup() {

// Set all pins as output to send output signals from the Arduino

// UNO to the coil windings of the stator

pinMode(aPin, OUTPUT);

pinMode(bPin, OUTPUT);

pinMode(aPrimePin, OUTPUT);

pinMode(bPrimePin, OUTPUT);

// Set LED pins for output

pinMode(redLED, OUTPUT);

pinMode(greenLED, OUTPUT);

// Start with all coils off

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

}

void loop() {

direction = numberOfRotations % 2;

if (direction == 1) {

digitalWrite(redLED, LOW);

digitalWrite(greenLED, HIGH);

}

else {

digitalWrite(greenLED, LOW);

digitalWrite(redLED, HIGH);

}

// Send current to the apin, and then the bpin

// then reverse the current and send reversed current

// to aPrimePin and then bPrimePin.

// 1. Energize the blue yellow stator coil

digitalWrite(aPin, HIGH);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 2. Reverse current direction and energize the blue yellow stator coil

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, HIGH);

digitalWrite(bPrimePin, LOW);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 3. Energize the blue yellow stator coil

digitalWrite(aPin, LOW);

digitalWrite(bPin, HIGH);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 4. Reverse current direction and energize the white orange stator coil

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, HIGH);

// Allow some delay between energizing the coils to allow

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

count = count + 4;

if (count == 100) delay(delay2);

if (count == 200) {

delay(delay2);

numberOfRotations = ++numberOfRotations;

count = 0;

if (direction == 0) {

aPin = 5; //IN1: first end of the blue and yellow stator coil

bPin = 4; //IN2: first end of the pink and orange stator coil

aPrimePin = 3; //IN3: second end of the blue and yellow stator coil

bPrimePin = 2; //IN4: second end of the pink and orange stator coil

}

else {

aPin = 2; //IN1: first end of the blue and yellow stator coil

bPin = 3; //IN2: first end of the pink and orange stator coil

aPrimePin = 4; //IN3: second end of the blue and yellow stator coil

bPrimePin = 5; //IN4: second end of the pink and orange stator coil

}

}

}

Step 11: Using a 2-phase 4-wire 7.5 Degree Stepper

The program presented in the Step above can be modified to accommodate almost any stepper. What is required is to know the steps per revolution. Here we use a round stepper, that unlike the preceding square NEMA 17 stepper that we used, has 2-phase and 4-wires. As noted this stepper is round. It is approximately 40mm in diameter, rather than square and 1.7 x 1.7 inches, as was the NEMA stepper.

If we do not know the number of steps in a full revolution we can simply count steps, but this can be a tedious process.

It can be seen that the shape and size of a stepper does not affect the sketch significantly. The new sketch only needs to be changed to account for the number of steps in 180-degrees and in a full 360-degree revolution. These changes depend on the stepper used. The primary assumption is that the current used by the new stepper can be handled by the L298N driver module.

In this example, a stepper with a basic step of 7.5 degrees was used. So, there are 360/7.5 = 48 steps in a full revolution, and 24 steps every 180-degrees. Here the shaft is in the center of the motor so this is likely a motor that is not geared, and thus we would expect the shaft to rotate faster than a stepper that has reduction gearing.

This information is used in the attached program to pause the stepper every 180-degrees, and change direction after each full revolution. Red and green LEDs are again used to identify the direction of rotation.

To see the sketch exactly as written, with spaces as included, please download the text file.

----------Sketch----------

// Sketch by R. Jordan Kreindler, written September 2016, to rotate

// a stepper motor, pause 1 seconds each 180 degrees of turn, reverse

// direction after each full revolution. Turn on red LED if clockwise

// rotation and green LED if counter-clockwise. Here a stepper with

// a step of 7.5 degrees is used.

// One coil is identified as coil-1 and its ends as a and aPrime

// The second coil is identified as coil-2 and its ends as b and Pprime

int aPin = 2; //IN1: first end of coil-1 (coil a) stator coil

int bPin = 3; //IN2: first end of coil-2 (coil b) stator coil

int aPrimePin = 4; //IN3: 2nd end of coil-1 (coil aPrime) stator coil

int bPrimePin = 5; //IN4: 2nd end of coil-2 (coil bPrime) stator coil

// One end of coil a is called a and the other end is called a-prime

int redLED = 6; // redLED on if rotor turning clockwise

int greenLED = 7; // greenLED on if rotor turning counter-clockwise

int count = 0; // The number of turns

int delay1 = 15; // Delay between steps

int delay2 = 1000; // Pause at 180 degrees

int direction = 2; // Direction flag: 0 = clockwise; else = counter-clockwise

int numberOfRotations = 0;

// We do not connect IN5, IN6, or IN7

void setup() {

// Set all pins as output to send output signals from the Arduino

// UNO to the coil windings of the stator

pinMode(aPin, OUTPUT);

pinMode(bPin, OUTPUT);

pinMode(aPrimePin, OUTPUT);

pinMode(bPrimePin, OUTPUT);

// Set LED pins for output

pinMode(redLED, OUTPUT);

pinMode(greenLED, OUTPUT);

// Start with all coils off

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

}

void loop() {

direction = numberOfRotations % 2;

if (direction == 1) {

digitalWrite(redLED, LOW);

digitalWrite(greenLED, HIGH);

}

else {

digitalWrite(greenLED, LOW);

digitalWrite(redLED, HIGH);

}

// Send current to the apin, and then the bpin

// then reverse the current and send reversed current

// to aPrimePin and then bPrimePin.

// 1. Energize the coils

digitalWrite(aPin, HIGH);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

// Allow some delay after energizing the stator coil to give

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 2. Reverse current direction and energize the stator coil

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, HIGH);

digitalWrite(bPrimePin, LOW);

// Allow some delay after energizing the coil to give

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 3. Energize the stator coil

digitalWrite(aPin, LOW);

digitalWrite(bPin, HIGH);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, LOW);

// Allow some delay after energizing the coil to give

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

// 4. Reverse current direction and energize the stator coil

digitalWrite(aPin, LOW);

digitalWrite(bPin, LOW);

digitalWrite(aPrimePin, LOW);

digitalWrite(bPrimePin, HIGH);

// Allow some delay after energizing the coil to give

// the stepper rotor time to respond.

delay(delay1); // So, delay1 microseconds

count = count + 4;

if (count == 24) delay(delay2);

if (count == 48) {

delay(delay2);

numberOfRotations = ++numberOfRotations;

count = 0;

if (direction == 0) {

aPin = 5; //IN1: first end of coil-1 stator coil

bPin = 4; //IN2: first end of coil-2 stator coil

aPrimePin = 3; //IN3: 2nd end of coil-1 stator coil

bPrimePin = 2; //IN4: 2nd end of coil-2 stator coil

}

else {

aPin = 2; //IN1: first end of coil-1 stator coil

bPin = 3; //IN2: first end of coil-2 stator coil

aPrimePin = 4; //IN3: 2nd end of coil-1 stator coil

bPrimePin = 5; //IN4: 2nd end of coil-2 stator coil

}

}

}

Step 12: Afterwards

If you have come to this point congratulations. You should now have a basic understanding of some of the key elements of the stepper motors covered in this Part. I hope you found this Part interesting and informative.

If you liked this, you may want to read Part 2. It can be found at

https://www.instructables.com/id/DC-Motors-Part-23...

It is likely obvious, that this tutorial, even though it is in three parts, just "scratches the surface" of DC motors. Each of the motors covered here could have its own multi-part tutorial, or perhaps an entire textbook. If you have any comments, suggestions, or questions related to this Part of the tutorial, please be kind enough to add them below.

There are many on-line locations to purchase stepper motors and drivers, e.g., Amazon, eBay, Banggood, Newegg, Newark/Element 14, RobotShop, Studica, Sparkfun, AliExpress, etc. The only one I have had any issue with is AliExpress. They do not provide a local phone number to call, and thus a buyer who has a problem must use their chat. Unfortunately, it appears their chat operators are given no authority to solve problems. This is a personal observation, based on personal experience, and should be taken in that context.

If you have any thoughts or questions, related to DC motors in general, but not covered in this tutorial, or any suggestions on how I could improve this or the other Parts of the tutorial, I would be pleased to hear from you. You can contact me at transiintbox@gmail.com. (Please replace the second 'i' with an 'e' to contact me. Thank you.)