Issue with 12V 28BYJ-48 Stepper Motor & Arduino

Hi All,

I am working with a 12V 28BYJ-48 Stepper Motor (32 Steps, 1/16 Gear Ratio) and a ULN2003 Darlington Array and trying to get it to turn to exact positions via the serial monitor user input. My code is as follows:

#define IN1  8
#define IN2  9
#define IN3  10
#define IN4  11
int Steps = 0;
int stepreq = 0;
unsigned long last_time;
unsigned long currentMillis ;
int steps_left = 512;
String rawString = ""; 

void setup() {
  Serial.begin(9600);
  pinMode(IN1, OUTPUT); 
  pinMode(IN2, OUTPUT); 
  pinMode(IN3, OUTPUT); 
  pinMode(IN4, OUTPUT); 
}

void loop() {
  if(Serial.available()) {
    stepreq = Serial.parseInt();
    steps_left = stepreq;
    rawString = Serial.readString();
    Serial.print("Direction read: ");
    if (rawString.endsWith("F") || rawString.endsWith("f")) {
      Serial.println("Clockwise");
    } else if (rawString.endsWith("B") || rawString.endsWith("b")) {
      Serial.println("Counter Clockwise");
    } else {
      Serial.println("String not recognized. Re-enter string.");
      rawString = "";
    }
    Serial.print("Number of steps read: ");
    Serial.println(stepreq, DEC);
  }
  
  while(rawString != "") {
    while(steps_left > 0) {
      currentMillis = micros();
      if(currentMillis-last_time >= 1000){
        stepper(1); 
        last_time=micros();
        steps_left--;
      }
    }
    Serial.println("Motor stopped. Enter a new command");
    Serial.println("");
    delay(2000);
    steps_left=stepreq;
    rawString="";
    }
}

void stepper(int j) {
  for (int x=0; x<j; x++){
    switch(Steps){
       case 0:
         digitalWrite(IN1, HIGH); 
         digitalWrite(IN2, LOW);
         digitalWrite(IN3, LOW);
         digitalWrite(IN4, LOW);
         break; 
       case 1:
         digitalWrite(IN1, HIGH); 
         digitalWrite(IN2, HIGH);
         digitalWrite(IN3, LOW);
         digitalWrite(IN4, LOW);
         break; 
       case 2:
         digitalWrite(IN1, LOW); 
         digitalWrite(IN2, HIGH);
         digitalWrite(IN3, LOW);
         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, LOW);
         digitalWrite(IN3, HIGH);
         digitalWrite(IN4, LOW);
         break; 
       case 5:
         digitalWrite(IN1, LOW); 
         digitalWrite(IN2, LOW);
         digitalWrite(IN3, HIGH);
         digitalWrite(IN4, HIGH);
         break; 
       case 6:
         digitalWrite(IN1, LOW); 
         digitalWrite(IN2, LOW);
         digitalWrite(IN3, LOW);
         digitalWrite(IN4, HIGH);
         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(rawString.endsWith("F") || rawString.endsWith("f")) {Steps++;} // CW
  if(rawString.endsWith("B") || rawString.endsWith("b")) {Steps--;} // CCW
  if(Steps>7) {Steps=0;}
  if(Steps<0) {Steps=7;}
}

I have it wired up with pin 8 going to IN1 of the ULN2003 Darlington Array, pin 9 to IN2, pin 10 to IN3 and pin 11 to IN4. I have the blue cable from the motor going to OUT1, Pink to OUT2, Yellow to OUT3 and Orange to OUT4. This code is a modification of one I found online here: http://www.instructables.com/id/BYJ48-Stepper-Motor/. Right now, this code works but I notice that one, 512 is not a full rotation. Instead, I found it to be around 1000-ish steps. Also, if I enter a number of steps in the serial monitor forwards and backwards, the stepper motor does not return to the exact same position. I appreciate any help!

The step mode you're using doubles the number of steps. You can knock out the cases where two pins are held high at the same time and cut the number of steps in half. This is referred to as "wave drive" but some will refer to it as a "full step", but then others would refer to what you have now as "full step". Suffice it to say that there are a number of step patterns you can use to spin the motor.

These stepper motors are not very accurate so don't expect a lot from them. Great for precisely controlling speed but not great for positioning. There's a lot of YMMV in something this cheap.

Chagrin:
The step mode you're using doubles the number of steps. You can knock out the cases where two pins are held high at the same time and cut the number of steps in half. This is referred to as "wave drive" but some will refer to it as a "full step", but then others would refer to what you have now as "full step". Suffice it to say that there are a number of step patterns you can use to spin the motor.

These stepper motors are not very accurate so don't expect a lot from them. Great for precisely controlling speed but not great for positioning. There's a lot of YMMV in something this cheap.

Thanks Chagrin! This was just the response I was looking for. This leads me to the question, what would be the best way to get accurate position control? I'm looking for a motor that would run on 12V and be relatively cheap.

anujent:
Thanks Chagrin! This was just the response I was looking for. This leads me to the question, what would be the best way to get accurate position control? I'm looking for a motor that would run on 12V and be relatively cheap.

That entirely depends on what you're using it for.