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) {

void loop() {


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

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?

  • How is this endstop switch wired exactly?
    – Nick Gammon
    Commented Oct 23, 2016 at 3:00
  • I'm controlling a RAMPS board (reprap.org/wiki/File:RAMPS1.4schematic.png)... It happens with all endstop min (3,14,18) and max (2,15,19) pins... I believe that it's some kind of noise problem that's possible to treat by software because the Marlin firmware ( the official firmware that runs with the Arduino Mega and RAMPS) works fine.
    – Ramuyko
    Commented Oct 23, 2016 at 3:24
  • 2
    Try making the endstop mode INPUT_PULLUP
    – Nick Gammon
    Commented Oct 23, 2016 at 3:28
  • It seems to be a little more stable... But it still stops before the point sometimes.
    – Ramuyko
    Commented Oct 23, 2016 at 3:36
  • 1
    Try a stronger pull-up, like 10k. Try a capacitor (eg. 0.1 µF) between the switch pin and ground.
    – Nick Gammon
    Commented Oct 23, 2016 at 4:14

2 Answers 2


Answer based on comments originally under the question

It sounds like the endstop switch may have floating input. Try making the endstop pins INPUT_PULLUP rather than INPUT.

It seems to be a little more stable... But it still stops before the point sometimes.

Try a stronger pull-up, like 10k. Over a long wiring run the inbuilt 50k pull-up may not be strong enough to counter noise in nearby wires.

Thanks, the extra 10K resistor solved the problem :)


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.

  • That's true, thanks ... But actually I wasn't using the 200000 max value. I just put a huge number there to make tests. Even fixing the int value my endstop is still detecting random pulses along the way and stopping before my motor reaches it.
    – Ramuyko
    Commented Oct 23, 2016 at 2:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.