user11181: Basically I have created the following code to control my stepper motor:
int pin_enable_motor_x = 38;
int pin_motor_step_x = A0;
int pin_direction_x = A1;
int endstop_x = 3;

void setup() {
  pinMode(pin_enable_motor_x, OUTPUT);
  pinMode(pin_motor_step_x, OUTPUT);
  pinMode(pin_direction_x, OUTPUT);
  pinMode(endstop_x, INPUT);

  digitalWrite(pin_enable_motor_x, LOW);

  for (int i = 0; i < 200000; i++) {
    runMotorSingleStepX(100, false); // runMotorSingleStepX(int delay, boolean direction)
    if (digitalRead(endstop_x) == true) {
      break;
    }
  }
}

void loop() {

}

void runMotorSingleStepX(int wait, boolean value) { 
  digitalWrite(pin_direction_x, value);
  digitalWrite(pin_motor_step_x, HIGH);
  delayMicroseconds(wait);
  digitalWrite(pin_motor_step_x, LOW);
  delayMicroseconds(wait);
}

The idea is that the stepper motor will give many single steps until it reaches my mechanical endstop. But what really happens is that it detects true random pulses even when my endstop isn't pressed. So my motor stops before the point it should stop. I'm not sure if it's a common problem and I should be using some tricks to fix this problem.
Does anyone know what could be happening supposing that my electronics circuit is properly settled?

user27482: Your for() loop is using an int type for the iterator. You are asking it to run for up to 200,000 steps but an int is a 16 bit signed entity (~+/-32,768) so the for() loop will exit well before it reaches your desired count. That may be why you are not reaching the stop while still in the loop.
You may want to use an unsigned long as the type for your loop counter and or use a while() construct in which you incorporate the exit condition.

11181: <p>Basically I have created the following code to control my stepper motor:</p>

<pre><code>int pin_enable_motor_x = 38;
int pin_motor_step_x = A0;
int pin_direction_x = A1;
int endstop_x = 3;

void setup() {
  pinMode(pin_enable_motor_x, OUTPUT);
  pinMode(pin_motor_step_x, OUTPUT);
  pinMode(pin_direction_x, OUTPUT);
  pinMode(endstop_x, INPUT);

  digitalWrite(pin_enable_motor_x, LOW);

  for (int i = 0; i &lt; 200000; i++) {
    runMotorSingleStepX(100, false); // runMotorSingleStepX(int delay, boolean direction)
    if (digitalRead(endstop_x) == true) {
      break;
    }
  }
}

void loop() {

}

void runMotorSingleStepX(int wait, boolean value) { 
  digitalWrite(pin_direction_x, value);
  digitalWrite(pin_motor_step_x, HIGH);
  delayMicroseconds(wait);
  digitalWrite(pin_motor_step_x, LOW);
  delayMicroseconds(wait);
}
</code></pre>

<p>The idea is that the stepper motor will give many single steps until it reaches my mechanical endstop. But what really happens is that it detects <code>true</code> random pulses even when my endstop isn't pressed. So my motor stops before the point it should stop. I'm not sure if it's a common problem and I should be using some tricks to fix this problem.</p>

<p>Does anyone know what could be happening supposing that my electronics circuit is properly settled?</p>
 27482: <p>Your <code>for()</code> loop is using an <code>int</code> type for the iterator. You are asking it to run for up to 200,000 steps but an <code>int</code> is a 16 bit signed entity (~+/-32,768) so the <code>for()</code> loop will exit well before it reaches your desired count. That may be why you are not reaching the stop while still in the loop.</p>

<p>You may want to use an unsigned long as the type for your loop counter and or use a <code>while()</code> construct in which you incorporate the exit condition.</p>
