Getting a stepper Motor to Home and then run through a cycle 10,000 times

Hi Folks,

I’m trying to get a stepper motor to home by hitting a limit switch and then run through a loop 10,000 times.

I have yet to add the 10,000 cycle code but I have looked around and found this way of activating the limit switch.

I have managed to get the stepper to rotate until it hits the limit switch but once it does it just goes through the rest of the loop without remembering where its home was, (it will try to go past the switch and judder) Could someone point out where I am going wrong I cant find anything on adding a home switch to steppers anywhere!

Here is the code!

 const int  stepPin = 7;
const int dirPin = 6;
const int enPin = 5;
const int limitPin = 11;

int direction;
int steps;




void setup() {



  pinMode (limitPin, INPUT_PULLUP);
  pinMode (stepPin, OUTPUT);
  pinMode (dirPin, OUTPUT);

  pinMode (enPin, OUTPUT);
  digitalWrite (enPin, LOW);




  while (digitalRead(limitPin)) {  // Do this until the switch is activated
    digitalWrite(dirPin, HIGH);      // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(stepPin, HIGH);
    delay(5);                       // Delay to slow down speed of Stepper
    digitalWrite(stepPin, LOW);
    delay(5);
  }



  while (!digitalRead(limitPin)) { // Do this until the switch is not activated
    digitalWrite(dirPin, LOW);
    digitalWrite(stepPin, HIGH);
    delay(10);                       // More delay to slow even more while moving away from switch
    digitalWrite(stepPin, LOW);
    delay(10);
  }

  steps = 0; // Reset position variable to zero

}

void loop() {



  digitalWrite(dirPin, HIGH);
  for (int x = 0; x < 900; x++) {
    digitalWrite (stepPin, LOW);
    delayMicroseconds (500);
    digitalWrite (stepPin, HIGH);
    delayMicroseconds (500);
  }
  delay(1000);



  {

  }

  digitalWrite(dirPin, LOW);
  for (int x = 0; x < 800; x++) {
    digitalWrite (stepPin, HIGH);
    delayMicroseconds (500);
    digitalWrite (stepPin, LOW);
    delayMicroseconds (500);

  }
  delay(1000);




}

the equipment I am using is:

Arduino UNO.
TB6600 Motor driver
Nema 23 stepper motor
Limit switch

Any Help will be appreciated and please go easy on me, I have only been coding for a few days now!

it just goes through the rest of the loop without remembering where its home was

Can you point out where, in loop(), you care about the value in steps (where its home is)?

Why do you rotate 900 steps one way and then 800 steps the other way?

I see my problem! ( I think) I haven’t defined it in the Loop so I need to add code there that tells it where to start?

Also, im still messing around with the distances as I need it to add a mechanism and I haven’t got it attached at the moment so I hadn’t changed the steps to match just yet but i will change them to match when i have the whole thing assembled!

would i use an

if (steps > 0) {

in the loop to make this work?

Thank you

but i will change them to match when i have the whole thing assembled

Regardless of what is eventually attached, don't you want to move away some amount and back THE SAME amount?

would i use an

if (steps > 0) {

in the loop to make this work?

Since you explicitly set steps to 0 in setup(), the if statement would evaluate to false. Maybe that's what you want, but I doubt it.

What kind of stepper and driver are you using? Why are you not using the Stepper library?

It seems to me that you should rename steps to homePosition. You can assign it a value of 0.

Then, you'd have another variable that you'd increment whenever you stepped in the plus direction (for whichever direction you define as the plus direction) and that you'd decrement whenever you stepped in the other direction.

Before stepping, you'd compare the value in that variable to homePosition. Don't step in the negative direction if the to be position is less than homePosition. You can, of course, step in the positive direction as often as you want (until you run into something in the positive direction... :slight_smile: ).

I'm using a NEMA 23 Stepper with a TB6600 Driver, I am unsure of which library to use as there doesn't seem to be anything for this driver on there!

I’m using a NEMA 23 Stepper

But what color is it?

with a TB6600 Driver

That link didn’t work. It appears that it has a direction pin and a step pin. The Stepper library can handle drivers like that.

Maybe 500 microseconds is not enough time for the command pulse.

It would be better to write this code

  digitalWrite(dirPin, HIGH);
  for (int x = 0; x < 900; x++) {
    digitalWrite (stepPin, LOW);
    delayMicroseconds (500);
    digitalWrite (stepPin, HIGH);
    delayMicroseconds (500);
  }

like this

  digitalWrite(dirPin, HIGH);
  for (int x = 0; x < 900; x++) {
    digitalWrite (stepPin, LOW);
    delayMicroseconds (10);
    digitalWrite (stepPin, HIGH);
    delayMicroseconds (1000);
  }

where the small number is the duration of a step pulse and large number is the time interval between steps. Then you only need to vary one number to change the speed.

…R
Stepper Motor Basics
Simple Stepper Code

also look up the AccelStepper library

Since you’re flying the motor around all the time...

Move the limitPin test to inside your loop, then if any steps are lost or skipped, the motion will be recalibrated ‘on the run’ to a known zero point.

Just because a movement starts, you can rarely guarantee the step count will always match the reality if the mechanical load varies.