Introduction: Arduino Library for 28BYJ-48 Stepper Motor and ULN2003 Driver

The 28BYJ-48 stepper motor with the ULN2003 driver now seems quite a commonplace configuration. The motor and driver are both readily available, and cheap. However, the standard arduino stepper motor library just doesn’t work with this configuration! Apparently something to do with the sequence required for rotation of the 28BYJ-48.

This instructable will show you how to write a simple library that will make life simpler for future uses of the 28BYJ-48. I have seen a couple of possible solutions (even writing out the pulse sequence over and over again), but I haven't been satisfied, so I decided to write my own.

We assume that you have a basic understanding of C++ and stepper motor theory.

You can get the code and an implementation from the EngyFun blog at:

Step 1: StepperMotor.h

Let's begin by writing the StepperMotor class.

Create a file called StepperMotor.h and copy the code in the image.

If you do have an understanding of C++, then the class definition will speak for itself.

Step 2: StepperMotor.cpp

Now let's write the class interface.

Create a file called StepperMotor.cpp, and copy the code in the images.

Let's look at the constructor on line 4. We start by assigning the user selected pins to the input pins array on lines 6 to 9. This will make it easier to access the pin numbers within the driving algorithm.

On line 12, we iterate through the input pin array, and set each pin to OUTPUT mode.

On line 15, we default the step duration to 50 ms.

On line 18 we have the step duration setter. This is self-explanatory.

Now let's look at the step method. This allows the stepper motor to step the number of times passed to the method.

On line 28, we define the rotation sequence using a 2d array. The rows represent a step and the columns represent the output pins.

On line 37 we calculate factor, which will be +1 or -1, depending on the sign of the passed number of steps. This value is required in the algorithm to direct the direction of iteration through the sequence array, i.e. to change the direction of rotation.

On line 38, we make noOfSteps positive, required for the design.

On line 44, we begin a loop that will run for each start of a rotation sequence, i.e. at the start of every 8 steps.

On line 45, we begin another loop that iterates through the rows of the sequence array.

On line 46, we delay as duration specifies.

On line 47, we iterate through the pin numbers.

On line 48, we write the digital signal to the current pin number.

If factor is negative, the sequence array's rows are accessed in the opposite direction on line 48 when we write to the pins. 8 is subtracted by the count of the row... so we access from bottom to top.

Step 3: Accessing the Library From the Arduino IDE

Now we just need to add these files to the arduino IDE's library directory. This will allows us to import that library within the IDE for use.

Go to the following directory:

C:\Program Files (x86)\Arduino\libraries

and then create a folder called StepperMotor.

Now put the .h and .cpp files in the created folder.

You can now import the library from within the IDE. ( sketch > import library... > StepperMotor )