#include <Stepper.h>
// The 28BYJ-48 stepper moves in exact steps: 2048 half-steps = exactly one turn.
// That is why printers and CNC machines use steppers, not DC motors.
// The ULN2003 board switches the four coils (an Arduino pin cannot do it alone).
// Press the button to reverse direction. The motor pins go IN1..IN4 -> D8..D11
// in the crossed order the Stepper library expects (8, 10, 9, 11).
Stepper motor(2048, 8, 10, 9, 11);
const int button = 2;
bool dir = 1;
void setup() {
pinMode(button, INPUT_PULLUP);
motor.setSpeed(5); // 5 rpm: about 12 seconds for one full turn
Serial.begin(9600);
Serial.println("One turn = 2048 steps");
}
void loop() {
if (digitalRead(button) == LOW) { dir = !dir; delay(400); }
Serial.println(dir ? "cw" : "ccw");
motor.step(dir ? 512 : -512); // a quarter turn at a time
}