Go Down

Topic: Limit switch control of stepper motor direction (Read 3928 times) previous topic - next topic


I am trying to control a stepper using the stepper.h library on the Arduino UNO.
The only additional thing is the control of direction of rotation of the stepper with a limit switch.
For either of the two conditions (HIGH and LOW) with the if statement in the code, the stepper motor works as intended.
However, when the two conditions are put together, only the HIGH condition is recognized.
Does anybody have any suggestions.
The code is
Code: [Select]
#include <Stepper.h> //stepper library

const int stepback = 2; //motorpin
const int stepforw = 3; //motorpin
const int Switch1= 4;  // pin from limit switch
int one_rotation=6400; //number of steps required for easydriver at 1/8 step
//to complete one revolution.

int n=1;
int Steps=8; //change the number of steps to achieve resolution and speed
int rotation=one_rotation/Steps;
int j = 0;
int k;
int Distance = n*rotation; //number of revolution times step in a revolution
int RPM = 20;
Stepper mystepper= Stepper (rotation, stepforw, stepback);

void setup()
  pinMode(Switch1, INPUT);  //pin from limit switch
  digitalWrite(Switch1, LOW); //limit switch pin; not necessary
  digitalWrite(stepback,LOW); // stepper motor pin1
  digitalWrite(stepforw,LOW); // stepper motor pin2
  mystepper.setSpeed(RPM);  // set the speed of the stepper


void loop()
    if (digitalRead(Switch1)==LOW) //limit switch is off
    mystepper.step(Steps);    //steps in forward direction
        if (digitalRead(Switch1)==HIGH) //limit switch is on
         mystepper.step(-Steps);   //reverse direction


You have only one limit switch?  How is that supposed to work?

Also your code is stateless, you need to have the current direction as a variable (which has state).

Currently it will go till it hits the switch and then oscillate as the switch goes on and off in rapid succession.

You need each limit switch to trigger a change in state in the correct direction - not one switch trying to
control both directions.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]


Thank you MarkT for your reply.
My understanding is that the direction of the current through the stepper is controlled by the sign of the 'step' in the stepper.h sketch. As I mentioned, the first if statement in the sketch, when the limit switch is LOW, turns the stepper in the CW. And when the limit switch is HIGH, it turns CCW without problem. But, when the two if statements are used in the same sketch, the first if statement is not recognized (LOW). Although I hear there is a load on the stepper, as I hear a humming sound.
Yes, you are right in mentioning the need for two limit switch for practical purpose. I was trying out the control of two logical states of rotation with two logical states of the limit switch (HIGH and LOW).
Any insight is greatly appreciated.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131