Colntrolling stepper motor with endstop microswitch

Hi. I want to stop moving the stepper motor when I press micro switch. But after I press it the stepper motor works yet. what should I do?

This is the sketch:

#include <Stepper.h>

const int stepsPerRevolution= 500;

Stepper myStepper(stepsPerRevolution,2,3);

int endstop=4;

void setup() {
myStepper.setSpeed(10);

pinMode(endstop,INPUT_PULLUP);
Serial.begin(115200);
}

void loop() {
if (digitalRead(endstop==0)) {
  Serial.println("stop");
  myStepper.step(0);
}

if (digitalRead(endstop==1))
{
  
myStepper.step(stepsPerRevolution);
}
}

This is the micro switch:

Should read
digitalRead (endStop) == 0
ans similar for other digitalRead()

1 Like

I changed the code. But the result is the same.



#include <Stepper.h>

const int stepsPerRevolution= 500;

Stepper myStepper(stepsPerRevolution,2,3);

int endstop=4;

void setup() {
myStepper.setSpeed(10);

pinMode(endstop,INPUT_PULLUP);
Serial.begin(115200);
}

void loop() {
if (digitalRead(endstop)==0) {
  Serial.println("stop");
  myStepper.step(0);
}

if (digitalRead(endstop)==1)
{
 Serial.println("move"); 
 myStepper.step(stepsPerRevolution);
}
}

You will need to post a schematic or diagram of how your hardware is connected.

Does Serial correctly reflect status of the button?

I think most people on the forum knows what a micro switch looks like. What we need to see is where those 3 wires are going!

For use as an end stop switch, only 2 wires would be needed, not 3, so maybe you made an error here.

It just displays "move" but when I press the switch It doesn't display "stop".

Yikes! That will cause a short circuit when the micro switch is activated! Disconnect it immediately!

So what should I do?

Shortcircuit?

Sorry, I see it now.

Yes you shortcircuit GND and 5V+ in the switch

Connect "C" to the Arduino pin and NC to ground. Do not connect 5V. Do not connect NO.

digitalRead() will return LOW when the switch is not activated and HIGH when it is activated.

Using the NC connection in the switch is a better way because if the wires to the switch become detached, the motor will stop, which is a good safety feature.

1 Like

I changed the code. When the switch is free the switch digital read returns HIGH and when I press the switch digital read returns LOW. But there is a problem. I have to hold the button to stop the stepper and when I release it the stepper keeps moving. I want after I press button and the release it the stepper don't work. Like 3D printers.



#include <Stepper.h>

const int stepsPerRevolution= 500;

Stepper myStepper(stepsPerRevolution,2,3);

int endstop=4;

void setup() {
myStepper.setSpeed(10);

pinMode(endstop,INPUT_PULLUP);
Serial.begin(115200);
}

void loop() {
if (digitalRead(endstop)==HIGH) {
  Serial.println("stop");
  myStepper.step(0);
}

if (digitalRead(endstop)==LOW)
{
 Serial.println("move"); 
 myStepper.step(stepsPerRevolution);
}
}

It should be the opposite to that. Did you follow my instructions?

Yes. And when I upload the code in void setup, the result is different. the stepper keeps moving when I press the switch and I don't see 'stop' in serial monitor.



#include <Stepper.h>

const int stepsPerRevolution= 500;

Stepper myStepper(stepsPerRevolution,2,3);

int endstop=4;

void setup() {
myStepper.setSpeed(10);

pinMode(endstop,INPUT_PULLUP);
Serial.begin(115200);

if (digitalRead(endstop)==HIGH) {
  Serial.println("stop");
  myStepper.step(0);
}

if (digitalRead(endstop)==LOW)
{
 Serial.println("move"); 
 myStepper.step(stepsPerRevolution);
}
}

void loop() {

}

This statement will take 6 seconds to complete and nothing else in the code can execute
myStepper.step(stepsPerRevolution);

The limit switch will be ignored during the 6 seconds.
You will need to use interrupts to detect the limit switch closure

Can you tell me exactly what you mean? Which changes I need in my code?

The Stepper library is very simple to use but can get complicated when trying to do what you want.
The AccelStepper or MobaTools libaries would be bettor.
Most people find MobaTools easier.

I'm working on a machine like 3D printer. I need to define the start point of the stepper motor when I execute the code and then I don't need the end stop until the machine finishes working. Do you have any idea? Do you offer AccelStepper library for this case? And I have to use A4988 but I think AccelStepper library is for AF motor shields right?

It sounds like you connected NO instead of NC. Please check that again.

Now your code makes no sense. setup() runs only once. You need the code that checks the switch to repeat continuously. Move the code back to loop().

1 Like