Python Exemplary |
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 |
Aim: Circuitry: 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: def forwardStep(): 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. Program:[►] # Stepper1.py import RPi.GPIO as GPIO import time P_A1 = 8 # adapt to your wiring P_A2 = 10 # ditto P_B1 = 11 # ditto P_B2 = 13 # ditto delay = 0.005 # time to settle def setup(): GPIO.setmode(GPIO.BOARD) GPIO.setup(P_A1, GPIO.OUT) GPIO.setup(P_A2, GPIO.OUT) GPIO.setup(P_B1, GPIO.OUT) GPIO.setup(P_B2, GPIO.OUT) def forwardStep(): setStepper(1, 0, 1, 0) setStepper(0, 1, 1, 0) setStepper(0, 1, 0, 1) setStepper(1, 0, 0, 1) def backwardStep(): setStepper(1, 0, 0, 1) setStepper(0, 1, 0, 1) setStepper(0, 1, 1, 0) setStepper(1, 0, 1, 0) def setStepper(in1, in2, in3, in4): GPIO.output(P_A1, in1) GPIO.output(P_A2, in2) GPIO.output(P_B1, in3) GPIO.output(P_B2, in4) time.sleep(delay) setup() # 512 steps for 360 degrees, adapt to your motor while True: print "forward" for i in range(256): forwardStep() print "backward" for i in range(256): backwardStep() Remarks: |