Stop stepper motor when hitting limitswitch

Hi guys,

I am trying to create a program where the stepper motor controlled with a L298H stops when hitting a limitswitch. However with the default stepper.h it needs to finish its pre-set length.

I am wondering if theres a library or code that helps to keep the motor running until it hits the limit switch.

Do you have a suggestion what i can do?

Thanks!

Break the movement into small steps

move 1 step
if the limit switch is activated then stop
repeat as many times as necessary

You could use the MoToStepper class of my MobaTools Lib. This lib is not blocking while the motor turns. You can read your limit switch and stop at any time.The lib can be installed by means of the library manager. Examples and a documentation in english are provided.

Thanks for your reply! i tried the library you created. However the motor only vibrates and doesnt spin. The code is used is this one:

/* ====== minimumStepper =======================================
 *  Bare minimum to get a stepper with step/dir driver turning
 */
#include <MobaTools.h>
// Stepper connections - Please adapt to your own needs.

const int stepsPerRev = 200;    // Steps per revolution - may need to be adjusted

MoToStepper stepper1( stepsPerRev, STEPDIR );  // create a stepper instance

void setup() {
  stepper1.attach(8,9,10,11);
  stepper1.setSpeed( 300 );              // 30 rev/min (if stepsPerRev is set correctly)
  stepper1.setRampLen( stepsPerRev / 2); // Ramp length is 1/2 revolution
  stepper1.rotate(1);                    // start turning, 1=vorward, -1=backwards                    
}

void loop() {
}

Connection is like this:

EN1 -> pin 8
EN2 -> pin 9
EN3 -> pin 10
EN4 -> pin 11

Am i misconnecting something?

Both jumpers are installed.

If you are using an L298H, you should not be creating your stepper instance as step/direction, but instead with HALF or FULL steps

//MoToStepper stepper1( stepsPerRev, STEPDIR );  // create a stepper instance
MoToStepper stepper1( stepsPerRev, HALFSTEP );  // create a stepper instance

The first important advice was given by @blh64. And if you are using an L298 H-bridge the wiring should look like this:

EN1 -> pin 8
EN2 -> pin 10
EN3 -> pin 9
EN4 -> pin 11

You could also change the order in the attach function, and leave the wiring as it is:

  stepper1.attach(8,10,9,11);

Multi-tasking in Arduino has a complete stepper example controlled by user inputs and temperature.
You can replace the temperature input with your limit switch readings

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