Stepper Motor control with 1 limit switch

Hello everyone.
I'm new in this forum and I need your help! I have a basic understanding on Arduino programming and I need your advices.
I have a project that includes a stepper motor, a stepper driver TB6600 and one limit switch.
In the project, when the limit switch is closed I need to move the motor forward to specific steps and the second time the limit switch is closed I need the motor to move backwards to home position. I want this loop to be continuous.
Can someone in this forum give me a help.
Thank's

Hi, welcome to the forum.

When the project is powered on, how do you know where the stepper motor is ?

The Arduino Stepper library: https://www.arduino.cc/en/reference/stepper.
Start with this library.

The AccelStepper library: https://www.airspayce.com/mikem/arduino/AccelStepper/

A switch bounces, you need something to debounce the switch. There are a number of good libraries for that.

To detect the moment (the event) when a switch is closed, you need the State Change Detection: https://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection.

Some debounce libraries have also the State Change Detection included.

Can you make a first sketch ? Put it between code tags and show it.

Take a look into Arduino/reference section switch.... case. Let one case be adjuting forward and the other case be adjusting backwards.

So if I use a proximity sensor instead of a limit switch would that make a difference? I will make a sketch and I will show it to you. But all I need to know is how the controller can understand the previous rotation of the motor(forward/backwards) and then move at the opposite side. That's all I need I think.
Thank you very much for your quick replies.

You make the motor move forward or backward in your sketch. You can remember in a flag wether you made it run forward or backward.

1 Like

Can you please give me an example?
Thank you very much

Please show a picture of your setup and your sketch as it looks so far.
Is it the same switch that is closed at the beginning and end of the movement?

Yes it's the same switch.
I want to build an electric cutter like this
Test of 24mm Automated BoilieRoller set - YouTube.
Thank you

There is a similar tutorial: Arduino - stepper motor and limit switch