Why do these two methods of controlling a step motor differ in behavior?

When using the Arduino Stepper.h library and objects I am getting terrible performance from my step motor.

I’m using a cheap BYJ-48 stepper motor and an off-brand stepper shield that’s a copy of the Adafruit one. When using the long manual code below it operates at the listed specs and turns smoothly. When attempting to use the Arduino stepper library it turns slower at max speed, jitters while turning, has weaker torque, and feels bad in general. While functional - it seems to be cycling out of sync.

The long more manual control below was taken from a forum but it all makes sense. It’s written to change direction after doing a full rotation but removing those lines has it turning smoothly and continuously like the second example is supposed to.

When examining the stepper object for the second example a lot seems obfuscated away and implementing the examples shown elsewhere for this motor seems to behave a ton worse than the manual version.

How should I be using the stepper library? Is it just a sub par implementation? Am I missing a configuration variable somewhere?

Thanks for any/all your help.

#define IN1  8
#define IN2  9
#define IN3  10
#define IN4  11
int Steps = 0;
boolean Direction = true;// gre
unsigned long last_time;
unsigned long currentMillis;
int steps_left = 4095;
long time;
void setup()
{
	Serial.begin(115200);
	pinMode(IN1, OUTPUT);
	pinMode(IN2, OUTPUT);
	pinMode(IN3, OUTPUT);
	pinMode(IN4, OUTPUT);
	// delay(1000);

}
void loop()
{
	while (steps_left>0) {
		currentMillis = micros();
		if (currentMillis - last_time >= 1000) {
			stepper(1);
			time = time + micros() - last_time;
			last_time = micros();
			steps_left--;
		}
	}
	Serial.println(time);
	Serial.println("Wait...!");
	delay(2000);
	Direction = !Direction;
	steps_left = 4095;
}

void stepper(int xw) {
	for (int x = 0; x<xw; x++) {
		switch (Steps) {
		case 0:
			digitalWrite(IN1, LOW);
			digitalWrite(IN2, LOW);
			digitalWrite(IN3, LOW);
			digitalWrite(IN4, HIGH);
			break;
		case 1:
			digitalWrite(IN1, LOW);
			digitalWrite(IN2, LOW);
			digitalWrite(IN3, HIGH);
			digitalWrite(IN4, HIGH);
			break;
		case 2:
			digitalWrite(IN1, LOW);
			digitalWrite(IN2, LOW);
			digitalWrite(IN3, HIGH);
			digitalWrite(IN4, LOW);
			break;
		case 3:
			digitalWrite(IN1, LOW);
			digitalWrite(IN2, HIGH);
			digitalWrite(IN3, HIGH);
			digitalWrite(IN4, LOW);
			break;
		case 4:
			digitalWrite(IN1, LOW);
			digitalWrite(IN2, HIGH);
			digitalWrite(IN3, LOW);
			digitalWrite(IN4, LOW);
			break;
		case 5:
			digitalWrite(IN1, HIGH);
			digitalWrite(IN2, HIGH);
			digitalWrite(IN3, LOW);
			digitalWrite(IN4, LOW);
			break;
		case 6:
			digitalWrite(IN1, HIGH);
			digitalWrite(IN2, LOW);
			digitalWrite(IN3, LOW);
			digitalWrite(IN4, LOW);
			break;
		case 7:
			digitalWrite(IN1, HIGH);
			digitalWrite(IN2, LOW);
			digitalWrite(IN3, LOW);
			digitalWrite(IN4, HIGH);
			break;
		default:
			digitalWrite(IN1, LOW);
			digitalWrite(IN2, LOW);
			digitalWrite(IN3, LOW);
			digitalWrite(IN4, LOW);
			break;
		}
		SetDirection();
	}
}
void SetDirection() {
	if (Direction == 1) { Steps++; }
	if (Direction == 0) { Steps--; }
	if (Steps>7) { Steps = 0; }
	if (Steps<0) { Steps = 7; }
}
#include <Stepper.h>

const int stepsPerRevolution = 48;  // change this to fit the number of steps per revolution
									// for your motor

									// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
	myStepper.setSpeed(100);
	Serial.begin(9600);
}

void loop() {
	myStepper.step(stepsPerRevolution);
}

Hi,

It sounds like the mapping of stepper wires to driver is wrong, so one code runs badly/incorrectly.

See known working code for that stepper here:

http://arduino-info.wikispaces.com/SmallSteppers

Yep, using the stepper class I had to change from 8-9-10-11 to 8-10-9-11.

How am I supposed to know which pins the object is expecting? Are the first two for the first core and the second two for the other?

Without seeing how they're toggling the 4 discreet wire states it's hard to know without testing until it works. I lowered the speed in the example in your link on the last state and it behaves almost exactly to my manual implementation.

Is there a good way to determine max speed programmatically? If the speed is too high it doesn't appear to move - is there anything I should be aware off when the motor is in this state?

Regardless, thanks for your help! You're article got me on the right track.

The wires go to coils in the motor. It doen't really matter which way they are hooked up (except for direction) as long as they are paired properly. If you take a circuit/continuity tester to the motor leads you'll quickly find which are paired.

All else aside, the maximum speed of any stepper motor is a function of power and impedance. The impedance will determine how quickly the coils charge/discharge and any calculation w.r.t. is only really useful at high speed. Otherwise, the higher the voltage, the greater the speed, more so that any increase in current (which will come into effect under load). If you intend to power the BYJ with 5v from the arduino, I think the maximum you can expect is about 8RPM (or was it 12?), not very fast. Put 24v into a NEMA17 and you can start pushing 1,000RPM. Try myStepper.setSpeed(10); and see how that goes.