Add limit switches to stepper motor

I have an example sketch that uses a potentiometer to increase speed of a stepper motor in the clockwise direction. I have that all working quiet well and I'm very happy since this is my first project. I however would like to know how to add code for a limit switch to reverse the direction to counter-clockwise or what ever direction is opposite from the current direction as soon as the limit switch is hit.

#include <Stepper.h>

const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);

int stepCount = 0; // number of steps the motor has taken

void setup() {
// nothing to do inside the setup
}

void loop() {
// read the sensor value:
int sensorReading = analogRead(A0);
// map it to a range from 0 to 100:
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// set the motor speed:
if (motorSpeed > 0) {
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(stepsPerRevolution / 100);
}
}

What sets the motor direction in the current code ?

UKHeliBob:
What sets the motor direction in the current code ?

The myStepper.step(x) function directs the code to issue x steps for clockwise, -x steps counter-clockwise.

aandrew7:
I however would like to know how to add code for a limit switch to reverse the direction to counter-clockwise or what ever direction is opposite from the current direction as soon as the limit switch is hit.

Firstly you will need 2 limit switches. One for each direction.
Do you know how to wire up and read the state of a pin connected to a switch ?

UKHeliBob:
Firstly you will need 2 limit switches. One for each direction.
Do you know how to wire up and read the state of a pin connected to a switch ?

I have been trying to learn how to do exactly that. I have two limit switches and I am assuming that from the three connectors I would wire one to ground, one to 5V and one to the defined pin!? I'm just learning as I go, I have added more code to my sketch but I am sure I'm missing much. I believe I have here defined I have something called Limit01 and Limit02 and I believe I have set them to INPUTs though I'm not sure if this is in the right areas or if this is how I am even suppose to go around to doing this.

#include <Stepper.h>

const int stepsPerRevolution = 400; // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);

int stepCount = 0; // number of steps the motor has taken

#define Limit01 2 // Pin 2 connected to Limit switch out
#define Limit02 3 // Pin 3 connected to Limit switch out

void setup() {
pinMode (Limit01, INPUT);
pinMode (Limit02, INPUT);
}

void loop() {
// read the sensor value:
int sensorReading = analogRead(A0);
// map it to a range from 0 to 100:
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// set the motor speed:
if (motorSpeed > 0) {
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(stepsPerRevolution / 100);
}
}

I have two limit switches and I am assuming that from the three connectors I would wire one to ground, one to 5V and one to the defined pin!?

Make, model, part number, wiring diagram, picture of the switches?

DKWatson:
Make, model, part number, wiring diagram, picture of the switches?

I have attached a photo of my limit switch.
It is labelled "6A 125 250V~10T85" and the connections from left to right at labelled 1,2 and 3 and on opposite side; 3 is labelled "NC" 2, "NO" and 3 "C" as far as I can tell from the small print. Hope that makes sense!

I have also been adding to my code but I am still unclear on what I am doing. I am getting examples from videos and trying to figure how to apply it to my sketch.

#include <Stepper.h>

const int stepsPerRevolution = 400; // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);

int stepCount = 0; // number of steps the motor has taken

#define Limit01 2 // Pin 2 connected to Limit switch out
#define Limit02 3 // Pin 3 connected to Limit switch out

void setup() {
pinMode (Limit01, INPUT);
pinMode (Limit02, INPUT);
}

void loop() {
// read the sensor value:
int sensorReading = analogRead(A0);
// map it to a range from 0 to 100:
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// set the motor speed:
if (motorSpeed > 0) {
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(stepsPerRevolution / 100);
}
}

if (!digitalRead(Limit01)) {} // check if limit switch is activated

then {
digitalWrite(dir_pin, HIGH); // HIGH = anticlockwise / LOW = clockwise)
}
}

if (!digitalRead(Limit02)) {} // check if limit switch is activated

then {
digitalWrite(dir_pin, LOW); // HIGH = anticlockwise / LOW = clockwise)
}
}

}

"then" does not seem to be understood..

I would really like help with my sketch, If I can see what a working sketch looks like I will be able to figure out how to connect the limit switches in order for everything to work. :confused:

Okay, so forget everything else. Your three connections: NC, NO, C stand for Normally Closed, Normally Open and Common. That means you use Common and one of the other two.

A normally open switch looks like ./ . where there is no connection between the two terminals.
A normally closed switch appears .._ where there is always a connection.

In either case, when the switch is activated/pushed/whatever, it goes closed if open, open if closed. Think of it as bit = !bit. Whatever you connect to your two terminals can then be interpreted by your logic. Say you set pinX to INPUT_PULLUP and connect to Common and then connect NO to Gnd. pinX remains HIGH while the switch is inactive. As soon as the switch is triggered it completes the circuit to ground thereby bringing the level on pinX LOW. This can be polled or trapped by a pin change interrupt on FALLING and then you have your end-stop/limit code take over and do whatever it does.

Other logic options are possible.

Hi aandrew7

First off, go to this page and have a look at the wiring diagram and code.

The code is very basic. It will just stop the motor when the limit switch is activated, but it will give you the beginning steps.

From the picture you posted of your project, you will need to wire the switches in properly.

Be aware that this is a very, very simple sketch. You will need to do a lot more research.

As to your own code, you may also need to do some research in programming in C/C++

if (!digitalRead(Limit01)) {} // check if limit switch is activated

   then {
     digitalWrite(dir_pin, HIGH);  // HIGH = anticlockwise / LOW = clockwise)
   }
 }

is syntactically incorrect.

The word "then" is not used in C/C++ in this context.

The code should look like this:

  if (!digitalRead(Limit01)) { // check if limit switch is activated
    digitalWrite(dir_pin, HIGH);  // HIGH = anticlockwise / LOW = clockwise)
  }

Take particular note of the braces "{}" and where they are positioned.

Also note that this minor correction will fix the syntax, but will not fix the sketch.
I'll leave it up to you to examine the sketch and wiring I pointed you to, then make modifications to your code to achieve what you want.

Ok I understand how to connect the limit switches now Thank you!, I have fixed and verified my sketch but my switches are not reversing the stepper. I will keep researching of course until I am finished and satisfied. I will go over the examples you showed me again, I have selected different pins then shown in the example and left out things like enPin and stepPin (Do I need those?) but everything else looks like it should work.

#include <Stepper.h>

const int stepsPerRevolution = 400; // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);

int stepCount = 0; // number of steps the motor has taken

#define Limit01 2 // Pin 2 connected to Limit switch out
#define Limit02 3 // Pin 3 connected to Limit switch out
#define dir_pin 8 // Pin 8 connected to Direction pin

void setup() {
pinMode (Limit01, INPUT);
pinMode (Limit02, INPUT);
pinMode (dir_pin, OUTPUT);
}

void loop() {
// read the sensor value:
int sensorReading = analogRead(A0);
// map it to a range from 0 to 100:
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// set the motor speed:
if (motorSpeed > 0) {
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(stepsPerRevolution / 100);
}

if (!digitalRead(Limit01)) { // check if limit switch is activated
}
digitalWrite(dir_pin, HIGH); // HIGH = anticlockwise / LOW = clockwise)

{
if (!digitalRead(Limit02)) { // check if limit switch is activated
digitalWrite(dir_pin, LOW); // HIGH = anticlockwise / LOW = clockwise)
}
}
}

Hi aandrew7

Two things.

  • Please read this post, specifically the part about posting code.
  • have another look at your if statements, paying particular attention to where the braces " {} " are placed. Compare them to the code I posted. You could read through this article.

cheers...

darrob:
Hi aandrew7

Two things.

  • Please read this post, specifically the part about posting code.
  • have another look at your if statements, paying particular attention to where the braces " {} " are placed. Compare them to the code I posted. You could read through this article.

cheers...

Excellent. Thank you! I did read that page I guess I missed that code part.

I have only just joined this forum and Honestly only since May I have started to use Linux, learn Python, get a Raspberry Pi then buy some kits, learn about electricity then start to develop a hobby in building electronics. learn to solder, come up with a project which lead me to Arduino, read and watch countless hours of tutorials, buy a stepper motor and then a driver and then a power source and then and then and then which brought be to here. I guess it's C++ next. I just want to tilt a screen with a push of a button LOL.
The people on this Forum have been extremely helpful and I can't get over how quick my questions are replied to and instructive you all are. If I can do anything for YOU please let me know.

UKHeliBob:
Firstly you will need 2 limit switches. One for each direction.
Do you know how to wire up and read the state of a pin connected to a switch ?

I am 99% sure the the limit switches are wired up correctly but I don't know how to read them.

aandrew7:
I am 99% sure the the limit switches are wired up correctly but I don't know how to read them.

Have a look at the Button example in the IDE

OR....Depending on the stepper motors , you may be able to use a mechanical stop to stall the motor for a brief time . This is often used ( in car gauges for example) as an easy way to reference a start position for the stepper motor. ( you drive it to the stop more than enough steps to be sure it’s at the stop.)

you drive it to the stop more than enough steps to be sure it's at the stop.

That does not sound like a good idea at all

UKHeliBob:
That does not sound like a good idea at all

With a current sensor, and stopping try to step when the motor actually stalls, it could work. This method is frequently used when there simply isn't space for limit switches (as in gauges).

PaulS:
With a current sensor, and stopping try to step when the motor actually stalls, it could work. This method is frequently used when there simply isn't space for limit switches (as in gauges).

With the addition of a current sensor it becomes viable option but I don't see the OP doing that any time soon.

aandrew7:
I have an example sketch that uses a potentiometer to increase speed of a stepper motor in the clockwise direction. I have that all working quiet well and I'm very happy since this is my first project. I however would like to know how to add code for a limit switch to reverse the direction to counter-clockwise or what ever direction is opposite from the current direction as soon as the limit switch is hit.