CustomStepper - A library for stepper motors

Hey Folks,

I have had some trouble making my stepper motor work, that made me decide to make my own library for stepper motors, and here I come to share it with you guys.

The library is attached to this post, to install it, all you have to do is place it inside your libraries folder inside the Arduino IDE folder.

Take a look at the stepper example for explanation of how it works.

I've just added a new entry for it in the libraries page, check it here: Arduino Playground - CustomStepper

Please post any bugs or suggestions you have.

Hope you find it useful.

CustomStepper.zip (16.7 KB)

I read the first page of your link, looks great!
Why are you the only person with such strange gear ratios?
Everybody I have talked to uses 64:1 and it works as expected.
Can you include the link where you bought your stepper motors?
(so we can avoid them)

The problem with your library is the Floats. That must be slower.
My code can be configured to use integers only...

Looks great been looking for a stepper motor code like this for my project. Is it ok for me to use some of it?

I'm currently trying to motorise a chair for a simulator with a stepper motor and I am using a rotary encoder for feedback. What I am intending to is have the user control the rotation of a chair by using a USB joystick and the encoder will count the steps the motor takes and will prevent the chair from rotating if it reaches a limit in that direction. The user would either press the home button making the motor rotate back to a neutral position or twist the joystick in the opposite direction.

I have all the parts ready the only thing that is holding me back is my lack of knowledge in programming but I have been learning a lot from reading all the tutorials I can find and I have started playing around with codes to gain a greater understanding.

Is there a way of using the library with a stepper driver instead of inputting all of the pins individually?

I did some tests with the Custom Stepper Library and it worked.
At startup my motor turns 90 degrees to left and then 90 degrees to the right, which is correct according to the code.

What I want, is when I push button1 the motor turns 90 degrees to the left and when I press button2 , 90 degrees to the right.

This is the working code (example)

#include <CustomStepper.h>
CustomStepper stepper(8, 9, 10, 11);
boolean rotatedeg = false;

void setup()
{
  stepper.setRPM(12);
  stepper.setSPR(4075.7728395);
}

void loop()
{ 
  if (stepper.isDone() && rotatedeg == false)
  {
    stepper.setDirection(CW);
    stepper.rotateDegrees(360);
    rotatedeg = true;
  }
  stepper.run();
}

Here's my changed code which is not working (with the implementation of the 1 button)

#include <CustomStepper.h>
CustomStepper stepper(8, 9, 10, 11);
boolean rotate1 = false;
boolean rotatedeg = false;
boolean crotate = false;

const int buttonPin = 4;    
int buttonState = 0;       

void setup()
{
  pinMode(buttonPin, INPUT);
  stepper.setRPM(12);
  stepper.setSPR(4075.7728395);
}
  
void loop()
{
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH)
  {
    stepper.setDirection(CW);
    stepper.rotateDegrees(360);
    rotatedeg = true;     
    stepper.run();
  } 
}

Is there somebody facing the same problem, or can somebody help me with this?
Thank you

sbright33:
Why are you the only person with such strange gear ratios?
Everybody I have talked to uses 64:1 and it works as expected.

The 28BYJ-48 stepper motor has a normal gear train (gear trains are seldom powers of two, since the
people designing gear trains are not computer scientists!), and is only approxomately 64:1.
The gears in mine are 9:32, 11:22, 9:26, 10:31, giving a reduction ratio of ~63.684:1

Look at the picture halfway down this thread: http://forum.arduino.cc/index.php?topic=71964.15
which is the same as the ones I have from eBay.

Of course there may be other manufacturers with different gearings, the motor is made to control air-vanes
in car ventilation / air-conditioning systems.

This looks great. I wrote a similar library for controlling these motors but the only copy is on a hard drive in a container on board the APL Yangshen at the moment...

I got these motors for controlling gauges for flight simulator cockpits. The input is an angle relative to the datum; the library tracks the motor's position relative to the datum and moves the motor CW or CCW towards the commanded position.

For this functionality a feedback sensor is needed (a hall switch and magnet does fine); on startup the motor rotates until the sensor is tripped at a known position relative to the datum (determined by experimentation).

Would it be OK if I recreate this functionality into the CustomStepper library? Progress is slow - nearly all my tools and components are also on the good ship Yangshen.

Hi,

I was facing the weird gear ratios of the 28BJY-48, and was looking for libraries that would work with it, when I found yours! Thanks a lot for your work!

I tried installing and trying out your custom stepper example. I'm using a 12v 28BJY-48, and have it hooked up using a L293D bridge, so I figured the example would work as-is, with the exception of the pins. The input pins I'm specifying are the ones from the arduino connected to input1, 2, 3, 4 of the L293D.

I hear the stepper motor buzzing, but nothing happens! :frowning: Am I missing something? The only thing I can think of are the pin assignments - they work with the regular stepper library and the adafruit library, but with an annoying drift because of the gear ratio.

Thanks!
Sridhar

Dear darthwissen,

I just got my Arduino Uno and some stuff along. It made me feel more comfortable to use your library than using the official one.
To avoid duplicate variables,I preferred to modify your code and added "get" versions some of your "set" methods.
Do you plan to release newer versions? Might be adding such methods favorable?

Regards
Ozer

I wonder if the odd gear ratios are designed to provide even wear on the gear teeth?

...R

Hi, i was experimenting with the customstepper library & 28BYJ motor, and trying to make the motor rotate one rev. in CW direction, then one rev. in CCW. the code looks somthing like below . the problem is the first "rotate" command is being ignored, the motor goes just 1 rev in CCW then stops.
will appreciate your help understanding why this happens and how make it to work.
thanks!

void loop()
{
if ( stepper.isDone() &&rotate_end == false)
{
stepper.setDirection(CW);
stepper.rotate(1);
delay(5000); //probably not needed ??
stepper.setDirection(CCW);
stepper.rotate(1);
rotate_end = true;
}
stepper.run();
}

Greetings! Would like to use your library with a Big Easy Driver. Will it work with that board? If so - in the constructor, what pins should be STEP, and DIRECTION?

=Alan R>

I must say, thank you a lot, I had so much trouble controlling this stepper, I could not solve this without you, many thanks.

It appears this library is NOT for use with a driver board such as the Big Easy Driver.

I've gone over the code quite a bit and can not identify where the STEP and DIRECTION pins are set up. I'm getting the feeling Custom Stepper is for interfacing directly with driver transistors where each coil in the motor is handled separately by the software. There is no info in the header file to explain this.

If anybody HAS used this with the Big Easy Driver (or equivalent) would love to hear from you and what mods you had to make.

=Alan R.

Is there any way to set an exact speed and make the motor run continously, with no stop? I am building a telescope drive and would like to have these options.

How would the code look like if I wanted to run two step motors in sequence for an XY plotter and have them go in a diagonal?

Hello there,
I am new to this part of the forum, also to steppers and arduino. Hope someone here can help me out!
I have three stepper motors that need to be controlled separately using an Arduino Mega!

I have used h-bridges (three of them each for one stepper) to connect the steppers and Arduino. I used the following setup:

now, I checked the "Knob" example but was reading through the Blog "Custom Stepper" here, and was wondering if there would be another way for me to make the steppers go back and forth, accelerate and decelerate, but in a specific order? i do not want the code to read an analog value (from the pot) to move the steppers, but to move automatic.
any help would be appreciated.
I am not new to coding or electronics, but have never used the Arduino software nor have worked with steppers.

Thank you,
Karim.

I suggest you delete your post here and start a new Thread with it as I don't think your question has any connection to the rest of this Thread. You might read up about the AccelStepper library.

...R

I was having similar problems to others in this thread trying to use this library with buttons etc. The issue seems to be that you need to call at least one stepper function before you call run() or else code execution haults. I haven't looked at the library but more than likely there's some initalized value that's no good.

So in your setup() method, just add something small like:
stepper.rotateDegrees(1);

and everything should be fine.

ChristopheDV:
I did some tests with the Custom Stepper Library and it worked.
At startup my motor turns 90 degrees to left and then 90 degrees to the right, which is correct according to the code.

What I want, is when I push button1 the motor turns 90 degrees to the left and when I press button2 , 90 degrees to the right.

This is the working code (example)

#include <CustomStepper.h>

CustomStepper stepper(8, 9, 10, 11);
boolean rotatedeg = false;

void setup()
{
 stepper.setRPM(12);
 stepper.setSPR(4075.7728395);
}

void loop()
{
 if (stepper.isDone() && rotatedeg == false)
 {
   stepper.setDirection(CW);
   stepper.rotateDegrees(360);
   rotatedeg = true;
 }
 stepper.run();
}




Here's my changed code which is not working (with the implementation of the 1 button)



#include <CustomStepper.h>
CustomStepper stepper(8, 9, 10, 11);
boolean rotate1 = false;
boolean rotatedeg = false;
boolean crotate = false;

const int buttonPin = 4;    
int buttonState = 0;

void setup()
{
 pinMode(buttonPin, INPUT);
 stepper.setRPM(12);
 stepper.setSPR(4075.7728395);
}
 
void loop()
{
 buttonState = digitalRead(buttonPin);
 if (buttonState == HIGH)
 {
   stepper.setDirection(CW);
   stepper.rotateDegrees(360);
   rotatedeg = true;    
   stepper.run();
 }
}




Is there somebody facing the same problem, or can somebody help me with this?
Thank you

Hi guys!

I've just wrote this code and it works:

#include <CustomStepper.h>

CustomStepper stepper(8, 9, 10, 11, (byte[]){8, B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001}, 4075.7728395, 12, CW);
boolean rotate1 = false;
boolean rotatedeg = false;
boolean crotate = false;

void setup()
{
  
  stepper.setRPM(12);
  stepper.setSPR(4075.7728395);
  stepper.rotateDegrees(1);
  
  
}

void loop()
{
  if (stepper.isDone())
  {
    stepper.setDirection(CCW);
    stepper.rotate();
    crotate = true;
      
}
  stepper.run();
}

But if I put some Serial operations like this:

...
void loop()
{
  if (stepper.isDone())
  {
    Serial.println("A");
    stepper.setDirection(CCW);
    stepper.rotate();
    crotate = true;
      
}

stepper.run();

}

All falls apart. I mean - stepper motor is not rotating at all and only one diode is turned on on controller - A.
Serial communication is priority to me.

I'm using Arduino MEGA2560 R3 with 28BYJ-48 stepper motor and controller based on ULN2003APG

Any kind of ideas are welcome :slight_smile:

Thanks :slight_smile:

Cheers!