Help!!! how to do that (28BYJ-48 Stepper Motor )

hi

i have 28BYJ-48 Stepper Motor and arduino mega 2560

i don't know how to switch the stepper motor rotation. :frowning: :frowning: :frowning:

i hope to do this ---> The Stand Alone Stepper Motor Driver Module With Arduino Contol Option - YouTube

Have you studied the stepper library that comes with the Arduino IDE?

Have you asked Google to find you any of the hundreds of existing Threads about those motors?

Have you been trying any code? If so post it here. And please use the code button </>

so your code looks like this

and is easy to copy to a text editor

…R
Stepper Motor Basics

Robin2:
Sorry,i first post in this forum so i don’t how to post my code.

my code

int Pin0 = 8;

int Pin1 = 9;
int Pin2 = 10;
int Pin3 = 11;

int _step = 0;

int val1 = 0;
int val4 = 0;

boolean dir = true;  // gre

void setup() {

pinMode(bPin4, INPUT);
  pinMode(bPin1, INPUT);

pinMode(Pin0, OUTPUT); 
  pinMode(Pin1, OUTPUT); 
  pinMode(Pin2, OUTPUT); 
  pinMode(Pin3, OUTPUT);

void loop() {

val1 = digitalRead(bPin1);
val2 = digitalRead(bPin2);

if (val1 == HIGH){
  s1(), k();
 
}

if(val4 == HIGH) {
  s0(), k();
}
}

void s0(){
  switch(_step){
  case 0:
    digitalWrite(Pin0, LOW); 
    digitalWrite(Pin1, LOW);
    digitalWrite(Pin2, LOW);
    digitalWrite(Pin3, HIGH);
  break; 
  case 1:
    digitalWrite(Pin0, LOW); 
    digitalWrite(Pin1, LOW);
    digitalWrite(Pin2, HIGH);
    digitalWrite(Pin3, HIGH);
  break; 
  case 2:
    digitalWrite(Pin0, LOW); 
    digitalWrite(Pin1, LOW);
    digitalWrite(Pin2, HIGH);
    digitalWrite(Pin3, LOW);
  break; 
  case 3:
    digitalWrite(Pin0, LOW); 
    digitalWrite(Pin1, HIGH);
    digitalWrite(Pin2, HIGH);
    digitalWrite(Pin3, LOW);
  break; 
  case 4:
    digitalWrite(Pin0, LOW); 
    digitalWrite(Pin1, HIGH);
    digitalWrite(Pin2, LOW);
    digitalWrite(Pin3, LOW);
  break; 
  case 5:
    digitalWrite(Pin0, HIGH); 
    digitalWrite(Pin1, HIGH);
    digitalWrite(Pin2, LOW);
    digitalWrite(Pin3, LOW);
  break; 
    case 6:
    digitalWrite(Pin0, HIGH); 
    digitalWrite(Pin1, LOW);
    digitalWrite(Pin2, LOW);
    digitalWrite(Pin3, LOW);
  break; 
  case 7:
    digitalWrite(Pin0, HIGH); 
    digitalWrite(Pin1, LOW);
    digitalWrite(Pin2, LOW);
    digitalWrite(Pin3, HIGH);
  break; 
  default:
    digitalWrite(Pin0, LOW); 
    digitalWrite(Pin1, LOW);
    digitalWrite(Pin2, LOW);
    digitalWrite(Pin3, LOW);
  break; 
}
}

void s1(){
  switch(_step){
  case 0:
    digitalWrite(Pin0, LOW); 
    digitalWrite(Pin1, LOW);
    digitalWrite(Pin2, LOW);
    digitalWrite(Pin3, LOW);
  break; 
  case 1:
    digitalWrite(Pin0, HIGH); 
    digitalWrite(Pin1, LOW);
    digitalWrite(Pin2, LOW);
    digitalWrite(Pin3, HIGH);
  break; 
  case 2:
    digitalWrite(Pin0, HIGH); 
    digitalWrite(Pin1, LOW);
    digitalWrite(Pin2, LOW);
    digitalWrite(Pin3, LOW);
  break; 
  case 3:
    digitalWrite(Pin0, HIGH); 
    digitalWrite(Pin1, HIGH);
    digitalWrite(Pin2, LOW);
    digitalWrite(Pin3, LOW);
  break; 
  case 4:
    digitalWrite(Pin0, LOW); 
    digitalWrite(Pin1, HIGH);
    digitalWrite(Pin2, LOW);
    digitalWrite(Pin3, LOW);
  break; 
  case 5:
    digitalWrite(Pin0, LOW); 
    digitalWrite(Pin1, HIGH);
    digitalWrite(Pin2, HIGH);
    digitalWrite(Pin3, LOW);
  break; 
    case 6:
    digitalWrite(Pin0, LOW); 
    digitalWrite(Pin1, LOW);
    digitalWrite(Pin2, HIGH);
    digitalWrite(Pin3, LOW);
  break; 
  case 7:
    digitalWrite(Pin0, LOW); 
    digitalWrite(Pin1, LOW);
    digitalWrite(Pin2, HIGH);
    digitalWrite(Pin3, HIGH);
  break; 
  default:
    digitalWrite(Pin0, LOW); 
    digitalWrite(Pin1, LOW);
    digitalWrite(Pin2, LOW);
    digitalWrite(Pin3, HIGH);
  break; 
}
}

void k(){
  if(dir){
  _step++;
}else{
  _step–;
}
if(_step>7){
  _step=0;
}
if(_step<0){
  _step=7;
}
delay(1);
}

You need to tell us what your code actually does, and what you want it to do.

You did not respond to my question about the stepper library.

...R

Robin2:
my code does

press button1 to clockwise

press button2 to counterclockwise

but it must keep press the button to rotate.

i want press once button1 to clockwise and press once button2 to counterclockwise

You need to make a logical separation between reading the buttons and commanding the motor.

The buttons should change the value of a variable. The movement of the motor should be based on the value of the variable. If no button is pressed the variable will remain unchanged and the motor will keep working.

...R

Robin2:
can you teach me than?

Have a look at Planning and Implementing a Program

The way your program is written

val1 = digitalRead(bPin1);
val2 = digitalRead(bPin2);

the values change when you take your finger off the button. You need a variable that only changes when you press the button, and not when you release it.

Something like

prevBtnVal = btnVal;
btnVal = digitalRead(bPin1);
if (btnVal == LOW && prevBtnVal == HIGH) {  // assumes LOW when pressed
   val1 = ! val1;
}

...R

Robin2:
thank you very much, i'm done!