Go Down

### Topic: How to achieve continuous rotation on a bipolar stepper motor. (Read 17535 times)previous topic - next topic

#### msdhillon

##### Jun 20, 2014, 03:02 pm
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.

#### TomGeorge

#1
##### Jun 20, 2014, 03:31 pm
Use CAD or picture of hand drawn circuit in jpg, png or pdf format.

Tom......
Everything runs on smoke, let the smoke out, it stops running....

#### johnwasser

#2
##### Jun 20, 2014, 03:41 pm
Code: [Select]
`const int ForwardLimitSwitchPin = 2;const int ReverseLimitSwitchPin = 3;const int StepperStepPin = 4;const int StepperDirectionPin = 5;const int LimitSwitchActivated = LOW;  // Limit switch grounds pinconst 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);    }}`
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

#### msdhillon

#3
##### Jun 20, 2014, 04:14 pm
Thanks for the prompt replies.

I am using DRV8825 stepper motor driver, high current for driving the stepper motor. http://www.pololu.com/product/2133
I have attached the picture. I am using http://www.pololu.com/product/1472  stepper motor. It has six leads and I am using for of them as mentioned on the website.

#### msdhillon

#4
##### Jun 20, 2014, 04:32 pm
@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.

#### johnwasser

#5
##### Jun 23, 2014, 02:46 am

Can I use your code for my project.

Yes.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

#### Precy

#6
##### Apr 01, 2015, 07:31 am
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
stepper.step(1);
}
// Step reverse until the limit switch is activated
stepper.step(-1);
}
}

#### Robin2

#7
##### Apr 01, 2015, 09:36 amLast Edit: Apr 01, 2015, 09:39 am by Robin2

Quote
"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.

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
Two or three hours spent thinking and reading documentation solves most programming problems.

#### Precy

#8
##### Apr 01, 2015, 10:20 amLast Edit: Apr 01, 2015, 10:24 am by Precy
the prograb is simulated i proteus. the motor atuomatically rotate eventhough the switch pins is in the low state. our reference circuit is ithis

#### TomGeorge

#9
##### Apr 01, 2015, 10:27 am
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.....
Everything runs on smoke, let the smoke out, it stops running....

#### Precy

#10
##### Apr 01, 2015, 10:48 am
@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
stepper.step(1);
}
// Step reverse until the limit switch is activated
stepper.step(-1);
}
}

#### TomGeorge

#11
##### Apr 01, 2015, 01:53 pm
Hi,
Can that CAD export an image, rather than you doing a screen capture?
Also remove the grid lines please.

Tom...
Everything runs on smoke, let the smoke out, it stops running....

#### Robin2

#12
##### Apr 01, 2015, 02:54 pm
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
Two or three hours spent thinking and reading documentation solves most programming problems.

#### dushan90

#13
##### Jan 13, 2019, 10:24 am
Code: [Select]
`const int ForwardLimitSwitchPin = 2;const int ReverseLimitSwitchPin = 3;const int StepperStepPin = 4;const int StepperDirectionPin = 5;const int LimitSwitchActivated = LOW;  // Limit switch grounds pinconst 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.

#### Robin2

#14
##### Jan 13, 2019, 11:21 amLast Edit: Jan 13, 2019, 11:25 am by Robin2
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
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up