Turning 28BYJ-48 stepper motor

I was Following the project
https://create.arduino.cc/projecthub/debanshudas23/getting-started-with-stepper-motor-28byj-48-3de8c9?f=1#comments
on arduino project hub, and the code it gave to turn the motor is

#define A 2
#define B 3
#define C 4
#define D 5
 
#define NUMBER_OF_STEPS_PER_REV 512

void setup(){
pinMode(A,OUTPUT);
pinMode(B,OUTPUT);
pinMode(C,OUTPUT);
pinMode(D,OUTPUT);
}

void write(int a,int b,int c,int d){
digitalWrite(A,a);
digitalWrite(B,b);
digitalWrite(C,c);
digitalWrite(D,d);
}

void onestep(){
write(1,0,0,0);
delay(5);
write(1,1,0,0);
delay(5);
write(0,1,0,0);
delay(5);
write(0,1,1,0);
delay(5);
write(0,0,1,0);
delay(5);
write(0,0,1,1);
delay(5);
write(0,0,0,1);
delay(5);
write(1,0,0,1);
delay(5);
}

void loop(){
int i;
i=0;
while(i<NUMBER_OF_STEPS_PER_REV){
onestep();
i++;
}

I was able to make the motor go backwards by writing there onestep function backwards, and ill leave that at he bottom, but I am trying to figure out how to make it go forwards, delay, backwards. Everything that I try seems like its going forwards and backwards at the same time.
Please help me out

void backstep()
{
write(1,0,0,1);
delay(1);
write(0,0,0,1);
delay(1);
write(0,0,1,1);
delay(1);
write(0,0,1,0);
delay(1);
write(0,1,1,0);
delay(1);
write(0,1,0,0);
delay(1);
write(1,1,0,0);
delay(1);
write(1,0,0,0);
delay(1);
}

Where is your code (attempt) to go forward and backward? That way we can see where you might have gone wrong.

I have no experience with steppers but I think your attempt should look something like

void loop()
{
  int i;
  i = 0;
  while (i < NUMBER_OF_STEPS_PER_REV) {
    onestep();
    i++;
  }

  i=0;
  while (i < NUMBER_OF_STEPS_PER_REV) {
    backstep();
    i++;
  }
}

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.