Accel stepper library with support for 28byj-48 motor

Not sure if this is the right place but I modified AccelStepper library by Mike McCaulay to work with 28byj-48 motors.
To use it simply define the motor in sketch as follows AccelStepper stepper(5, pin1, pin2, pin3, pin4); Where ‘5’ is the interface type for the 28byj-48 motor. The pins are just whatever pins you want to use. The library didn’t use number 5 at all so it has its full functionality plus support for 28byj-48 motor.

In case you’ve never used the original library it is pretty awesome as it allows adding more than one stepper and supports acceleration. So now you have it - you can drive more than one 28byj-48 motor.

Here is an example code I am using in the video:

#include <AccelStepper.h>

// Define some steppers and the pins the will use
AccelStepper stepper1(5, 2, 3, 4, 5);
// The first argument '5' indicates that we're using a 28byj-48 geared motor. The rest is just pin numbers. 
//You can still use all the other types of motors supported by accelstepper library (e.g. 4 for a normal 4 wire step motor, 8 for a halfstepped normal 4 wire motor etc.) 
AccelStepper stepper2(5, 6, 7, 8, 9);

void setup()
  stepper1.setMaxSpeed(900.0); //max speed of the first motor - modify if you want to
  stepper1.setAcceleration(700.0); // rate at which the first motor accelerate -



void loop()

  if (stepper1.distanceToGo() == 0){
    stepper1.moveTo(random(1500,7000));//just an easy way to get the motors to move to random positions
  if (stepper2.distanceToGo() == 0){


What do you think?

Mind you that you have to be logged in to see the attachments and download the modified library. Otherwise you can’t even see that the files are here.
To install just copy and paste the two files into AccelStepper library folder and they will replace the original files ( you need both AccelStepper.cpp and AccelStepper.h ).

AccelStepper.cpp (14.6 KB)

AccelStepper.h (32.9 KB)

That does make it easy to use! But it doesn't have all the features that I listed in my code...

Thanks. Well I got to admit that it doesn't have all the features of your code but I guess you could expand on it. The library is very well structured from the ground up, which makes it easy to follow and expand. What's more, you can now use different types of motors in one project, so nothing stops you from using for example 2 regular step motors with a 28byj-48 motor etc.

Hi Kerimil,

I have a question for you. Do you have any trouble running this motor in reverse using your modified Accelstepper library? I can only get mine to move forward, then it stalls out when it should run in reverse until it's time to move forward again. I noticed that your code is only calling the motor to move in positive (greater than 0) positions, so it moves in reverse because it (for instance) is moving from 7000 to 2000. I'm using the following basic code, and it won't work in reverse for me:


AccelStepper stepper3(5, 10, 11,12, 13); //this is the ship rotation motor that is controlled by encoder, the '5' signifies that the motor is a 28byj-48 8-step motor. This uses the modified accelstepper library

void setup() { stepper3.setMaxSpeed(150.0); stepper3.setAcceleration(300.0); stepper3.moveTo(768); }

void loop() { // Change direction at the limits if (stepper3.distanceToGo () == 0) stepper3.moveTo(-stepper3.currentPosition());; }

Any thoughts on why this doesn't work? It works with other motors and the original Accelstepper library.

Thanks in advance! Atman

hmm no idea really... uploaded your code and it works just fine.

I see two possible options 1. Try lowering speed and acceleration as the motors move better in one direction that the other so my only guess is that it is stalling. 2. Your wiring is incorrect

Glad to hear it works for you. I'll check out my wiring. I had my speed and acceleration set pretty low, so I doubt that's the problem.

Thanks! Atman

usually I run it much faster and it works fine

Try to remove AccelStepper library and import it again with new files you downloaded here :)

good idea expanding the library, these steppers are cheap and great for experimenting

what version of the library did you change? and what exactly did you change? and why? what was missing in the library? afaik the 28byc is just a stepper with a gearbox attached, and it works with the standard library.

i am a bit new to steppers, so please excuse me if I overlook something obvious. i downloaded version 1.39

They need an 8 step sequence. I used the newest version I could find

hi again ...

i delved a bit deeper myself, as you provide very little information the version you used is 1.37 and it has a little bug. (which is fixed in the latest version, currently 1.39)

i still do not fully understand why you need a 8 step sequence to make the stepper run. the original stepper library manages to do so nicely with 4 step sequence. I am going to investigate further, and I will let you know.

just as part as my learning process, to really understand how steppers work, and how this library can be used

ah - got it ...

nothing wrong with the original library

pins 2 and 3 need to be swapped, than both FULL4WIRE and HALF4WIRE work as expected so - define a stepper as follows: AccelStepper byj(AccelStepper::HALF4WIRE, 8, 10, 9, 11); // byj - pins 2 and 3 swapped !!! (use HALF4WIRE for half stepping)

arduino pin: ULN2003A IN pin out pin color code on the byj stepper 8 in4 D blue 9 in3 C pink 10 in2 B yellow 11 in1 A orange +5v vcc red

sorry - don't know how to make a pretty table, hope this clarifies things

That's what the datasheet says. IIRC sbbright mentioned it in his example code.

kerimil: To install just copy and paste the two files into AccelStepper library folder and they will replace the original files ( you need both AccelStepper.cpp and AccelStepper.h ). ;)

can you now add it to the control of the two motors with a joystick? I need exactly the same kit but we should control joystick .. I do not know programming, so I need some help with this .. thx!

why cannot use ?

If you indent your code consistently it is easier to see the problem

void loop()
       // Change direction at the limits
    if (stepper3.distanceToGo () == 0) {

The command is what makes the motor move and it must be called more often than the motor needs to take steps. It does not implement the entire move with one call. For that you need the blocking function runToPosition()

The way your code is written, as soon as you set the distance to 1700 you set it back to 0.

Try this variation and see what happens

void loop()
       // Change direction at the limits
    if (stepper3.distanceToGo () == 0) {