How to control direction of stepper motor using micro switches

Hello.

First project on here i am in need of some help and guidance. I have a basic understanding of Arduino and have got as far as i can from reading and videos.

I am using a Big easy driver board from sparkfun to control my stepper motor which i currently have rotating a set amount of steps and then changing direction. This is rigged up to a lead screw. What i want to achieve is a constant rotation in one direction until a micro switch is pressed at the end of the lead screws travel, at which point the stepper motor direction changes and rotates until a micro switch is pressed at the other end. This will then switch the direction once more. The whole process is to run on loop, effectively moving a block up and down the lead screw at a constant speed.

I am using the AccelStepper library but only because i found an example that helped me understand the basics.

I am guessing i need to use the micro switch as an input and get it to switch the state of the stepper from HIGH to LOW??

Any help would be amazing as i have reached a sticking point

Thank you in advance

How to control direction of servo motor using micro switches

control my stepper motor which i currently have rotating a set amount of steps

the servo motor direction changes

The sticking point for me, is not knowing if you have a servo, a stepper, or one of each? 8)

Very good point thats not going to help my cause.

I am using a stepper motor.

Sorry for the confusion

Post your code, using code tags (the # button above the input box).

Look at the state change detection example to understand how to detect when a switch has been operated. If you have two limit switches, you will need to apply this logic separately to each one.

You need to have a variable recording the current direction of your stepper. The code handling the limit switches would change this to control which way the motor moves.

You would have some code to move the stepper in the current direction at whatever speed you want. The blink without delay example demonstrates how to run code at regular intervals; in this case, the code you run would move the stepper a small amount in the current direction.

Given that stepper motors give you absolute control over their position, once you have found the initial position you could produce the behaviour you describe just by controlling the steps taken by the motor. In that case you would just use one of the limit switches at startup to enable you to bring the stepper motor back to a 'home' position, and after that you wouldn't need to use the limit switches.

Very slow reply to all this sorry. Thank you for all your help so far.

Thank you PeterH for your advice, i did think about controlling it by step count but due to the application i need it to be fail proof, hence i thought the use of limit switch to trigger a change in direction. Over kill is perfectly acceptable for this project to ensure it doesn't hit the end and carry on going.

I have had some guidance from Proto-Pic on the code see below. However it is not working. I think i have failed to define something at the start of the sketch.

Any thought?? I'm stumped :frowning:

#define stepPin 2
#define dirPin 3
#define pinLimitswitchLeft 4  // these can be either left or right depending on motor wiring and dir pin.
#define pinLimitswitchRight 5 // these switches must be N.O. types
int stepDelay = 100; // this is the step delay in microseconds. Reduce this to speed up motor rotation and vice versa.
 
void setup()
{
 pinMode (stepPin, OUTPUT);
 pinMode (dirPin, OUTPUT);
 pinMode (pinLimitswitchLeft, INPUT_PULLUP);
 pinMode (pinLimitswitchRight, INPUT_PULLUP); 
}

void loop()
{
 digitalWrite(dirPin, HIGH);  // set direction pin, the direction that the motor will rotate is dictated by it's wiring
 while (digitalRead(pinLimitswitchLeft) == HIGH) // move motor until left limit switch is pressed.
 {
  stepMotor(); 
 }
 
 digitalWrite(dirPin, LOW);  // set direction pin, the direction that the motor will rotate is dictated by it's wiring
 while (digitalRead(pinLimitswitchRight) == HIGH) // move motor until right limit switch is pressed.
 {
  stepMotor(); 
 }

}

void stepMotor()
{
 digitalWrite(stepPin, HIGH);
 delayMicroseconds(stepDelay);
 digitalWrite(stepPin, LOW);
 delayMicroseconds(stepDelay); 
}

Again thanks for any help out there. I am learning

Very slowly

However it is not working.

Doesn't move at all? Doesn't react to the switches properly? Other?

No movement at all, Not even random stepper movements.

Wiring could be wrong...... Image attached i think

Quick check had two wires switched. My Bad.

This works code works perfectly.

Thanks for all the help especially Roy at Proto-Pic.

So now that i have it working for One stepper motor i thought getting it to work for five at the same time would be easy.

I was very wrong

I have now moved on to get trying to get three working. I have had a play with the sketch and have two stepper motors turning at the same rate. Each stepper has a set of micro switches (one at each end of the lead screws) to change the direction. Each stepper has its own Big easy driver board. So later on when using five steppers and using the arduino Uno board i will have used up all the digital and analog pins. stepper motors taking up 0-9 digital pins and the switches using all the analog pins and the remaining two digital pins. This is my thinking anyway.

my current sketch is

#define stepPinOne 0
#define dirPinOne 1
#define pinLimitswitchLeftOne A0  // stepper 1
#define pinLimitswitchRightOne A1 // 

#define stepPinTwo 2
#define dirPinTwo 3
#define pinLimitswitchLeftTwo A2  // stepper 2
#define pinLimitswitchRightTwo A3

#define stepPinThree 4
#define dirPinThree 5
#define pinLimitswitchLeftThree A4  // stepper 3 
#define pinLimitswitchRightThree A5

int stepDelay = 22
; 
 
void setup()
{
 pinMode (stepPinOne, OUTPUT);
 pinMode (dirPinOne, OUTPUT);
 pinMode (pinLimitswitchLeftOne, INPUT_PULLUP);
 pinMode (pinLimitswitchRightOne, INPUT_PULLUP);
 pinMode (stepPinTwo, OUTPUT);
 pinMode (dirPinTwo, OUTPUT);
 pinMode (pinLimitswitchLeftTwo, INPUT_PULLUP);
 pinMode (pinLimitswitchRightTwo, INPUT_PULLUP);
}

void loop()
{
 
 digitalWrite(dirPinOne, HIGH);  // stepper 1
 while (digitalRead(pinLimitswitchLeftOne) == HIGH) // move motor until left limit switch is pressed.
 
 {
   
  stepMotor(); 
 }
 
  digitalWrite(dirPinTwo, HIGH);  // stepper 2
 while (digitalRead(pinLimitswitchLeftTwo) == HIGH) // move motor until left limit switch is pressed.
 
 {
   
  stepMotor(); 
 }
 
 digitalWrite(dirPinOne, LOW);  // stepper 1
 while (digitalRead(pinLimitswitchRightOne) == HIGH) // move motor until right limit switch is pressed.
 
 {
  
   stepMotor(); 
 }
 
 digitalWrite(dirPinTwo, LOW);  // stepper 2
 while (digitalRead(pinLimitswitchRightTwo) == HIGH) // move motor until right limit switch is pressed.
 
 {
   
  stepMotor(); 
 }
}

void stepMotor()
{
 digitalWrite(stepPinOne, HIGH);
 digitalWrite(stepPinTwo, HIGH);
 delayMicroseconds(stepDelay);
 digitalWrite(stepPinOne, LOW);
 digitalWrite(stepPinTwo, LOW);
 delayMicroseconds(stepDelay); 
}

Currently with this code i am getting both steppers to turn (stepper 1 and stepper 2) however when the micro switches are pressed they seem to effect one direction at a time but not for the intended stepper. So when i press A0 it may change Stepper 1 but then if you press A1 to change the direction back it doesn't work. then A3 may be pressed and stepper on changes and not stepper 2?

I am a bit stuck here and have exhausted trial and error.

Any ideas??

Many thanks

You need to restructure your code. When you had one motor, this was OK:

 while (digitalRead(pinLimitswitchLeftOne) == HIGH) // move motor until left limit switch is pressed. 
 {  
  stepMotor(); 
 }

Now, it isn't. This loop concentrates only on a single limit switch while stepping both motors. The other limit switches can be hit and ignored.

A better solution is to examine all the limit switches at the top of loop. For each that are pressed, reverse the direction on the motor they control. Then step all motors with your existing stepMotor routine.

If you're lucky, that single step will un-press the limit switch. If not, a more complex algorithm will be required.