28BYJ-48 stepper motor

hello again, i've started toying with a 28BYJ-48 stepper motor i bought off ebay, with a uln2003a controller and arduino uno. i started with some code i found on the forums here.

//Small Stepper Motor and Driver


/*-----( Import needed libraries )-----*/
#include <Stepper.h>

/*-----( Declare Constants, Pin Numbers )-----*/
#define STEPS  100   //Number of steps per revolution


/*-----( Declare objects )-----*/
/* create an instance of the stepper class, specifying
 the number of steps of the motor and the pins it's
 attached to. The pin connections need to be 4 pins connected
 to Motor Driver In1, In2, In3, In4  and then the pins entered
 here in the sequence 1-3-2-4 for proper sequencing*/

Stepper small_stepper(STEPS, 8, 10, 9, 11);

int  Steps2Take = 0;

void setup(){   
}
void loop()   
{
  small_stepper.setSpeed(100);
  Steps2Take  = 2038;                     // Rotate CW steps = 2038
  small_stepper.step(Steps2Take);
  delay(200);
}
/* --(end main loop )-- */

from all the info on the net i can find on peoples experiences and from data sheets, the motor has a

Reduction Ratio: 1/64
Step Torque Angle: 5.625°/64

i have a couple of questions about this code:

how is the steps2take value of 2038 calculated?
what is #define STEPS 100 doing?

i have also made some mods to this code so that the stepper will not move untill i press a button, i would like to make this movement exactly 1/6 a rev or 1/10 a rev is this possible with this code and this stepper motor?

i have changed Steps2Take to 339, this i got by dividing 2038 by 6 hoping this would be close enough to 1/6 a rev, but slowly it moves out due to it actually needing to move by 339.6 steps (i assume this isnt possible)

#include <Stepper.h>

const int stepsPerRevolution = 100;     // change this to fit the number of steps per revolution
const int buttonPin = 2;               // the number of the pushbutton pin
 
int  Steps2Take = 0;
int buttonState = 0;         // variable for reading the pushbutton status
 
Stepper small_stepper(stepsPerRevolution, 8, 10, 9, 11); 
 
void setup()
{   
  pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input
}
void loop()   
{
  buttonState = digitalRead(buttonPin); // read the state of the pushbutton value
  small_stepper.setSpeed(50);
  
  if (buttonState == HIGH) {
    Steps2Take  = 339;                  // Rotate CW steps = 339
    small_stepper.step(Steps2Take);
  
}
  else   
{ 
    (buttonState == LOW); {
      Steps2Take  = 0;                  // Stop moving
      small_stepper.step(Steps2Take);
       }
  
}
}
/* --(end main loop )-- */

here is what i have done, please let me know if there is a better way of doing this as im very new to all this XD

If you cannot divide the interval you want evenly into 360 degrees... Then you have to keep track of the running error in steps or parts of a step as a float. When it gets to 1 then you step 1 and subtract 1 from the error. I wish I had a simple example to show you, but it gets complicated when you combine it with other features in the code.

If you read the comments it always helps ...

STEPS is the number of steps for 1 revolution of the output shaft. for your motor and gearbox you have a 64 step motor with a 64:1 gearbox so you have 4096 Steps per rev. 4096 doesn't divide by 6 cleanly so you will have some error at any position with the denominator not a power of 2 (1/2, 1/4, 1/8, 1/16,...)

Steps2Take is the number of steps you wish to move.

Someone wanted to take 2038 steps.

To calculate and be very accurate you need to keep track of the current position, and calulate a destination position and subtract the current position from the destination position and the result is the steps to take. Your error will always be less than 1 step that way.

If you want to be as accurate as possible you will need to keep track of your absolute position, as the library is incremental, and calculate a destination position as a distance from zero and subtract eh current position from the destination position to come up with a number for an incremental move.

Thanks for the replys, i like the idea of + 1 step abd -1 step its a possible fix if i can work it out :slight_smile:
or keeping track of it might be even better, but for the minute both codes would be out of my league

Is there maybe a better motor I could use?

a few more questions about this motor, you say #steps is the amount of steps needed for a rev, but this is set on 100, it doesn't seem to be doing anything, also you calculated the number of steps lower down to be 4096, I asked how was 2038 calculated, but what I didn't add is 2038 is exactly 1 rev, as this isn't half of your calculated 4096 I am unsure how this is one rev.

I'm just trying to under stand :expressionless:

It is a good motor choice unless you need more speed. Off the top of my head 4096 is the number of steps / revolution.

If 2038 is exactly 1 revolution then some of the motor data you listed is wrong, you have things wired wrong, or you are doing something else with you program.

2038 makes no sense, as it is not divisible by 4. the 64 steps for the motor before the gearbox works out as 4 X 16. 2038 doesn't...

Because you used a sample program the NumberofSteps was set to 100. You need to change it to match your motor. What code from the sample did you delete? Maybe NumberOfSteps was used by the deleted code. Just because a number is in a sample program doesn't mean it will work for your situation. Your motor is 4096 steps per Rev. other common numbers are 72 and 200 steps per rev. And depending on gearing, the steps per rev for some system could be quite different, but it will be based on Motor Steps per Rev multiplied by some gear ratio.