Stepper Motor Setup

Hi all! I am very new, by a day. I have been working for about 4 hours on how to get a stepper motor to work. I have a 28BYJ-48 motor, hooked up to a ULN2003 “ULN2003 Four-phase Five-wire Driver Board Electroincs Stepper Motor Driver Board” link to that I don’t know how to describe it, but. How could I hook that up to my uno and have the stepper spin?

The closet I got was this

//original source is http://www.geeetech.com/wiki/index.php/Stepper_Motor_5V_4-Phase_5-Wire_%26_ULN2003_Driver_Board_for_Arduino
// Update by Ahmad Shamshiri for RoboJax.com
// Published on March 27, 2017 in Aajx, ON, Canada.

int Pin1 = 10; 
int Pin2 = 11; 
int Pin3 = 12; 
int Pin4 = 13; 
int _step = 0; 
boolean dir = true;// false=clockwise, true=counter clockwise
int count=0;
void setup() 
{ 
 pinMode(Pin1, OUTPUT);  
 pinMode(Pin2, OUTPUT);  
 pinMode(Pin3, OUTPUT);  
 pinMode(Pin4, OUTPUT);  
} 
 void loop() 
{ 
 switch(_step){ 
   case 0: 
     digitalWrite(Pin1, LOW);  
     digitalWrite(Pin2, LOW); 
     digitalWrite(Pin3, LOW); 
     digitalWrite(Pin4, HIGH); 
   break;  
   case 1: 
     digitalWrite(Pin1, LOW);  
     digitalWrite(Pin2, LOW); 
     digitalWrite(Pin3, HIGH); 
     digitalWrite(Pin4, HIGH); 
   break;  
   case 2: 
     digitalWrite(Pin1, LOW);  
     digitalWrite(Pin2, LOW); 
     digitalWrite(Pin3, HIGH); 
     digitalWrite(Pin4, LOW); 
   break;  
   case 3: 
     digitalWrite(Pin1, LOW);  
     digitalWrite(Pin2, HIGH); 
     digitalWrite(Pin3, HIGH); 
     digitalWrite(Pin4, LOW); 
   break;  
   case 4: 
     digitalWrite(Pin1, LOW);  
     digitalWrite(Pin2, HIGH); 
     digitalWrite(Pin3, LOW); 
     digitalWrite(Pin4, LOW); 
   break;  
   case 5: 
     digitalWrite(Pin1, HIGH);  
     digitalWrite(Pin2, HIGH); 
     digitalWrite(Pin3, LOW); 
     digitalWrite(Pin4, LOW); 
   break;  
     case 6: 
     digitalWrite(Pin1, HIGH);  
     digitalWrite(Pin2, LOW); 
     digitalWrite(Pin3, LOW); 
     digitalWrite(Pin4, LOW); 
   break;  
   case 7: 
     digitalWrite(Pin1, HIGH);  
     digitalWrite(Pin2, LOW); 
     digitalWrite(Pin3, LOW); 
     digitalWrite(Pin4, HIGH); 
   break;  
   default: 
     digitalWrite(Pin1, LOW);  
     digitalWrite(Pin2, LOW); 
     digitalWrite(Pin3, LOW); 
     digitalWrite(Pin4, LOW); 
   break;  
 } 
 if(dir){ 
   _step++; 
 }else{ 
   _step--; 
 } 
 if(_step>7){ 
   _step=0; 
 } 
 if(_step<0){ 
   _step=7; 
 } 
 delay(1); 

}

That made it spin very slow, which I guess would work. I would rather it spin faster but got very confused. I tried other codes including making my own, which just resulted in it vibrating. I got that code from here I don’t know what to do and would appreciate help!

How is the motor powered? Not from the Arduino, I hope. Can you post a diagram of YOUR wiring?

I suggest that you look at the Stepper library and the examples that come with it.

Karma for code tags on your first post. Quite unusual.

Stepper motors are slow compared to other types of motors. Worse yet the 28BYJ-48 is a "Reduction Stepper Motor" meaning it has gear reduction unit in it.
25-30 rpm is typical speed for these.

MCDran:
The closet I got was this
That made it spin very slow,

That is the normal way to make a 28BYJ motor move. And it is probably as fast as it can go as the Arduino is doing nothing else.

You may get some extra performance if you use the digitalWriteFast() library.

The 28BYJ-48 is small, cheap and quiet. But it is not designed for speed. For higher speeds you need a motor without a gearbox and with a specialized stepper motor driver such as the DRV8825 (for motor currents up to about 1.7 amps) and a high voltage (12v+) motor power supply. The whole package will be heavier, bigger and more expensive.

...R
Stepper Motor Basics
Simple Stepper Code

Thank you very much for your replies, im now trying to fit the stepper code in but how could I have the stepper run until a certain time?

When you start the stepper moving, record in a variable, the value of millis(). Then as you run the motor, each time through loop() check the start value against the current value of millis(). When the difference is more or equal to the desired run duration, stop the motor. This, of course, depends on the program being non blocking so as to tun the loop() frequently.

See the beginner's guide to millis() for a more thorough explanation.

Robin2 linked his simple stepper program. The second example shows how to step the motor using millis() instead of delay for non blocking motor control.

So I read all of the links you sent me, my goal is to press a button, light up 6 leds in order, spin a stepper for a set period of time, then move a servo. I have the button leds and servo working, any ideas?

int buttonPin = 6;     // the number of the pushbutton pin
int ledPin =  13;      // the number of the LED pin
boolean on=false;
int buttonState = 0; 
#include <Servo.h>
Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards
int LED1 = 13;
int LED2 = 12;
int LED3 = 11;
int LED4 = 10;
int LED5 = 9;
int LED6 = 8;
int Pin1 = 4; 
int Pin2 = 3; 
int Pin3 = 2; 
int Pin4 = 1; 
int _step = 0; 
boolean dir = true;// false=clockwise, true=counter clockwise
int count=0;

int pos = 0;    // variable to store the servo position

void setup() {
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(Pin1, OUTPUT);  
  pinMode(Pin2, OUTPUT);  
  pinMode(Pin3, OUTPUT);  
  pinMode(Pin4, OUTPUT);  
  myservo.attach(7);   
  const int pushButton = 8;
  const int redLed = 2;
  boolean signal;
}

void loop(){
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {    
    if(on==true){
      on=false;
    }  else{
      on=true;
    }
  }
 
  
   if (on == true) {    
  for (pos = 180; pos <=0;);
  myservo.write(pos);
 
  delay(500);
  digitalWrite(LED1, HIGH);    // turn on LED1 
  delay(200);                  // wait for 200ms
  digitalWrite(LED2, HIGH);    // turn on LED2
  delay(200);                  // wait for 200ms       
  digitalWrite(LED3, HIGH);    // turn on LED3 
  delay(200);                  // wait for 200ms
  digitalWrite(LED4, HIGH);     // turn off LED1
  delay(200);                  // wait for 300ms
  digitalWrite(LED5, HIGH);     // turn off LED2
  delay(200);                  // wait for 300ms
  digitalWrite(LED6, HIGH);     // turn off LED3
  delay(00);                  // wait for 300ms before running program all over again
  delay(500);
  for (pos = 0; pos <= 180;) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(0);                       // waits 15ms for the servo to reach the position

switch(_step){ 
   case 0: 
     digitalWrite(Pin1, LOW);  
     digitalWrite(Pin2, LOW); 
     digitalWrite(Pin3, LOW); 
     digitalWrite(Pin4, HIGH); 
   break;  
   case 1: 
     digitalWrite(Pin1, LOW);  
     digitalWrite(Pin2, LOW); 
     digitalWrite(Pin3, HIGH); 
     digitalWrite(Pin4, HIGH); 
   break;  
   case 2: 
     digitalWrite(Pin1, LOW);  
     digitalWrite(Pin2, LOW); 
     digitalWrite(Pin3, HIGH); 
     digitalWrite(Pin4, LOW); 
   break;  
   case 3: 
     digitalWrite(Pin1, LOW);  
     digitalWrite(Pin2, HIGH); 
     digitalWrite(Pin3, HIGH); 
     digitalWrite(Pin4, LOW); 
   break;  
   case 4: 
     digitalWrite(Pin1, LOW);  
     digitalWrite(Pin2, HIGH); 
     digitalWrite(Pin3, LOW); 
     digitalWrite(Pin4, LOW); 
   break;  
   case 5: 
     digitalWrite(Pin1, HIGH);  
     digitalWrite(Pin2, HIGH); 
     digitalWrite(Pin3, LOW); 
     digitalWrite(Pin4, LOW); 
   break;  
     case 6: 
     digitalWrite(Pin1, HIGH);  
     digitalWrite(Pin2, LOW); 
     digitalWrite(Pin3, LOW); 
     digitalWrite(Pin4, LOW); 
   break;  
   case 7: 
     digitalWrite(Pin1, HIGH);  
     digitalWrite(Pin2, LOW); 
     digitalWrite(Pin3, LOW); 
     digitalWrite(Pin4, HIGH); 
   break;  
   default: 
     digitalWrite(Pin1, LOW);  
     digitalWrite(Pin2, LOW); 
     digitalWrite(Pin3, LOW); 
     digitalWrite(Pin4, LOW); 
   break;  
 } 
 if(dir){ 
   _step++; 
 }else{ 
   _step--; 
 } 
 if(_step>7){ 
   _step=0; 
 } 
 if(_step<0){ 
   _step=7; 
 } 
 delay(1); 


 delay(5000);
    
    while(1){}
    }
     }         
  
  else {
  for (pos = 180; pos <=0;);
  myservo.write(pos);
     }        
  
  

  
}

This is what I have so far, I don’t want you to fix it just do you have any ideas? I know its messy but i’m still learning

int Pin4 = 1;

Pin 1 on an Uno is the hardware serial TX line. Even if you are not using serial that pin is still connected to the USB to TTL converter output. The big question is; why are you not using serial? The serial port is the best (and about only) troubleshooting and tool that you have. Serial prints, strategically placed, can tell you about the variables and program flow.

 if (on == true)
   {
      for (pos = 180; pos <= 0;);
      myservo.write(pos);

That for statement looks funny, no indexer?

for (pos = 180; pos <= 0;);

Again. The for loop reference.

A bit more reading: the designing and implementing a program tutorial might be worth a read. Really helped me when I started out.

Your code would be much easier to follow if it were indented properly. Fortunately the IDE has a tool to do that for us. Autoformat (ctrl-t or Tools, Auto Format) will indent the code in a standard fashion.

Thank you for trying to explain but ill probably just ditch the project. Thanks!

You have to get rid of the delay() calls and use millis() timing instead. Use a counter to keep track of at which step of the stepper sequence you are.

You say it's spinning slowly - how slow is that? Indeed 30 rpm is about the maximum you can get from this particular stepper, and that means a great number of steps each second. Your 5000 ms delay is not helping to keep things moving.