|Python Exemplary - RPi Tutorial|
The source code of all examples can be downloaded from here.
How stepper motors work
The rotor of a stepper motor is like a compass needle, one side is the north, the other side is the south pole. The north pole of a magnetic needle turns in the direction of a magnetic field H like the compass needle points to the north in the earth magnetic field.
The two coils A and B have both a center tap connected to VCC. Since the direction of the magnetic field is a right-hand thread of the current in the windings, the direction of the magnetic field can be switched by pulling to ground (GND) one or the other side of each coil. (Current flows from VCC to GND, see red arrows).
To turn the rotor, the resulting field is rotated in 90 degrees steps by selecting the right sequence of coil currents. As you can see in the diagrams below, to create a counter-clockwise rotation, the 4 inputs A1, A2, B2, B2 must be connected as follows:
Blue arrows: Magnetic field of each coil (alongside the magnet arms).
Experiment 1: Stepper motor connected via a ULN2003A driver
If you compare the circuit to the table above, you find the following correspondence:
0 = LOW, 1 = HIGH
If setStepper(in1, in2, in3, in4) is a function that applies the logic values to the driver inputs, a full counter-clockwise rotation of the rotor can be obtained by calling the sequence:
Normally there is a gear that links the rotor axis and the motor shaft. So the number of forwardStep() to turn the shaft for a given angle depends on the reduction ratio of the gear. In this example, 512 calls are needed for a 360 degrees shaft rotation.
Highlight program code