Endstops dont always register when ping ponging stepper motor

This code does ping-pong a stepper motor between 2 end stops, the problem is, it doesn't register every time an end stop is activated and will just get stuck.

This becomes even more of an issue because I'd like to run the motor at high speeds as possible.

Im not sure what to test next, could it be an issue with the code or the mechanical endstops?

Hardware
Arduino Uno Rev3
Nema 17 Stepper Motor 4-Wire 0.68Nm 1.8Deg 2 Phase
DM542 Stepper Motor Driver
Endstop image attached
Endstops - https://www.aliexpress.com/item/33023678899.html

#include <Adafruit_MotorShield.h>
Adafruit_MotorShield AFMS = Adafruit_MotorShield();          //creating object
Adafruit_StepperMotor *secondMotor = AFMS.getStepper(200, 1);// Defin pins we are using the ports M1 and M2
 //-------------------------------------------------------INPUTS---------------------------------------------------------
int endStop1 = 2;     // Push button for left
int endStop2 =3;      // Push button for right    

//Main Motor
int driverPUL = 7;    // PUL- pin
int driverDIR = 6;    // DIR- pin
int spd1 = A0;        // Potentiometer

//second motor
int spd2=A1;          //potentiometer
bool setdir2=5;       //direction switch for second motor


 //-------------------------------------------------------Variables---------------------------------------------------------

//Main Motor
int pd = 500;           // Pulse Delay period (speed)
boolean setdir1 = LOW;   // Set Direction

//second motor
int secondMotorSpeed;   //for the second motor after mapping
int x;                  //for the potentiometer values



// Interrupt Handler
//changes the direction of main motor and runs the second motor for 2 steps at the perviously set speed and direction.
void revmotor (){
  setdir1 = !setdir1;
  if(digitalRead(setdir2)==HIGH){
  secondMotor->step(2, FORWARD, DOUBLE);}
  else
  secondMotor->step(2, BACKWARD, DOUBLE);
}
 
 
void setup() {
  pinMode (endStop1, INPUT);
  pinMode (endStop2, INPUT);
  
  pinMode (spd1, INPUT);
  pinMode (driverPUL, OUTPUT);
  pinMode (driverDIR, OUTPUT);
  
  pinMode (spd2,INPUT);
  pinMode (setdir2,INPUT);
  
  attachInterrupt(digitalPinToInterrupt(endStop1), revmotor, FALLING);
  attachInterrupt(digitalPinToInterrupt(endStop2), revmotor, FALLING);  
}


 
void loop() {
    x = analogRead(spd2);
    secondMotorSpeed = map(x ,0 ,1023 ,0 ,255);
    secondMotor->setSpeed(secondMotorSpeed);

  
    pd = map((analogRead(spd1)),0,1023,1000,50);
    digitalWrite(driverDIR,setdir1);
    digitalWrite(driverPUL,HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL,LOW);
    delayMicroseconds(pd);
 
}

Hi,
Can you post a link to the limit switches?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.
In particular, how you have wired your limit switches.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

does the end stop include a builtin pull-up resistor ?

can it bounce?

If you want the highest speed as possible, you should use the endstops for homing and travel measurement, and then use acceleration and deceleration to hit speeds above the self-start range.

Why do you have one ISR for two attached endstops?

Endstops are here, but I am not sure if they have a pullup resistor (I'm a beginner at this) https://www.aliexpress.com/item/33023678899.html?

It does currently ping pong between the endstops, I would say 80% of the time they register correctly

Make sure your end stops are active by OPENING the switch, not by closing the switch. Add a condition to the interrupt code to NOT allow the interrupt to be processed unless some time has passed since the previous interrupt. You decide the time based on how fast your system operates.

Limit switches an present issues when used in motor applications.
All cables should preferably be shielded and not run alongside power runs.
There are a good few topics in the CNC section that cover this aspect in much more detail.
If running the motor in a small sketch consider adding some form of DEBOUNCE too.

Sure - https://www.aliexpress.com/item/33023678899.html?

Here is video showing issue, you can see sometimes it registers the endstop, other times takes many clicks to get it to change motor direction - Imgur: The magic of the Internet

I just have the endstops powered through a breadboard and plugged directly into the digital ports on the uno

You are using all 3 contacts of the the microswitch. Does the UNO pin 2 ever go to float condition during switch transition? That may cause an undetermined condition (not high or low guaranteed) for the input.

Those switches already include some basic filters.
However as stated they need shielded cable and not the ones normally supplied.
You can still add additional filtering at the Arduino end too.
Most of mine have a ferrite core with at least two wraps through them.
Consider also adding an additional ground from the motor chassis to the Arduino.

Hi,
Can you place a DMM in DC volts and measure with respect to gnd the voltage on the digital input pins that are connected to the limit switches when you activate them.

When you wrote this code, did you do it in stages and write some code JUST to test the limit activations?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

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