BareBones stepper homing with basic Stepper.H and 28BYJ motor

I was about to give up and ask for help, then all the sudden I got it to work.

Then I thought surely I'm not the only noob struggling with this, and I did in fact do alot of forum searching, and I did find alot of stuff, but some of it didn't apply to my situation, and the rest of it was either over my head, or for whatever other reason, I couldn't get it to work.
So while I know this is re-posting old news i'm sure, I wanted to share my results, and if it helps even one person that was about to give up and go do something easier, like competitive eating, I'll feel good about it.

The goal:
Using the 28BYJ-48 stepper motor and ULN2003 stepper motor driver that comes with every basic arduino starter kit above 25$, a breadboard, a 6.5-9v power module, and an Arduino Uno R3, use the absolute BASIC Stepper.h library and a cheap NC-NO limit switch to make a stepper motor move to a specified physical point, stop, back off, and stop again.
Just that, no more. But in the process, learn how this stuff works without having to buy a bunch of expensive hardware.

The setup

The white thing on the far left is the rail and rack setup I want to eventually use for this whole thing. It's nothing fancy, something I whipped up and 3d printed. If you want the files of that I can make them available.
A word on the switch, there's info available with a quick google, but it's not complicated. There are three prongs, A common or 'ground' a NO for Normally Open, and NC for Normally Closed. I have mine connected by a black wire from the common on the switch going to one of the grounds on the arduino. A red wire connects the Normally Open prong to one of the digital inputs on the arduino. I chose 3 but you can do whichever you want, it just has to match to what you declare in the sketch for the 'homeSwitch_A_pin' variable.

The Code:

//For Homing cycle with switch
//On start up motor runs until switch is closed

#include <Stepper.h>

int homeSwtichA_Pin = 3;
int homeSwtichA_State = 0;

const int stepsPerRevolution = 2048;  

//int buttonState = 0;

//8 to IN1
//9 to IN2
//10 to IN3
//11 to IN4

Stepper Stepper_A(stepsPerRevolution, 8, 10, 9, 11);

void setup()
{
    delay(1000);
  Serial.begin(9600);
  Stepper_A.setSpeed(5); 
  pinMode(homeSwtichA_Pin, INPUT_PULLUP); //DECLARE MODE OF SWITCH
  homeSwtichA_State = digitalRead(homeSwtichA_Pin); //INITIAL UPDATE FOR STATUS OF SWITCH AND SWITCH VARIABLE
  do { 
    Stepper_A.step(128);       //COMPLETE PART OF FULL REVOLUTION DEPENDING ON YOUR SETUP, MINE IS 2048 STEPS FOR A FULL ROTATION
                              //SO AS 2048 DIVIDED BY 16 = 128 THIS IS 'THEORETICALLY' 1/16TH OF A ROTATION, OR A 22.5 DEGREE SWEEP
    homeSwtichA_State = digitalRead(homeSwtichA_Pin); //UPDATE THE STATUS OF THE SWITCH AND SWITCH VARIABLE
    Serial.println("HOMING");
  } while (homeSwtichA_State == HIGH); //RUNS THE CODE IN THE BRACKETS UNLESS THE SWITCH IS PRESSED, THEN IT STOPS
  Stepper_A.step(-128); //BACK OFF THE SWITCH, A WEE BIT.
  //stepper can now be considered to be 'homed'
  Serial.println("HOMED");
}

void loop()
{

}

Some things I learned:
This has been talked about other places, but I'll mention it here too:
The wiring between the motor driver and the Arduino is kind of goofy. Pick which ever pins you want, but when your setting up the stepper, you have to tell it to go in the order of IN1, IN3, IN2, IN4. Also, in an earlier post mentioned how I struggled getting anything to work until I relised the little chip on the driver board wasn't physically installed correctly, so you have to be on the lookout for stuff like that too.

Don't make the stepper take too big of steps, it won't do anything while it's taking the steps, including checking the state of the switch.

If your code isn't behaving as expected, it could be due to being stuck in a loop somewhere. You can check on this by throwing Serial.Println() codes in there, until you get it lined out.

You should use a variable to hold the information regarding the state of the switch, rather than trying to have the code read the switch itself. This is handled with 'homeSwitchA_State'

You will have to call this variable more than once, and in the right places, or you might get stuck in a loop, or it just won't work.

You need to keep your speed to 15 or less. I used 5 which was plenty. Higher values may or may not do truly terrible things, but the motor probably won't spin at all, making you think you have problems with your code.

Bear in mind that Stepper.h doesn't have a "home position" reference function. This is just to use if you want to test that your setup would theoretically work with a limit switch.

I went down the rabbit hole of AccelStepper for a while, but couldn't ever get me and it on the same page.

I recommend starting here with the basic library, then slowly and saving new files as you go, converting over to a more advance stepper library. That's what I'm going to do, but after I take a break for awhile, I fought with this every spare moment for 2 weeks just because I knew it didn't have to be complicated.

Click the switch, motor stops. That's all I wanted.

So, I hope it bails out somebody else, and good luck, god bless.

You will want to replace the 9vdc/PP3 battery with a power supply sufficient for the motor's current, for example, a laptop power supply.

Thanks for sharing! follow @xfpd suggestion that battery will cost a lot in a short time because they do not support the load you are putting on it. Also they are not inexpensive at least where I live. With the money you save you can purchase a multi-meter for future experiments.

I wasn't sure wether to post this here, or make a new post. But I'm trying to graduate myself to using the AccelStepper library so I can utilize the setCurrentPosition option.

But here's the problem.

WIthout changing anything to the hardware setup, the new code that I made for using AccelStepper doesn't work as expected.

In the original code using the basic stepper.h library, the motor moved towards the switch, when the switch was clicked, the controller exited the do_while loop and moved to the next line, which was to reverse direction of the motor an arbitrary amount of steps, and that was it. And it would do that.

WIth the new code using AccelStepper, the motor moves towards the switch, then if the switch is clicked the motor stops, but then it just sits there. It won't move on and exectue the next line of code.

//this worked with the basic Stepper.h library
//Now to see how it does with accelstepper.h library

#include <AccelStepper.h>

#define HALFSTEP 8

#define motorPin1  8     
#define motorPin2  9     
#define motorPin3  10    
#define motorPin4  11

int homeSwtichA_Pin = 3;
int homeSwtichA_State = 0;

const int stepsPerRevolution = 2048;  

//8 to IN1
//9 to IN2
//10 to IN3
//11 to IN4

AccelStepper Stepper_A(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);


void setup()
{
  delay(1000);
  Serial.begin(9600);
  Stepper_A.setMaxSpeed(1000);
  Stepper_A.setAcceleration(75);
  Stepper_A.setSpeed(500); 
  pinMode(homeSwtichA_Pin, INPUT_PULLUP); //DECLARE MODE OF SWITCH
  homeSwtichA_State = digitalRead(homeSwtichA_Pin); //INITIAL UPDATE FOR STATUS OF SWITCH AND SWITCH VARIABLE
  do { 
    Stepper_A.move(128);       //COMPLETE PART OF FULL REVOLUTION DEPENDING ON YOUR SETUP, MINE IS 2048 STEPS FOR A FULL ROTATION
    Stepper_A.runSpeed();                          //SO AS 2048 DIVIDED BY 16 = 128 THIS IS 'THEORETICALLY' 1/16TH OF A ROTATION, OR A 22.5 DEGREE SWEEP
    homeSwtichA_State = digitalRead(homeSwtichA_Pin); //UPDATE THE STATUS OF THE SWITCH AND SWITCH VARIABLE
    Serial.println("HOMING");
  } while (homeSwtichA_State == HIGH); //RUNS THE CODE IN THE BRACKETS UNLESS THE SWITCH IS PRESSED, THEN IT STOPS
  Stepper_A.move(-128); // gets stuck here seems like  //BACK OFF THE SWITCH, A WEE BIT.
  Stepper_A.runSpeed();  //stepper can now be considered to be 'homed'
  //Stepper_A.setCurrentPosition(); wait on this till we see how the rest behaves
  Serial.println("HOMED");

}

void loop()
{

}

I tried changing the reversing line, Stepper_A.move(-128) to a bigger step, -4096, but that didn't help. And the serial.println("HOMED") never executes either.

My next move is to put the reverse code in a do_while loop as well with the while_statement being contingent on the switch being 'LOW' but I don't understand why that's nessecary in AccelStepper.H but not Stepper.H and I sure would like to. Understand, that is.

So yeah, the Do_While loop is nessecary or the program hangs once the switch is clicked. Don't fully understand why, but it is working, so partial win.

Here's the updated code.

//this worked with the basic Stepper.h library
//And now it works with accelstepper.h library

#include <AccelStepper.h>

#define HALFSTEP 8

#define motorPin1  8     
#define motorPin2  9     
#define motorPin3  10    
#define motorPin4  11

int homeSwtichA_Pin = 3;
int homeSwtichA_State = 0;

const int stepsPerRevolution = 2048;  

//8 to IN1
//9 to IN2
//10 to IN3
//11 to IN4

AccelStepper Stepper_A(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);


void setup()
{
  delay(1000);
  Serial.begin(9600);
  Stepper_A.setMaxSpeed(1000);
  Stepper_A.setAcceleration(75);
  Stepper_A.setSpeed(500); 
  pinMode(homeSwtichA_Pin, INPUT_PULLUP); //DECLARE MODE OF SWITCH
  homeSwtichA_State = digitalRead(homeSwtichA_Pin); //INITIAL UPDATE FOR STATUS OF SWITCH AND SWITCH VARIABLE
  do { 
    Stepper_A.move(128);       //COMPLETE PART OF FULL REVOLUTION DEPENDING ON YOUR SETUP, MINE IS 2048 STEPS FOR A FULL ROTATION
    Stepper_A.runSpeed();                          //SO AS 2048 DIVIDED BY 16 = 128 THIS IS 'THEORETICALLY' 1/16TH OF A ROTATION, OR A 22.5 DEGREE SWEEP
    homeSwtichA_State = digitalRead(homeSwtichA_Pin); //UPDATE THE STATUS OF THE SWITCH AND SWITCH VARIABLE
    Serial.println("HOMING");
    Serial.println(Stepper_A.currentPosition());
  } while (homeSwtichA_State == HIGH); //RUNS THE CODE IN THE BRACKETS UNLESS THE SWITCH IS PRESSED, THEN IT STOPS
  do {
    Stepper_A.move(-128);  //BACK OFF THE SWITCH, A WEE BIT.
    Stepper_A.runSpeed();
    homeSwtichA_State = digitalRead(homeSwtichA_Pin);
  } while (homeSwtichA_State == LOW);
  //Stepper_A.setCurrentPosition(); 
  Serial.println(Stepper_A.currentPosition());
  Serial.println("HOMED");
  
}

void loop()
{

}


That's because AccelStepper doesn't create any step-pulse without calling run() or runSpeed(). (apart from the blocking functions).

It maybe much more easy when using the MoToStepper class of the MobaTools library:

//this worked with the basic Stepper.h library
//And now it works with accelstepper.h library
// And now with MobaTools library ;-)

#include <MobaTools.h>


const byte motorPin1 = 8;
const byte motorPin2 = 9;
const byte motorPin3 = 10;
const byte motorPin4 = 11;

const byte homeSwtichA_Pin = 3;

const int stepsPerRevolution = 4096;  // steps per rev with HALFSTEP

//8 to IN1
//9 to IN2
//10 to IN3
//11 to IN4

MoToStepper Stepper_A(stepsPerRevolution, HALFSTEP);


void setup()
{
    delay(1000);
    Serial.begin(9600);
    Stepper_A.attach( motorPin1, motorPin2, motorPin3, motorPin4);
    Stepper_A.setSpeed(100);                        // Speed as rpm*10
    Stepper_A.setRampLen(stepsPerRevolution/10);    // 1/10 rev from standstill to speed
    pinMode(homeSwtichA_Pin, INPUT_PULLUP);         //DECLARE MODE OF SWITCH
    Stepper_A.rotate(1);                            // stepper moves toward the switch
    Serial.println("START HOMING");
    while (digitalRead(homeSwtichA_Pin) == HIGH);   // wait till home switch is activated
    Stepper_A.setSpeed(20);                         // lower Speed
    Stepper_A.rotate(-1);                           // stepper moves slowely back
    while (digitalRead(homeSwtichA_Pin) == LOW);    // wait till home switch is released
    Stepper_A.stop();                               //  stop the stepper immediately
    Stepper_A.setZero();                            // actual position as new zero-point
    Serial.println(Stepper_A.currentPosition());
    Serial.println("HOMED");

}

void loop()
{

}

Your use of Accelstepper is not as it is intended and is not correct. It works more or less by chance - so should not be an example for others. Try and error is not always a good idea :wink:

[Edit] .move() and .rundSpeed() don't fit together. .runSpeed() steps the stepper according to .setSpeed() without regarding any target position. And it doesn't do acceleration/deceleration (just like stepper.h). And .runSpeed() creates only one step at most when called. So .runSpeed() must be called more often than the needed steprate. Your prints in the while loop() with every loop() cycle slows down the steprate a lot.
If you want to use runSpeed(), don't call .move(), but set the speed ( with .setSpee() ) accordingly to move in one direction or the other. Even worse: .move() may also change the speed.