Stuck Getting a stepper to home

Hi I hope I'm in the right forum/category. apologies if not.

I am working on a project for my local railway club to make a stepper motor driven turntable controlled by an Arduino.
the motor when powered up needs to auto home to the initial straight through track, then have the ability to turn a given point in this example 180' left of home when a button is pressed or 90' left of home if a different button is pressed.
i am assuming the motor needs to find a starting point point each time it is turned on to then go back to the home position ready for the first button press.
I can control my stepper left and right and get to cycle left or right each time an optical sensor is triggered, but i have no idea how to get it move to the reference point then count back to the home position for start of working.

can anyone help me get started please?
stepper motor uses pins 8,10,9,11 in that order and has a full revolution of 2048 steps and my sensor is on analogue pin 3
cheers Kevin

You tell the motor to step, so count steps from home. Reverse that many steps and you are back home.

To determine where home is upon startup, just step until the home sensor is activated and call that position zero. Keeping track from zero works if your mechanical design does not overload the motor, so it does not skip steps.

On the other hand, it does not damage a stepper to stall it (although if it is HIGHLY geared down, you might damage the gears or output mechanism), so in most cases, you don't even need a home switch. Just give it enough steps at startup to run the motor into an end stop, and call that zero.

This question was raised a short while ago and is still active.
Personally I recommend a DC motor and some detectors for the tracks.

While home & counting steps is fine, the extra cost to put a small magnet (sensor point) at each position index is negligible.

Once the turntable is homed, you can be sure of positioning, AND recalibrate the known current location every time the turntable passes any index point…

—think hard-sectored disks vs soft-sectored ! :clown_face:

Cheers all, I've got the code below that starts the stepper running, waits for the optical sensor to be triggered then stops the motor. (home) (sets int posHome as 0)
then i have asked it to go to pos 512 from the home when the button is pressed.
the issue i have is there is an error somewhere as i get a missing if when using the esle if statement.
any advice greatly appreciated, cheers.

#include <Stepper.h>
int motSpeed=3;
int dt=500;
int stepsPerRevolution = 2048;  //number of steps per revolution
// for your motor
int buttonPin=2;
int motDir=1;
int buttonValNew;
int buttonValOld=1;
int sensorPin=3;
int posHome;

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

void setup() {
  Serial.begin(9600);
  myStepper.setSpeed(motSpeed);
  pinMode(buttonPin,INPUT);
  digitalWrite(buttonPin,HIGH);
  pinMode(3,INPUT);
}

void loop() {
  buttonValNew=digitalRead(sensorPin);
if (buttonValOld==1 && buttonValNew==0) {
    myStepper.setSpeed(0);
    posHome=0;
}
else if (buttonPin==0); {
  myStepper.step(512);
}
else {
  myStepper.step(motDir*1);
  buttonValOld=buttonValNew;
}

Ive had a little more success, see code below, i can now get the motor to start turning, when it reaches the optical sensor it pauses, and returns 500 steps back and then stops. this is going to be the main starting position of the turntable (main track aligned).
the bit i cant get to work now is when i press the button the turntable should move 1000 steps in the opposite direction and then stop. if i can get this working then i can use the logic to add in extra button presses and home return steps. any help would be great please. :slightly_smiling_face: cheers Kevin.

#include <Stepper.h>
int motSpeed=3;
int dt=500;
int stepsPerRevolution = 2048;  //number of steps per revolution
// for your motor
int buttonPin=2;
int motDir=1;
int buttonValNew;
int sensorValNew;
int buttonValOld=1;
int sensorValOld=1;
int sensorPin=3;
int posHome;

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

void setup() {
  Serial.begin(9600);
  myStepper.setSpeed(motSpeed);
  pinMode(buttonPin,INPUT);
  digitalWrite(buttonPin,HIGH);
  pinMode(3,INPUT);
}
void homeing(){
  sensorValNew=digitalRead(sensorPin);
  
if (sensorValOld==1 && sensorValNew==0) {
    posHome=0;
    myStepper.step(posHome -500);
    myStepper.setSpeed(0);
    posHome=-500;
}
 else {
  myStepper.step(motDir*1);
  sensorValOld=sensorValNew;
} 
StartPos();
}

void StartPos(){
  buttonValNew=digitalRead(buttonPin);
  if (buttonValOld==1 && buttonValNew==0) {
  myStepper.setSpeed(motSpeed);
  myStepper.step(posHome +1000);
  myStepper.setSpeed(0);
  posHome=500;
  }
  else{
  }
  buttonValOld=buttonValNew;
}

void loop() {
homeing();
StartPos();
}

What is the stepper speed used when finding the index? I suggest running at good speed to find the index and the single step backwards checking for the index after every step. That might improve the home position.

Usually, stepper systems home at power up, so you would run homing in setup.

From there, you just count steps when moving, so loop needs to read buttons and move to a new position as they request.