Inductive Sensor with Stepper Motor Not Working


Thanks for reading this post.

I am working on a project where a video monitor has been attached to a lead screw and will move back on forth. Using inductive sensors, my plan was to have the stepper motor reverse direction when coming in contact with a sensor. This worked in my trial run, but now is failing. I’m hoping it is a simple solution that another pair of eyes can recognize. What is failing now is when the monitor reaches a sensor it sits there, engaged with the sensor, but does not reverse direction. Below is info on my components and my sketch. Any help would be greatly appreciated!

  • Arduino Uno
  • Motor: NEMA 17 Stepper
  • Motor driver: TB6560
// defines pins numbers

const int stepPin = 5;

const int dirPin = 2;

const int enPin = 8;

const int LimitSwitch_LEFT_Pin  = 10;

const int LimitSwitch_RIGHT_Pin = 11;

void setup() {


  pinMode(LimitSwitch_LEFT_Pin , INPUT);

  pinMode(LimitSwitch_RIGHT_Pin , INPUT);

  // Sets the two pins as Outputs





  // Set Dir to Home switch

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction


void loop() {

    int leftSw  = digitalRead( LimitSwitch_LEFT_Pin);

    int rightSw = digitalRead( LimitSwitch_RIGHT_Pin);


    if( (leftSw  == HIGH && (digitalRead(dirPin) == HIGH)) ||

        (rightSw == HIGH && (digitalRead(dirPin) == LOW)) ){




    else if( leftSw == LOW && (digitalRead(dirPin) == HIGH) ){




    else if( rightSw == LOW && (digitalRead(dirPin) == LOW ) ){






void motorStep( int MAX){

   for(int x = 0; x < MAX; x++) {









Use Serial and put some debugging prints in there to see where execution is going. Display the value of you switches too.

Should you be using input_pullup on your switches?

If the software worked properly during the trial as indicated, it appears it may be a hardware problem. Please post a schematic, not a frizzy thing, with all power and ground connections. Post links to the motor etc. Is the motor the same one that was used during trials. Is the load on the motor the same?

You are not speed-ramping the stepper, so its probably stalling - NEMA17 motors are far too massive to
start up at 1000 steps per second from stationary.

Perhaps you need the AccelStepper library?

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