How to get a stepper motor to turn x steps and return to original position?

Hi. This is my first major Arduino project and I'm not that experienced with coding yet. I have a stepper motor and a sensor connected to an Arduino Uno. I'm trying to get the stepper motor to run for as long as the sensor reads above a certain value, and then return to it's original position when the sensor reading drops below. I'm thinking of somehow recording the number of steps turned as a function of time, as the motor speed will be constant, but I don't know how to set this up. I'm also not sure if this is the most efficient way to do so. Since I'm still new to Arduino, I don't know much about what functions to use. Does anyone know how to accomplish this? I'm controlling a NEMA 17 stepper motor by a L298N driver if that helps. Anything is appreciated, thanks!

Xingyu_Zhao:
Hi. This is my first major Arduino project and I'm not that experienced with coding yet. I have a stepper motor and a sensor connected to an Arduino Uno. I'm trying to get the stepper motor to run for as long as the sensor reads above a certain value, and then return to it's original position when the sensor reading drops below. I'm thinking of somehow recording the number of steps turned as a function of time, as the motor speed will be constant, but I don't know how to set this up. I'm also not sure if this is the most efficient way to do so. Since I'm still new to Arduino, I don't know much about what functions to use. Does anyone know how to accomplish this? I'm controlling a NEMA 17 stepper motor by a L298N driver if that helps. Anything is appreciated, thanks!

Welcome to the Arduino forum. I am sure you will enjoy building things controlled by your Arduino.

But first, some real world education is necessary.

First, your stepper motor cannot ever have a constant speed. It must start at ZERO speed and move one step at a time. If the steps are close enough together in time, the movement will be continuous, but will still be impulsed a bit for each step.

Second, the original position cannot be remembered by the Arduino. Consider the motor runs 1,256 steps and stops. Where is the original position? Are you wanting the motor to move in the opposite direction until the steps are back to zero? Most people force their stepper motor to move to a known position when first starting their program. Some sensor or switch that is activated by the stepper movement will tell your Arduino the home position has been reached. Then you know the ZERO position to begin counting motor steps.

Even with the home position switch, there will be some computation of the number of steps to take and which direction to move the stepper in order to get back to it's home position. Unless you just force the stepper motor to continue turning until the home position switch/sensor is reached.

Good luck with your project.

Paul

Xingyu_Zhao:
I'm trying to get the stepper motor to run for as long as the sensor reads above a certain value, and then return to it's original position when the sensor reading drops belowdriver if that helps. Anything is appreciated, thanks!

You don't say whether the motor must always start from a specific position. If so then you need to identify that position every time the Arduino starts. That is usually done by driving the motor step by step until it triggers a limit switch. The Arduino then knows it is at the HOME or ZERO position.

After that if you use a variable to keep count of the number of steps you can always get back to the zero position.

...R
Stepper Motor Basics
Simple Stepper Code

You don't say if the motor will make a movement of more than 360 degrees.

If it moves from (say) 0 to 270 degrees then a micro-switch activated by something on the axis of the stepper motor would let you know when it gets back to its original position.