Basic limit switch question TB6600 Arduino Uno Nema 23 reverse motor direction

Hi there,

I have learn basic code to make a motor spin in either direction.
I am also able to wire limit switches to the Arduino Board.

Is it possible to achieve the following;?

I would like to place the limit switches either side of a very small ballscrew.
I would like the motor to turn in one direction until it activates the limit switch at which point the motor will now reverse direction.

And therefore will be going forward and back all day long using the limit switches at either end as foward and reverse buttons.

If this is possible it would save me alot of time trying to find codes on the internet.

regards.

Yes.

Set the CW/Direction pin of your TB6600 to choose which direction the stepper motor will turn. Pulse the CLK/Step pin to get the stepper motor to move a step. Use digitlRead() to determine if you have reached the limit switch. If you have, reverse direction.

Like this?

thats it ping pong lol

would it possible to put a time delay in thier for 60 seconds at each end?

Q question,nand a suggestion.
.
1.Why a stepper ?

  1. Remember to slow and srop before reversing each ruined, or you may skip steps / stress the mechanical components.

Please post the code that you want to modify. Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Yes. The simplest way is "delay(60000ul);" in the place where you "Use digitalRead() to determine if you have reached the limit switch. If you have, reverse direction."

const byte TB6600_CW_Pin = 2;
const byte TB6600_CLK_Pin = 3;
const byte CW_Limit_Pin = 4;
const byte CCW_Limit_Pin = 5;

void setup()
{
  pinMode(TB6600_CW_Pin, OUTPUT);
  pinMode(TB6600_CLK_Pin, OUTPUT);
  pinMode(CW_Limit_Pin, INPUT_PULLUP);
  pinMode(CCW_Limit_Pin, INPUT_PULLUP);
}

void loop()
{
  static int currentDirectionCW = HIGH;

  // Set the CW/Direction pin of your TB6600 to choose which direction the
  // stepper motor will turn.
  digitalWrite(TB6600_CW_Pin, currentDirectionCW);

  // Use digitlRead() to determine if you have reached the limit switch.
  // If you have, reverse direction.
  if (currentDirectionCW == HIGH && digitalRead(CW_Limit_Pin) == LOW)
  {
    // Hit CW limit while traveling CW
    currentDirectionCW = LOW; // Reverse to CCW
    delay(60000ul); // delay for 60 seconds at each end
  }

  if (currentDirectionCW == LOW && digitalRead(CCW_Limit_Pin) == LOW)
  {
    // Hit CCW limit while traveling CCW
    currentDirectionCW = HIGH; // Reverse to CW
    delay(60000ul); // delay for 60 seconds at each end
  }

  // Pulse the CLK/Step pin to get the stepper motor to move a step.
  digitalWrite(TB6600_CLK_Pin, HIGH);
  digitalWrite(TB6600_CLK_Pin, LOW);

  delay(10); // About 100 steps per second.
}

Why use limit switches? Since it's a stepper you can determine how far it goes. An easy solution might be to use grbl and feed it g-codes to move one way then the other at whatever speed you need. GRBL is a 3-axis CNC interpreter that takes g code and generates pulse streams for a stepper, taking account of acceleration and deceleration. g-code for this might be...
G1 X100 F100
G1 X0 F100
then repeat as often as needed.

Though 3-axis GRBL is quite happy with 1 or two. I have a little CNC controller box to run the power feed on my "big" mill, just needs 1 axis. It has 2 channels so I can use the other for a rotary axis, and also I use both channels for a coil winder I built.

But you still need at least 1 switch on each axis to "home" the axis on startup.

Not necessary. My cnc mill has home/limit switches on each axis but they never get used. Drive the motor to the point you want to be home and call it zero. I think if you jog the axis to a position then send G92 X0 it then calls that position zero. G10 may work too. Or you can send G91 which switches to incremental mode and then send G1 X+100 F100 and G1 X-100 F100.

john wasser; for your code what kind of limit switch do i need?

one with three contacts that required 5V, Ground and communication pin that goes to digital pin?

or can use simple two contact limit switch, One to ground and the other to digital pin?

Also can you give me the wiring diagram for your code TB6600 Arduino because there are two ways to wirre them.

Dir + and Puls + to the digital pints 2 & 3? and all the dir - and pulse - to the ground.

Because on the "ping pong" video in the above posts Dir + and puls + end up going to the ground.

Is how most people wire them. The internal pullup on the pin is enabled so that input is active low (closed switch = LOW). I put a 0.1uF cap across each switch (input to ground) to drain off noise picked up by the wire. You may need to reduce the pullup resistance value (stronger pullup) if wires are noisy and/or long.

There are those that espouse the active high limit switches, but I do not like running a hot (Vcc) wire all around.

ground fungus;
const byte TB6600_CW_Pin = 2;
const byte TB6600_CLK_Pin = 3;

so would pin 2 be Dir + and pin 3 Pulse +

as mentioned before the ping pong video wiring is almost opposite how these two boards are usually wired together.

Normally open, between pin and Ground.

Yes.

it worked john. much appreciated.
ive got tb6600 set to 16 micro step.

anyway in the coding to increase the speed of travel. its too slow. :slight_smile:

edit: is it a case of adjusting the delay as per below;

delay(10); // About 100 steps per second.

Yes.

Is there a way to put a time delay on only one of the limit switches?

So for example the process of the ping pong 1 x left and right occurs once every 15 minutes.

I assume you change the delay time on one of the switches on the code. I know i sound silly but I don't like messing with something that I am not concrete sure of lol

There are already separate delays at each end. They can be different values (or either can be eliminated completely).

  // Use digitlRead() to determine if you have reached the limit switch.
  // If you have, reverse direction.
  if (currentDirectionCW == HIGH && digitalRead(CW_Limit_Pin) == LOW)
  {
    // Hit CW limit while traveling CW
    currentDirectionCW = LOW; // Reverse to CCW
    delay(60000ul); // delay for 60 seconds at each end
  }

  if (currentDirectionCW == LOW && digitalRead(CCW_Limit_Pin) == LOW)
  {
    // Hit CCW limit while traveling CCW
    currentDirectionCW = HIGH; // Reverse to CW
    delay(60000ul); // delay for 60 seconds at each end
  }