I'm learning how to use a 28BYJ-48 stepper motor with the ULN2003AN driver. After doing a lot reading on how steppers work and the specific sequences in which the coils must be powered on and off in order to operate, I still am confused about how to use the stepper library which comes with the arduino.
For full step instead of half step, the motor wants power to the wires in the following order: BP - PY - YO - OB. I have those pins connects to arduino, via the driver, as follows: B=3, P=4, Y=5, O=6. So the pins need to be powered in the order: 34 - 45 - 56 - 63. The included library (Stepper.h, Stepper.cpp) requires declaring a stepper with pins in the order 3, 5, 4, 6.
The library defines the Stepper using this code:
Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2,
int motor_pin_3, int motor_pin_4)
Then to actually turn the motor, it uses the following code:
switch (thisStep) {
case 0: // 1010
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, LOW);
digitalWrite(motor_pin_3, HIGH);
digitalWrite(motor_pin_4, LOW);
break;
case 1: // 0110
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, HIGH);
digitalWrite(motor_pin_3, HIGH);
digitalWrite(motor_pin_4, LOW);
break;
case 2: //0101
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, HIGH);
digitalWrite(motor_pin_3, LOW);
digitalWrite(motor_pin_4, HIGH);
break;
case 3: //1001
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, LOW);
digitalWrite(motor_pin_3, LOW);
digitalWrite(motor_pin_4, HIGH);
break;
}
I don't understand why it sets pin1 and pin3 to HIGH at the first step and not pin1 and pin2. As far as I can tell, this is what causes the need to call the function with
Stepper(num_steps, 3, 5, 4, 6)
instead of
Stepper(num_steps, 3, 4, 5, 6)
Is there an advantage to doing it this way? I would write it to be
switch (thisStep) {
case 0: // 1100
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, HIGH);
digitalWrite(motor_pin_3, LOW);
digitalWrite(motor_pin_4, LOW);
break;
case 1: // 0110
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, HIGH);
digitalWrite(motor_pin_3, HIGH);
digitalWrite(motor_pin_4, LOW);
break;
case 2: // 0011
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, LOW);
digitalWrite(motor_pin_3, HIGH);
digitalWrite(motor_pin_4, HIGH);
break;
case 3: // 1001
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, LOW);
digitalWrite(motor_pin_3, LOW);
digitalWrite(motor_pin_4, HIGH);
break;
}
This would allow the pins to be entered as
Stepper(num_steps, 3, 4, 5, 6)
But then I'm new to electronics and programing with controllers, so maybe I am not aware of something here?