Using a button but it randomly turns on without pressing?

so i made new project, all works fine, or atleast i tought so. But i left the power on, went to grab some zipties, come back and all of a sudden my machine is moving all on its own without pressing that start button. The way i set it up is that i have 1 side of my button to the arduin ground pin, the other to pin 12, and in the sketch i define startbutton on pin 12 and then i have this in the setup

 pinMode(startButton, INPUT_PULLUP);

Wich is think should be the right way to go? But for some reason it sometimes randomly sees a press of the button. I even checked the button, it wasnt not a closed contact and right on that moment it turned itself on as wel. So am i doing something wrong? Multimeter said the button is OL if i take it out and measure it, and when i pres it its 0ohm so that should be good.

Or is there another way to get out of these random "ghost" events?

And in case it might be needed. Its a setup where a tb6600 microstep driver is controlling a stepper motor to move to a switch at a preset steprate when i press the button. But now it sometimes moves to that switch without me pressing anything. Not how i intended it.

Your topic was MOVED to its current forum category which is more appropriate than the original as it is not an Introductory Tutorial

Please post your full sketch, using code tags when you do

Which Arduino board are you using ?

You probably know this but here goes:

Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘posting menu’ to attach the copied sketch.

1 Like

That is the schematic.
This is the button used

Wiring is 0.75mm² wiring with 2 wires in 1 black sleeve, about 50cm long. On the arduino end i solderd a pin that fits in to the arduino socket. There is a relay shield on top of the arduino.

1 Like

What sketch are you running that produces the random outputs ?

Remove it while testing INPUT_PULLUP

i tried it wiithout the relay shield as wel because i tough might be a glitchy shield as it was not working wel at first either. But even without the shield its the same. So i dont think its in the shield
Full code is below.

#include <AccelStepper.h>

// Define constants for the motor
const int stepsPerRevolution = 3200;
const int motorSpeed = 3000;
const int motorAccel = 400;

// Define pins for the motor
const int motorPin1 = 8;
const int motorPin2 = 9;

// Define pins for the limit switches and start button
const int topLimitSwitch = 2;
const int bottomLimitSwitch = 3;
const int startButton = 12;

// Define pin for the pump relay
const int pumpRelay = 7;

// Create a stepper object
AccelStepper myStepper(1,9,8);

void setup() {
  // Set the pins for the limit switches and start button as inputs
  pinMode(topLimitSwitch, INPUT_PULLUP);
  pinMode(bottomLimitSwitch, INPUT_PULLUP);
  pinMode(startButton, INPUT_PULLUP);

  // Set the pump relay pin as an output
  pinMode(pumpRelay, OUTPUT);

  // Set the motor speed, acceleration, and maximum speed
  myStepper.setMaxSpeed(motorSpeed);
  myStepper.setAcceleration(motorAccel);

  // Set the current position to 0
  myStepper.setCurrentPosition(0);

 digitalWrite(pumpRelay, LOW);  
}

void loop() {
  // Check if the start button is pressed
  if (digitalRead(startButton) == LOW) {
    // Turn on the pump relay and wait for 5 seconds
    digitalWrite(pumpRelay, HIGH);
    delay(5000);

    // Move the motor in the clockwise direction until the top limit switch is pressed
    while (digitalRead(topLimitSwitch) != LOW) {
      myStepper.setSpeed(motorSpeed);
      myStepper.runSpeed();
    }

    // Double the motor speed and move the motor in the counterclockwise direction until the bottom limit switch is pressed
    myStepper.setMaxSpeed(motorSpeed * 4);
    while (digitalRead(bottomLimitSwitch) != LOW) {
      myStepper.setSpeed(-motorSpeed * 4);
      myStepper.runSpeed();
    }

    // Set the motor speed back to normal and turn off the pump relay
    myStepper.setMaxSpeed(motorSpeed);
    digitalWrite(pumpRelay, LOW);
  }
}

Load this sketch
https://docs.arduino.cc/built-in-examples/digital/InputPullupSerial
but change the pin number to match the button.

Do you really have to set the motor speed 1,000 times?

Are you really saying that it does the aberrant behaviour without the motor connected, just the button, or is the schematic incomplete. Your initial post had me thinking motor EMI or current transients on your ground.
If it does it with just the button connected, it's strange, because a bad solder joint, or bad wire, or other bad connection, would prevent a button press, not cause it.

what do you mean with "Do you really have to set the motor speed 1,000 times?"
?

I mean its taking the info from the motorspeed int there if im right? And later on its going to move the other way at 4times that speed. I dont really see your point here at all?

Even with the motor disconnected and a serial readout i had random lows. So either its picking up some interference from outside, but i tought that was a bit les common with the input pullup option? Or it might be a broken arduino?
When i unplugged the motor i also unplugged the driver power supply from the net just to make sure i wasnt picking up from a power supply in the area. I know that sometimes causes glitches.

Well, using input pullup, it shouldn't see anything that results from a wire break; however, the other possibility is a loose wire occasionally shorting the pin to ground. I guess I have to ask, could you show us the bottom of the Arduino board?
This is probably a tangent, by the way, so feel free to not pursue, but I can't think of another cause.

Does the button have an LED inside it?

the arduino is in this case. Im guessing you are thinking it might short out on something its laying on? Or do you think a short on the arduino itself?

no there is no LED in this button, just a simple (chinese i guess) button.

Ok, no LED (no possible defect causing a short). Does the "action" of the button feel and sound solid (no weak spring or dangling internal parts)? Have you tried other devices?

I chased a solder finger two days ago for nearly half an hour, and it wasn't intermittent; I had taken a shortcut, not cleaned the board after soldering; solder bit stuck in the flux. It's an easy mistake to make, easier if your not skilled at soldering.
But, if the board looks clean, I'm out of ideas.

I don't see any debouncing.

would debouncing be the problem? Its not like it sees a double press, it literally turns itself on while im standing there, can be 5 min after i last touched it.
And i dont really see a reason for debouncing as the sketch wil run for 30+ sec before its done, so even if it bounces the sketch wil allready be going and it keeps on going.