Stepper motor losing its position

This is the part of the project, controlling 2 axis CNC with a touch screen.

i am trying to use pot to give input to the stepper motor for its positioning of one of its axis, using a pot for testing purposes to replicate a touchscreen

the code works as it is supposed to and stepper motor sits in its position whenever i rotate the knob of the pot slowly.

but when i rotate it a bit faster the positioning is lost, ie. whenever knob is at lowest position the stepper does not return to the original position

i know the stepper motor takes time to reach its position and it cant keep up with the knob position, but atleast the motor should not lose its position right?? what part of code do i have to edit ??

#include <Stepper.h>

int xpos = 0;
int xinput;
int steps= 0;

Stepper myStepper(steps, 8,9,10,11);
void setup()
{  
  myStepper.setSpeed(60);
   Serial.begin(9600);
}


void loop()
{
  xinput=analogRead(A0);
  steps=xinput-xpos;
  if(steps!=0)
  {
  myStepper.step(steps);
  }
  xpos=xinput;
  
}

I cannot see anything wrong with the code you have written.

It is possible that turning the knob fast causes spurious readings from the pot, but even then, your code should still work.

Is there some limit on the number which the function argument "steps" can be ?

michinyon i think you are right

the motor stalls when ever the steps go above 500 for 60 RPM

also the motor stalls for 500 steps at 120RPM but it does not stall at 60RPM :~

what can i do to overcome this?? do i have to change something in the stepper library to make it work?

It sounds like the stepper can't actually do 60 RPM. When you ask it to move a few steps that isn't tested. But when you ask it to move by a large enough increment the limitation appears. Try lower speeds to see if the problem goes away.

I haven't used it, but you might look at the accelstepper library which includes the ability to ramp speed up and down.

How are you powering the stepper motor? What stepper driver board are you using? What voltage is the power supply?

...R

Steppers cannot accelerate instantly - particularly here as you have significant driven mass.

AccelStepper, not Stepper, is the library to use, as you can set the max speed and max acceleration.

If you set the acceleration conservatively then you can experiment to find the max speed for reliable operation, then up the acceleration until it miss-steps and back-off 20% or so.

Miss-stepping can be caused by overload (the motor cannot pull hard enough for the load / acceleration), or by resonance. Mechanical load will both affect the resonant frequency and add some damping, so you can only tune a stepper under load. Micro-stepping drivers reduce the amplitude of resonance.

I am currently testing it so there is no load attached to the stepper motor so i guess i would run into more problems when i add the load

It is a small second hand 4 wire stepper and im powering it up with 9V adapter of 1 Amp rating, i have made my own driver with an l293d

have to look into accelstepper and see how to make it work with my code, thank you :)

It is a small second hand 4 wire stepper and im powering it up with 9V adapter of 1 Amp rating, i have made my own driver with an l293d

How small? 1 Amp may be nowhere near enough current. 9V sounds low, too.

i am attaching an image for its size, also using 12v from a battery is causing it to skip steps so i think 9v is sufficient…

Have you measured the resistance of the motor coils?

The problem with using an L293 for a driver is that you have no method of limiting the current through the motor. If you use a proper stepper motor driver (see, for example the Pololu A4988) you can limit the current and drive it with a much higher voltage for better high speed performance.

...R

i am attaching an image for its size

Same approximate size as the ones I have. 9V (or 12V) and 1A is NOT sufficient, nor, as Robin2 points out, is your method of driving them.

Does the motor have a part number? Have you measured the winding resistance? bipolar steppers come in two categories, low-impedance designed for constant-current drive and high-resistance designed for fixed-voltage drive.