A stepper is four coils and a table
The 28BYJ-48 and its ULN2003 board are the first motor most people meet that goes to a position rather than at a speed.
There is less to the driver than anybody expects: a table of eight patterns and a delay.
A DC motor has a speed. Put volts across it and it turns, take them away and it stops somewhere. A stepper has neither: it has four coils, and whichever ones are switched on decide where the rotor stands. Switch them in order and it walks round; stop, and it stays exactly where it was left, holding.
The pair
They are sold together and this part models them together, because it is one product: the motor's five-way plug fits nothing else, nobody buys either alone, and a driver board with nothing on it would have nothing to show.
The motor is unipolar. The red wire is the common and goes to +5 V; each of the other four is one coil's far end. A Darlington channel on the green board pulls that end down, current flows, and that coil becomes a magnet. The rotor lines up with whichever magnet, or pair of magnets, is on.
Each Darlington drops about a volt when it is conducting, so on a 5 V rail a 50 ohm coil sees about 4 V and draws about 80 mA. That volt is the reason a 28BYJ-48 is weaker on 5 V than the arithmetic suggests.
The table
The part is not told what "mode" a sketch is using. It works out where the field is pointing from the four coils and moves the rotor there, and every sequence anybody writes falls out of that one table:
pattern IN4..IN1 |
field, in half-steps |
|---|---|
0001 |
0 |
0011 |
1 |
0010 |
2 |
0110 |
3 |
0100 |
4 |
1100 |
5 |
1000 |
6 |
1001 |
7 |
- Half-step walks the eight rows one at a time: one half-step per change.
- Wave drive, one coil at a time (
0001,0010,0100,1000), jumps two rows: a whole step, with one coil's worth of torque. - Two-phase-on full step (
0011,0110,1100,1001) also jumps two rows, with two coils on and twice the torque. It is what a sketch should use when it needs to move something.
Where 4096 comes from
The datasheet's heading is "stride angle 5.625 degrees / 64". The rotor turns 5.625 degrees per half-step (sixty-four half-steps to its own revolution), and the gear train divides that by 64 again:
output per half-step = 5.625 / 64 = 0.087890625 degrees
half-steps per turn = 64 * 64 = 4096
full steps per turn = 2048
which is why every 28BYJ-48 sketch has a 4096 or a 2048 in it. A single half-step moves the shaft about a twelfth of a degree, which is why the part draws a mark on it.
One deviation, on purpose. The real gearbox is 63.68395:1 rather than a round 64, so a real motor is about half a percent out over a revolution and 4096 steps leaves it a degree and a half short. This model uses the datasheet's 64, so it comes back exactly to where it started. It is the one place this part is kinder than the bench.
Where it stalls
The rotor goes the short way around to the new field. Four rows away there is no
short way (the field has been reversed and the rotor is pulled equally hard
both ways), so it does not move at all. That is the stall, and it is what a
sketch that writes 0001 then 0100 gets: a motor that buzzes and stays put.
It is also what the commonest wiring mistake on this board produces. The motor's plug does not present its four wires in the order the pin header does, so swapping two of them is easy, and the result is a perfectly good sequence arriving in an order the rotor cannot follow. A logic analyzer on the four lines shows a textbook waveform while the shaft sits still, which is exactly the point of looking.
With every coil off there is no field and nothing holds the rotor: the shaft stays where it was left and a real one can be turned by hand. That is what writing all four pins low at the end of a move is for, and it is why a battery lasts longer for it.
How fast it will go
The datasheet gives an idle out-traction frequency of more than 1000 Hz. So a step asked for less than a millisecond after the last one is a step the rotor cannot make: it is missed, and the motor ends up somewhere the sketch does not think it is.
That is the whole of why a stepper driven too fast whines and goes nowhere, and it is much better met on a simulator than on a robot that has already driven off the table. The part models it: change the field faster than that and the shaft takes one step and then loses every one after it.
The Stepper and AccelStepper libraries
Both are built in, under their usual names, so a kit sketch compiles as it stands.
#include <Stepper.h>
Stepper myStepper(2048, 8, 10, 9, 11); // IN1, IN3, IN2, IN4
void setup() { myStepper.setSpeed(10); } // rpm
void loop() { myStepper.step(2048); delay(500); }
The pins go in as 8, 10, 9, 11, not 8, 9, 10, 11. The library's sequence is written for a motor on two H bridges, and that order is what makes it walk the ULN2003's four coils round in turn. In header order it energizes opposite coils, and the motor hums and stays put, here as on the bench.
AccelStepper does the same with a speed ramp, which is what a real mechanism wants:
#include <AccelStepper.h>
AccelStepper plate(AccelStepper::HALF4WIRE, 8, 10, 9, 11);
void setup() {
plate.setMaxSpeed(800); // half steps a second
plate.setAcceleration(400);
plate.moveTo(4096); // one turn
}
void loop() { plate.run(); } // one step at most, as often as possible
run() makes at most one step a call, so a delay() in loop() makes the
motor stutter. HALF4WIRE is 4096 steps a turn and FULL4WIRE 2048.
It does not know where it is
Nothing reads the shaft. A sketch counts the steps it has sent and assumes the motor made them, which is true until it is not: a stall, a jam, or a rate past the ceiling. That is why every real positioning system has a limit switch to home against, and why a stepper with no feedback is an open loop.
What is not modeled
The load on the supply. Four coils at 80 mA each is 320 mA in half-step, and this part does not draw it from the rail, so the "never run a stepper off the board's 5 V pin" lesson cannot be taught here. Mokxi's rail is ideal and would not sag anyway.
Coil inductance, so there is no current rise time and no reason for the torque to fall off with speed beyond the flat rate ceiling above. Detent and holding torque, and any mechanical load: nothing can be hung on the shaft. Microstepping, which a ULN2003 cannot do at all: it is a switch, not a current source. And the gearbox's backlash, which on a real one is a degree or so and is why these are no good for anything that has to come back to a mark from both directions.
The circuit to open
Stepper dial on the analyzer: a knob on A0, the shaft following it, and a
four-channel logic analyzer across the four coil lines so the sequence can be
read as it walks.
Stepper turntable: a photo turntable that turns a full circle in eight stops with AccelStepper, flashing a camera LED at each one.