< async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"">

Sunday, September 18, 2016

Simple and quick way to test a 28BYJ-48 Stepper motor using arduino [beginner]

ADVERTISEMENT
When you need to move something with your micro-controllers (or arduinos), Stepper motors comes to your rescue. One of the cheapest and widely used stepper motor is 28BYJ-48 which can work with a 5v supply and gives enough juice for small tasks like moving a small robot or a small lever or a valve.


Quick start with a 28BYJ-48 with an arduino

Quickly test the Stepper motor with an arduino



Wiring is very simple. It is important to use a stepper controller board using ULN2003 stepper driver which basically deliver enough current to stepper motor (as arduino pin has limitations on current drain). Connect it as shown in figure and basically 4 digital pins are needed (here to test use D8, D9, D10, D11) and a separate line to provide the vcc with enough current to run the stepper. For testing with no load, you can connect 5v pin from arduino to the vcc pin on stepper.

Upload the sketch below and if the stepper is fine it will start turning back and forth.

Idea is to keep it simple to just test the stepper after taking out of its package. I am going to use it in a small automated antenna tuner and more about it can be read in my next post

Arduino stepper motor quick start

Source Code :


/*
  Connect to stepper
  In addition add powersupply for stepper (GND & 5 to 12v as in figure
  shown in http://blog.riyas.org)
  
   IN1 >> D8
   IN2 >> D9
   IN3 >> D10
   IN4 >> D11

  */

#define IN1  8
#define IN2  9
#define IN3  10
#define IN4  11

int Steps = 0;
boolean step_dir = true;// gre
unsigned long last_time;
unsigned long currentMillis ;
int steps_left=4095;
long time;
void setup()
{
Serial.begin(115200);
pinMode(IN1, OUTPUT); 
pinMode(IN2, OUTPUT); 
pinMode(IN3, OUTPUT); 
pinMode(IN4, OUTPUT); 
// delay(1000);

}
void loop()
{
  while(steps_left>0){
  currentMillis = micros();
  if(currentMillis-last_time>=1000){
  stepper(1); 
  time=time+micros()-last_time;
  last_time=micros();
  steps_left--;
  }
  }
  Serial.println(time);
  Serial.println("Pause for 2 second");
  delay(2000);
  step_dir=!step_dir;
  steps_left=4095;
}

void stepper(int xw){
  for (int x=0;x<xw;x++){
switch(Steps){
   case 0:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, HIGH);
   break; 
   case 1:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, HIGH);
     digitalWrite(IN4, HIGH);
   break; 
   case 2:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, HIGH);
     digitalWrite(IN4, LOW);
   break; 
   case 3:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, HIGH);
     digitalWrite(IN3, HIGH);
     digitalWrite(IN4, LOW);
   break; 
   case 4:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, HIGH);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break; 
   case 5:
     digitalWrite(IN1, HIGH); 
     digitalWrite(IN2, HIGH);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break; 
     case 6:
     digitalWrite(IN1, HIGH); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break; 
   case 7:
     digitalWrite(IN1, HIGH); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, HIGH);
   break; 
   default:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break; 
}
Setstep_dir();
}
} 


void Setstep_dir()
{
if(step_dir==1){ Steps++;}
if(step_dir==0){ Steps--; }
if(Steps>7){Steps=0;}
if(Steps<0){Steps=7; }
}

No comments:

Post a Comment