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 )
9 Comments
2 years ago
great work!
7 years ago
...\Arduino\ULNStepper\ULNStepper.ino:2:26: fatal error: StepperMotor.h: No such file or directory
#include <StepperMotor.h>
Keeps coming up.
Reply 4 years ago
I think the library was not exist in directory
Reply 6 years ago
same here..
5 years ago
Amazing Work !!!
6 years ago
Works great! Ist there a possibility to let it turn faster then 1 step per microsecond? If I understand that correct the library only understands integers. How can I change that?
7 years ago
I'm missing something here.
What should I be using to create the .h and .cpp files?
How should I bundle this together to create a library that will install in the IDE?
I have so far made two notepad docs that when labelled with the suffixes appear the same as any other library I have. I have attempted to insert these docs into a main file, placed them into another file inside the main, had them in the same level as the test code and juggled bits of this in a few iterations.
Please help.
7 years ago
Very nice information! I finally got this stepper to run. I'd like to use buttons to control the speed/rpm. What value have to be changed to make it go faster? I can make it go slower by changing the value in motor.setStepDuration(1) but what about faster?
7 years ago on Step 2
Brilliant, excellent, helpful; thank you!