28BYJ-48 Stepper Motor Interface



Assuming you have this type of stepper motor or any Bipolar stepper motor, and you want to drive it with your arduino board... Getting into troubles to let it rotate in 2 directions ? ( using arduino library) or its hard to maintain the angle of rotation ( using your own code ).. apart from wasting your time with trials and errors before starting to build your own project, you are wasting your effort by re-inventing the wheel. Here I introduce my library version of controlling such device in a fancy easy weasy way !!!

You only have to install the library code, set it up with your project and tadaa run it.






1.Blocking:

By blocking i meant while motor is rotating, all the system will freeze until the motor reaches its end destination


#include "Arduino.h"
#include <28byj48 .h="">

BlockingMotor motor(6, 7, 8, 9);


long f1(long x){
   return x*motor.steps_per_rev/360;
}

void setup(){
  Serial.begin(9600);

  motor.init();
  motor.set_time_between_pulses(2000); // recommended
  // set conversion function to convert
  // given argument (degrees) into steps
  motor.set_conversion_function(f1);

}

void loop(){
  motor.status = CW;
  motor.rotate_by(90);
  delay(1000);
  //
  motor.status = CCW;
  motor.rotate_by(90);
  delay(1000);
  //
  motor.status = CW;
  motor.rotate_by(180);
  delay(1000);
  //
  motor.status =CCW;
  motor.rotate_by(180);
  delay(1000);
  //

}


-----

2.Non-Blocking:

The system will work in async mode where the motor will be rotating while everything is operating ( no freezing )


#include "Arduino.h"
#include <28BYJ48.h>

NonBlockingMotor motor(6, 7, 8, 9);


long f1(long x){
   return x*motor.steps_per_rev/360;
}

void on_reached(long steps){
  Serial.println(steps);
}

void setup(){
  Serial.begin(9600);

  motor.init();
  motor.set_time_between_pulses(2000); // recommended
  // set the function that will convert degrees to
  // steps (OPTIONAL)
  // you can just provide steps
  motor.set_conversion_function(f1);
  motor.set_on_destination_reached(on_reached);
  int angle = 180 ;
  motor.set_destination(angle); // destination angle
  // Set the direction CW: clockwise CCW:anti-clockwise
  motor.status = CW;
}




void loop(){
  motor.update(); // commit rotation
}


---

Comments