How to achieve continuous rotation on a bipolar stepper motor.

Hi All
I want to know how can I make a program to rotate a stepper to rotate continuously until I tell it otherwise.
I want to make a program where I have two limit switches. If the stepper rotates clockwise and it should continue until it hit one of the limit switches and then it should rotate anticlockwise direction and it another limit switch and rotate clockwise and so on.

I tried to use default stepper library but I could not figure out how to rotate it continuously. I also tried custom stepper library and it solves the problem but it takes at least 4 output pins to attach stepper. My arrangement works with only two outputs to the stepper.

I am new to arduino so any help would be appreciated.

1 Like

Hi, post us a copy of your circuit of your arrangement.
Use CAD or picture of hand drawn circuit in jpg, png or pdf format.

Tom...... :slight_smile:

const int ForwardLimitSwitchPin = 2;
const int ReverseLimitSwitchPin = 3;
const int StepperStepPin = 4;
const int StepperDirectionPin = 5;
const int LimitSwitchActivated = LOW;  // Limit switch grounds pin
const int StepperMaxRPM = 100;

Stepper stepper(200, StepperStepPin, StepperDirectionPin);

void setup() {
    pinMode(ForwardLimitSwitchPin, INPUT_PULLUP);
    pinMode(ReverseLimitSwitchPin, INPUT_PULLUP);
    stepper.setSpeed(StepperMaxRPM);
}

void loop() {
    // Step forward until the limit switch is activated
    while (digitalRead(ForwardLimitSwitchPin) != LimitSwitchActivated) {
        stepper.step(1);
    }
    // Step reverse until the limit switch is activated
    while (digitalRead(ReverseLimitSwitchPin) != LimitSwitchActivated) {
        stepper.step(-1);
    }
}

Thanks for the prompt replies.

I am using DRV8825 stepper motor driver, high current for driving the stepper motor. Pololu - DRV8825 Stepper Motor Driver Carrier, High Current
I have attached the picture. I am using Pololu - Stepper Motor: Unipolar/Bipolar, 200 Steps/Rev, 57×56mm, 7.4V, 1 A/Phase stepper motor. It has six leads and I am using for of them as mentioned on the website.

0J4232.600.png

@john

Thanks very much. You are a life saver. Can I use your code for my project. It works. I understood the basic functionality and now I can expand on that to what I need.
:slight_smile:

msdhillon:
Can I use your code for my project.

Yes.

1 Like

Hi all, i'm using this codes for the continuous rotation of stepper motor but its not working,anyone who can help me with this? i'm using L297 and L298 stepper motor driver. I'm new in arduino, i badly need you help
#include<Stepper.h>
const int ForwardLimitSwitchPin = 2;
const int ReverseLimitSwitchPin = 3;
const int StepperStepPin = 4;
const int StepperDirectionPin = 5;
const int LimitSwitchActivated = LOW; // Limit switch grounds pin
const int StepperMaxRPM = 100;

Stepper stepper(200, StepperStepPin, StepperDirectionPin);

void setup() {
pinMode(ForwardLimitSwitchPin, INPUT_PULLUP);
pinMode(ReverseLimitSwitchPin, INPUT_PULLUP);
stepper.setSpeed(StepperMaxRPM);
}

void loop() {
// Step forward until the limit switch is activated
while (digitalRead(ForwardLimitSwitchPin) != LimitSwitchActivated) {
stepper.step(1);
}
// Step reverse until the limit switch is activated
while (digitalRead(ReverseLimitSwitchPin) != LimitSwitchActivated) {
stepper.step(-1);
}
}

@Precy - you are double posting and wasting time. I have already replied to your question in your other Thread

Here is the other answer - ask the Moderator to lock the other Thread

"its not working" does not provide any useful diagnostic information.

Have you any program that can make the motor work properly?
If not do that first. Don't bother with limit switches or anything else that might complicate things.

If you can make the motor work properly with other code tell us what, exactly, the problem code does.

You don't have any Serial.println() statements to allow you to see the values of the detected switches.

L298s are a poor choice for driving stepper motors. See stepper motor basics.

Post a link to the datasheet for your stepper motor?
Give details of the motor power supply - volts and amps.

My reply would probably have been different if I had known the code came from an experienced programmer and was working for another user. However I think I covered the essential points.

...R

the prograb is simulated i proteus. the motor atuomatically rotate eventhough the switch pins is in the low state. our reference circuit is ithis

ckt.jpg

Hi,

Can you please post a copy of your sketch, using code tags?
Please use code tags.. See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

Tom..... :slight_smile:

@tom here is my code :

#include<Stepper.h>
const int ForwardLimitSwitchPin = 2;
const int ReverseLimitSwitchPin = 3;
const int StepperStepPin = 4;
const int StepperDirectionPin = 5;
const int LimitSwitchActivated = LOW; // Limit switch grounds pin
const int StepperMaxRPM = 100;

Stepper stepper(200, StepperStepPin, StepperDirectionPin);

void setup() {
pinMode(ForwardLimitSwitchPin, INPUT_PULLUP);
pinMode(ReverseLimitSwitchPin, INPUT_PULLUP);
stepper.setSpeed(StepperMaxRPM);
}

void loop() {
// Step forward until the limit switch is activated
while (digitalRead(ForwardLimitSwitchPin) != LimitSwitchActivated) {
stepper.step(1);
}
// Step reverse until the limit switch is activated
while (digitalRead(ReverseLimitSwitchPin) != LimitSwitchActivated) {
stepper.step(-1);
}
}

Hi,
Sorry your image is unreadable.
Can that CAD export an image, rather than you doing a screen capture?
Also remove the grid lines please.

Tom... :slight_smile:

Precy:
the prograb is simulated i proteus.

I doubt if that includes either a link to the datasheet for your motor OR the details of the motor power supply that I requested earlier.

...R

johnwasser:

const int ForwardLimitSwitchPin = 2;

const int ReverseLimitSwitchPin = 3;
const int StepperStepPin = 4;
const int StepperDirectionPin = 5;
const int LimitSwitchActivated = LOW;  // Limit switch grounds pin
const int StepperMaxRPM = 100;

Stepper stepper(200, StepperStepPin, StepperDirectionPin);

void setup() {
   pinMode(ForwardLimitSwitchPin, INPUT_PULLUP);
   pinMode(ReverseLimitSwitchPin, INPUT_PULLUP);
   stepper.setSpeed(StepperMaxRPM);
}

void loop() {
   // Step forward until the limit switch is activated
   while (digitalRead(ForwardLimitSwitchPin) != LimitSwitchActivated) {
       stepper.step(1);
   }
   // Step reverse until the limit switch is activated
   while (digitalRead(ReverseLimitSwitchPin) != LimitSwitchActivated) {
       stepper.step(-1);
   }
}

Hello!

How do I change this to stop when it hits the limit switch?

What I need is that it should starts to rotate when I press a push button and rotates until it hits limit switch 1. And when I press the push button once again, it should stars to rotate in the opposite direction until it its limit switch 2.

dushan90:
How do I change this to stop when it hits the limit switch?

That is not a complete program. And this Thread has been dead for nearly 4 years.

It would probably make more sense to start your own Thread with details of your project. For example what stepper motor driver and stepper motor are you using? (Post links to their datasheets)

And if you have a program that partially works post it and tell us what it actually does and what you want it to do that is different.

...R