Stepper motor control

I just bought the following stepper motor and im running the following code for it. It seems like by adjusting the delaymicroseconds on lines 12/14 adjusts the speed of the stepper motor. Is this the proper way of adjusting the speed of the stepper motor?

How would i go about making the stepper motor rotate 1.60463 RPM? Not sure how accurate you can make the stepper motor but from from calculations thats the speed i need it to be for what i need it for.

Also, the distance on line 18 i have set at 1600 for 1 full rotation for the stepper motor. But that is just to the eye what seems to be one full rotation. How do i know what value to input into there for an accurate 1 full rotation?

Thanks,

Stepper motor

int Distance = 0;  // Record the number of steps we've taken

void setup() {                
  pinMode(8, OUTPUT);     
  pinMode(9, OUTPUT);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
}

void loop() {
  digitalWrite(9, HIGH);
  delayMicroseconds(70); //70 minimal         
  digitalWrite(9, LOW); 
  delayMicroseconds(70);  //70 minimal
  Distance = Distance + 1;   // record this step
  
  // Check to see if we are at the end of our move
  if (Distance == 32000)
  {
    // We are! Reverse direction (invert DIR signal)
    if (digitalRead(8) == LOW)
    {
      digitalWrite(8, HIGH);
    }
    else
    {
      digitalWrite(8, LOW);
    }
    // Reset our distance back to zero since we're
    // starting a new move
    Distance = 0;
    // Now pause for half a second
    delay(500);
  }
}

The number of steps for one rotation is set by the motor mechanics you need to find the figure in the motor's data sheet. They are normally 200 steps per revolution and you might be half or quarter stepping it.

Is this the right way? Well what I would do is to have an interrupt called step routine driven off a timer. You can only get the motor's speed as accurate as you can set the timer or the delays.

This is the data sheet for the stepper motor. And pretty sure i've read somewhere that it is 200 steps per revolution

data sheet

You read it in the 'datasheet' you just linked:

step angle 1.8°

360/1.8 = 200

:wink:

Yes that quick math would surely make sense lol. I've also tried to run the following code that's in the arduino examples and when i set the speed to a certain value say 10 for when it says "mystepper.setspeed(x)" and put in 10 it doesn't spin 10 RPM, but incredibly slowly and kinda jittery. Am i missing something?

Thanks,

#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, 9);

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(10);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(100);
}

If you want to experiment with speeds this simple stepper code should make it easy. Note that there is a very short pulse and a number of millisecs between steps. The second example that does not use the delay() function would be more appropriate for a real project, but the first example makes testing simple.

You need to figure out how many steps you need per second. For 1.064 rpm that would be
200 * 1.064 / 60 or 3.5467 and that requires 1 step every 1000 / 3.5467 or 281.9 millisecs

You could probably do the control using microsecs for finer control.

As @Grumpy_Mike said you may get better control using one of the internal timers.

Your requirement for 1.06463 rpm suggests a required accuracy of 1 part in 100,000. Is that really necessary?
Where did that strange number come from?

...R

Where did that strange number come from?

Judging by his other posts he is working on a star tracker so it has to keep sidereal time.

Judging by his other posts he is working on a star tracker so it has to keep sidereal time.

Precisely. From using the following formula I need the stepper motor RPM to be about 1.6RPM for my star tracker.

Radius (in inches) = RPM / ((2π/ 1436) * TPI)

in your post, you use delay(100) for a stepper, that is sorta like telling a runner to run 5 steps, then delay about 2 seconds..... it will be hugely noticeable in cogging.

also, get rid of the serial print. that too is like stopping for a huge pause.

very interesting formula. I am not very good at the math for tracking, but using time in a formula to calculate distance eludes me.

also, you did not mention what pitch your thread is on, I am assuming a worm gear ?

from what I have figured, is that when using a worm gear with a thread, you need to know how many rotations of the worm to make the gear rotate 1 revolution.

circumference is important and will be what is used for the calculations. radius is only used as a way to solve for circumference.

((# rotations of the worm for 1 gear revolution) x motor steps per worm rotation ) x microsteps )

What I think you want to know is how many steps per minute.
once you know how many micro-steps per minute, you can work on the pulse generator to accomplish that rate.

you can increase the diameter of the final gear,
increase the micro steps
increase the TPI

all of which will help increase resolution.

I got the formula from some other website for barn door star trackers. And since the only variable that i need to determine now is the RPM just solved for that instead and calculated it to be pretty much 1.6RPM. Since im not using my DC geared motor anymore im not using a gear system anymore so just connecting the stepper motor directly to a threaded rod to move the platform my camera sits on.

Radius (in inches) = RPM / ((2π/ 1436) * TPI)

21.375(inches) = RPM/((2PI/1436)*18 TPI) >> 1.68RPM.

And still trying to figure out how to exactly get the stepper motor to turn 1.68RPM but any time i input values for it never turns at that rate.

dave-in-nj:
using time in a formula to calculate distance eludes me.

Knowing that you're moving at 10ms-1, how far will you travel in 2s ?

I'm sure you know the answer - but more to the point, looking at a unit analysis:

m/s * s = m

...you've figured out a distance using a time.

Indicating there is a velocity implicit in the equation :wink:

And still trying to figure out how to exactly get the stepper motor to turn 1.68RPM but any time i input values for it never turns at that rate.

No it won't look at the library code that uses an int for the variable and you have a float.

1.68 RPM = 1.68 / 60 = 0.028 revolutions per second

so one revolution should take 1 / 0.028 = 35.7142857143 seconds

200 steps per revolution so each step should take

35.7142857143 / 200 = 0.17857142857 seconds

It is quite a tall order to be that precise given that the Arduino clock works at 16MHz or 62.5 nS per clock pulse. Therefore between steps you need to have 2971428.5712 clock cycles between steps this will have to be 2971428 clocks as you can't get half a step.

Eurotrax:
I got the formula from some other website for barn door star trackers. And since the only variable that i need to determine now is the RPM just solved for that instead and calculated it to be pretty much 1.6RPM. Since im not using my DC geared motor anymore im not using a gear system anymore so just connecting the stepper motor directly to a threaded rod to move the platform my camera sits on.

Radius (in inches) = RPM / ((2π/ 1436) * TPI)

21.375(inches) = RPM/((2PI/1436)*18 TPI) >> 1.68RPM.

And still trying to figure out how to exactly get the stepper motor to turn 1.68RPM but any time i input values for it never turns at that rate.

use a different formula. you have 21.375 inches = 1.68 RPM. that formula does not get you to your goal.

steps per rotation, that would be stepper motor natural steps times micro-steps. micro-steps are added by the stepper driver.

threaded rod revolutions per inch. you listed 18.

if your motor is 200 steps and you are doing 8th step, then you get 1,600 steps per revolution of the screw.
times 18 to get number of screw turns per inch.
calculate how many inces per hour or minute you need to get a value that you need. steps per minute.
once you know steps per minute, you can solve for that in your software.

remember that your barn door is not linear to the rotation and it will be off. your drive angle changes, ergo your rotation is altered by changing dimensions of the sides of the drive mechanism. that is the basis of the design and the window of accuracy is fleeting. good enough for a photo time lapse of x seconds, not not over long periods.

the options are to move the threaded rod to get a more accurate number or adjust for it in software.

if you come up with 87.26895 steps per minute, you can calcuate the closest step rate you can, then, every 3 or 7 or 10 steps, add a step or skip a step. or change the rate.

if you want to use your arduino to extend the duration of tracking, you can do that as well, once you get the correct formula.

Grumpy_Mike:
No it won't look at the library code that uses an int for the variable and you have a float.

1.68 RPM = 1.68 / 60 = 0.028 revolutions per second

1.68 rpm = (200 motor steps * 8 th steps ) // assuming a micro-stepper for 8th steps
1.68 * 1,600 steps = 2688 steps per minute
44.8 steps per second.

assuming 1.68 rpm is correct.

1:1:
Knowing that you're moving at 10ms-1, how far will you travel in 2s ?

I'm sure you know the answer - but more to the point, looking at a unit analysis:

m/s * s = m

...you've figured out a distance using a time.

Indicating there is a velocity implicit in the equation :wink:

not missing the point, the formula is to figure out the distance between the hinge and the screw.
if you motor has a fixed RPM, such a gear motor, and you use 2i()/radians, and the screw thread, your result is the proper distance in which to mount the screw from the hinge.

this formuala is used as the basis of locating the physical points for the machine.

you had to alter it and create things that are implied in order to try to make it fit.

this formula is not used for calculating motor speed. or steps per minute or pulses per second. I was hoping the OP would dig a little deeper into the web pages he was getting the designs in order to have that eureka moment where he realizes how simple the answer is.

I've passed exams using unit analysis on the supplied formula sheet only. It's one of the tactics you can resort to when you have next to no idea about the subject :slight_smile:

I think I'm doing the same here - and perhaps making to general a comment...

Outside of this, it appears you know the subject well !

I note however that in the variables you state I see RPM and pitch - the units are adding up again ! heh heh

it was a simple XY problem and we should have caught out of the gate.

once you ignore the old formula and figure out the right one, things will get very clear, very fast.

also, the basis of design of the barn door is a fixed gear motor and a predicted acceleration of error over time.

with the arduino and software, you can reduce that error to near infinitesimal proportions by continuously adjusting pulse rate.

you had to alter it and create things that are implied in order to try to make it fit.

this formula is not used for calculating motor speed. or steps per minute or pulses per second. I was hoping the OP would dig a little deeper into the web pages he was getting the designs in order to have that eureka moment where he realizes how simple the answer is.

Since I've built my tracker mount and have the radius that the drive rod is at, that's why i manipulated the formula for the 'RPM' for my tracker.

Also i am making an isosceles tracker mount. I intend to only take pictures that are a few minutes long, nothing on the extreme end like 20 minutes since i live near the city so i shouldnt have to worry about any tangental errors with a 5 minute exposure.

I think GrumpyMike is thinking along the way I'd go about it...

considering the hardware I'd be using would only reasonably be able to do half-stepping...

so I'd start by saving the current time from Millis() or Micros(), and calculating (adding) the number of microseconds until the next step, then in the loop() I'd keep rechecking to see if that time has elapsed, if it has, I'd step the motor, and add to the value again.. there may be slight variations from step to step, but it should be pretty accurate over any period of time.

Eurotrax:
Since I've built my tracker mount and have the radius that the drive rod is at, that's why i manipulated the formula for the 'RPM' for my tracker.

Barn door tracker - Wikipedia

Also i am making an isosceles tracker mount. I intend to only take pictures that are a few minutes long, nothing on the extreme end like 20 minutes since i live near the city so i shouldnt have to worry about any tangental errors with a 5 minute exposure.

glad you figured out the details.
once you have taken a few pics, you should come back and post them !
always happy to hear when a project comes together.