<Help> Using buttons in parallel with servomoteur create interference

Hi everyone,
I've recently started a school project where I have to create a moving load on a rail, using a servomotor (with an ASDA B2 driver). I've completed the motor part, but now I have to add button on each end of the track to stop the motor when the load goes too far.
So I've simply followed the Arduino tutorial to install a button and the same code, but something weird happens when trying the program. If I press the button, the motor (which is still wired in but not declared in anyway in the program) moves by a step. I can't find any solution to stop this behavior. I don't know if it's some kind of interference in the board (I'm using an Arduino Due).
I'm putting the code below but I don't think it will be of much help since it's just on included in the examples.

const int buttonPin = 4;
const int ledPin = 13;    

int buttonState = 0;  

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}

As any of you encountered such issue ?
Tell me if I need to add any information.
Thanks for your help !

Are we supposed to guess which pins you have the motor or driver wired up to?

Sorry, the driver is connected to pin 12 and 13, which control the Pulse and Direction of the motor. They are connected to an optoisolator which then connects to the driver which uses 24V.

A picture is always worth a thousand words.
Show us the circuit diagram.

So the driver is connected to pin 13, and the LED is also connected to pin 13. Do you not see a problem with that?

LED_BUILTIN

Yes, and when the sketch writes to the LED on pin 13, it also writes to the driver - which is also on pin 13. Which explains why the motor moves by a step whenever the LED is toggled - the driver is being written to as well.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.