// ULN2003 stepper driver - one step or forward and reverse.
#include <Stepper.h>
#define IN1 10 // input pins for ULN2003 driver with BYJ28-48 stepper
#define IN2 8
#define IN3 6
#define IN4 4
#define ledFWD 2 // forward LED
#define ledREV 12 // reverse LED
byte pinOut[] = {2, 4, 6, 8, 10, 12}; // output pins
int stepDir = 1; // motor direction 1 (FWD) or -1 (REV)
int stepCount = 0; // counter
int stepsPerRevolution = 2048; // motor specific
Stepper myStepper(stepsPerRevolution, IN1, IN3, IN2, IN4); // Create stepper object, windings 1, 3, 2, 4
unsigned long timer, interval = 250;// timer for onestep()
void setup() {
Serial.begin(115200);
for (int i = 0; i < 6; i++)
pinMode (i, OUTPUT);
digitalWrite(ledFWD, HIGH);
digitalWrite(ledREV, LOW);
}
void loop() {
if (millis() - timer > interval) { // wait for interval
timer = millis(); // reset timer
// onestep(); // advance motor one step
freerun();
}
}
void onestep() {
myStepper.step(1); // advance motor one step
}
void freerun() {
if (millis() < interval + 50) Serial.print("FWD | "); // print once
Serial.print(stepCount % 4); // four steps of ULN2003
myStepper.step(1 * stepDir); // "steps" and "direction"
stepCount++;
if (!(stepCount % 20)) { // at 20 steps
Serial.println(); // format serial output
stepCount = 0; // reset step count
stepDir = -stepDir; // change motor direction
if (stepDir > 0) {
Serial.print("FWD | ");
digitalWrite(ledFWD, HIGH); // indicate direction
digitalWrite(ledREV, LOW); // indicate direction
} else {
Serial.print("REV | ");
digitalWrite(ledFWD, LOW); // indicate direction
digitalWrite(ledREV, HIGH); // indicate direction
}
}
}
IN1
IN2
IN3
IN4
REV
FWD
A
B
C
D
ULN2003