Stepper Home Position

Hi guys, for project that I am making I need to set stepper in home position. Plan is that stepper is rotating in reverse until carriage hits limit switch and that is home (start position). For now I have connected A4988 driver and I can control motor on pins 7 i 8 (direction and stepper). Limit switch is on 2 with internal pullup, and a4988 enable is on 3. Also for now I use internal LED. For now I am trying to set it to rotate until I press limit and since it senses press it shuts down (pin 3 goes high), and that's why there is nothing in loop since this is one of event on startup. Arduino is nano (eBay) and with this code all that happens is that motor is spinning but it won't stop on button press. What am I doing wrong :(

const int dirPin = 7;
const int stepperPin = 6;
const int homepolozaj = 2;
const int paljenjeMotora = 3;
const int ledPin = 13;
byte stanje;

void setup() {
  pinMode(dirPin, OUTPUT); //smjer
  pinMode(stepperPin, OUTPUT);
  pinMode(homepolozaj, INPUT); // limitswitch pin 2
  digitalWrite(homepolozaj, HIGH); // pali interni pullup
  pinMode(paljenjeMotora, OUTPUT); // pali gasi driver pin 3
  pinMode(ledPin, OUTPUT); // ledica

void loop() {

void pocetak () {
  digitalWrite(dirPin, HIGH);  // promjena smjera
  stanje = digitalRead(homepolozaj);   // cekam prekidac
  while (stanje == HIGH)
    digitalWrite(stepperPin, HIGH);
    digitalWrite(stepperPin, LOW);
        digitalWrite(ledPin, LOW);
        stanje = digitalRead(homepolozaj);
  digitalWrite(ledPin, HIGH);
  digitalWrite(paljenjeMotora, HIGH); // gasi motor

How is your switch wired - make a pencil drawing of all the connections and post a photo of the drawing.

Have you written a short test program to check that the line stanje = digitalRead(homepolozaj); works as you expect.

For the future this would be a better way to produce step pulses

digitalWrite(stepperPin, HIGH);
digitalWrite(stepperPin, LOW);

...R Simple Stepper Code Stepper Motor Basics

After afternoon run I have figured out the problem. It was pushbutton which had intermittent problem so it wasn't working during test, although it was working two days ago. now it is working as expected. What did you mean with controlling motor with pulses? How is that better, and how do I control speed and motor as general with UL pulses?

chuspy: What did you mean with controlling motor with pulses?

I didn't quite say that. I suggested a better way to produce pulses.

Your code produces pulses with equal high and low times. If you want to change the step interval you probably change both values.

In my code there is a very short HIGH and all of the timing is moved to the LOW part. Then if you want to change the step interval you only have to change one value.


You have not determined how accurately you need to control the home position of the stepper motor. Depending on what you use for a switch to detect home, it will vary a small amount each time. Will this be a problem? There is also a chance your stepper motor will actually go past the stop switch, depending on it's design.