Here is the source code for our 28BYJ48 stepper motor, ULN2003 driver library, as well as an implementation. Check out the diagram for the wiring.
The header file:
__________________________________________________________________
#ifndef Stepper_h
#define Stepper_h
class StepperMotor {
public:
StepperMotor(int In1, int In2, int In3, int In4); // Constructor that will set the inputs
void setStepDuration(int duration); // Function used to set the step duration in ms
void step(int noOfSteps); // Step a certain number of steps. + for one way and - for the other
int duration; // Step duration in ms
int inputPins[4]; // The input pin numbers
};
#endif
___________________________________________________________________
The cpp file:
___________________________________________________________________
#include <Arduino.h>
#include <StepperMotor.h>
StepperMotor::StepperMotor(int In1, int In2, int In3, int In4){
// Record pin numbers in the inputPins array
this->inputPins[0] = In1;
this->inputPins[1] = In2;
this->inputPins[2] = In3;
this->inputPins[3] = In4;
// Iterate through the inputPins array, setting each one to output mode
for(int inputCount = 0; inputCount < 4; inputCount++){
pinMode(this->inputPins[inputCount], OUTPUT);
}
duration = 50;
}
void StepperMotor::setStepDuration(int duration){
this->duration = duration;
}
void StepperMotor::step(int noOfSteps){
/*
The following 2D array represents the sequence that must be
used to acheive rotation. The rows correspond to each step, and
the columns correspond to each input. L
*/
bool sequence[][4] = {{LOW, LOW, LOW, HIGH },
{LOW, LOW, HIGH, HIGH},
{LOW, LOW, HIGH, LOW },
{LOW, HIGH, HIGH, LOW},
{LOW, HIGH, LOW, LOW },
{HIGH, HIGH, LOW, LOW},
{HIGH, LOW, LOW, LOW },
{HIGH, LOW, LOW, HIGH}};
int factor = abs(noOfSteps) / noOfSteps; // If noOfSteps is +, factor = 1. If noOfSteps is -, factor = -1
noOfSteps = abs(noOfSteps); // If noOfSteps was in fact negative, make positive for future operations
/*
The following algorithm runs through the sequence the specified number
of times
*/
for(int sequenceNum = 0; sequenceNum <= noOfSteps/8; sequenceNum++){
for(int position = 0; ( position < 8 ) && ( position < ( noOfSteps - sequenceNum*8 )); position++){
delay(duration);
for(int inputCount = 0; inputCount < 4; inputCount++){
digitalWrite(this->inputPins[inputCount], sequence[(int)(3.5 - (3.5*factor) + (factor*position))][inputCount]);
}
}
}
}
_______________________________________________________________________
An implementation:
_______________________________________________________________________
#include <StepperMotor.h>
StepperMotor motor(8,9,10,11);
void setup(){
Serial.begin(9600);
motor.setStepDuration(1);
}
void loop(){
motor.step(1000);
delay(2000);
motor.step(-1000);
delay(2000);
}
_________________________________________________________________________
Wiring diagram:
Thanks, this was just what I was looking for to drive a stepper/driver combination I picked up from MPJA. Code works fine, as long as the number of steps is evenly divisible by 8, but stepping is intermittently incorrect otherwise, at least on my hardware.
ReplyDeleteWow. This is the fourth source for driver codes to my stepper.
ReplyDeleteAnd it is by far the simplest too...
Cheers!
Morten, Denmark.
Awesome, only library I tried that is simple to use and works with this stepper!
ReplyDeletethank you even I can understand it!
ReplyDeleteHow would the code go if you wanted to run the same design to 2 stepper motors?
thank you even I can understand it!
ReplyDeleteHow would the code go if you wanted to run the same design to 2 stepper motors?
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHow do I add this to my arduino library
ReplyDeleteI would like to know how to add this to my library too!
ReplyDeletethere's a library that works well here:
ReplyDeletehttp://forum.arduino.cc/index.php?topic=422336.0
When I upload to my uno board I get the following error:
ReplyDeleteStepperMotor.cpp:54: error: '_______________________________________________________________________' does not name a type
_______________________________________________________________________
^
/home/moonstroller/Downloads/arduino-1.6.12/libraries/Stepper/src/StepperMotor.cpp: In function 'void setup()':
StepperMotor.cpp:66: error: 'motor' was not declared in this scope
motor.setStepDuration(1);
^
/home/moonstroller/Downloads/arduino-1.6.12/libraries/Stepper/src/StepperMotor.cpp: In function 'void loop()':
StepperMotor.cpp:70: error: 'motor' was not declared in this scope
motor.step(1000);
^
exit status 1
'_______________________________________________________________________' does not name a type
I copied and pasted the code from StepperMotor.h and cpp to their appropiate files and placed them in the appropiate place. Any ideas?
Is this in a source repository somewhere? I'd love to link to it in my project.
ReplyDeleteVery useful, thank you! Worked perfectly w/ my Uno R3. For those who need some extra help getting this up and running, the Instructables article has more details: http://www.instructables.com/id/Arduino-Library-for-28BYJ-48-Stepper-Motor-and-ULN/?ALLSTEPS
ReplyDeletehi i want that step motor do a half turn to the right and go back to the middle and do a half turn to the left and go back to the middle and repeat all that every 2 hours
ReplyDeleteare that possible if yes can anyone give me the code please i try but i Failed thx
This comment has been removed by the author.
DeleteNothing could be simpler!
Delete#include
Stepper myStepper(2048,11,9,10,8);//your numeration may be other than my
void setup() {
myStepper.setSpeed(16);
}
void loop() {
myStepper.step(1024);//half turn clockwise
delay(500); //delay - you can remove it
myStepper.step(-1024);//half turn anticlockwise (return to 0)
delay(500); //delay - you can remove it
myStepper.step(-1024);//half turn anticlockwise
delay(500); //delay - you can remove it
myStepper.step(1024);//half turn clockwise (return to 0)
delay(7200000); //delay 2 hours (1ms*1000*3600s*2h)
}
This site not include some symbols then:
Delete#include <"Stepper.h">
Remove the quotation marks themselves :)
this does not work boo!
ReplyDeleteThis works great! Best code for this combo of ULN2003 and ROHS 28BYJ48 that I've found.
ReplyDeleteBut I would like to have speed control, too.
PBH 2 Phase and 3 Phase stepper motors and stepper motor drives. Our latest product is Closed Loop Stepper Drive that is Stepper Motor with encoder.
ReplyDelete