Stepper Motor 28byj48 with Arduino :

After using the servo motor and DC motor, today we are going to learn how to use Stepper Motor(28byj48). Stepper motor is basically a motor that can rotate 360 degree and we can control the rotation. In this tutorial we will use 28byj48 stepper motor. 28byj48 can move 2048 steps per revolution. 28byj48 used with UNL-2003 stepper motor driver.

28bjy48

UNL2003 is a stepper motor driver. Using this driver we can control stepper motor. It has 4 pins (IN1,IN2, IN3, IN4). By using these pins we can control the speed, direction and steps.

Components :

  • Arduino Uno
  • Jumper wires
  • 28byj48 stepper motor
  • ULN2003

Pinout :

Circuit Diagram :

Code :


#define STEPPER_PIN_1 9
#define STEPPER_PIN_2 10
#define STEPPER_PIN_3 11
#define STEPPER_PIN_4 12

int step_number = 0;

void setup() {
  pinMode(STEPPER_PIN_1, OUTPUT);
  pinMode(STEPPER_PIN_2, OUTPUT);
  pinMode(STEPPER_PIN_3, OUTPUT);
  pinMode(STEPPER_PIN_4, OUTPUT);
}

void loop() 
{
  for(int i=0 ; i<2048 ; i++){ 
  OneStep(true);
  delay(2); // you can change the delay to set the speed
  }
  delay(2000);
   for(int i=0 ; i<2048 ; i++){ 
  OneStep(false);
  delay(10); // you can change the delay to set the speed
  }
   delay(2000);
}



void OneStep(bool dir){
  if(dir){
    switch(step_number){
      case 0:
      digitalWrite(STEPPER_PIN_1, HIGH);
      digitalWrite(STEPPER_PIN_2, LOW);
      digitalWrite(STEPPER_PIN_3, LOW);
      digitalWrite(STEPPER_PIN_4, LOW);
      break;
      case 1:
      digitalWrite(STEPPER_PIN_1, LOW);
      digitalWrite(STEPPER_PIN_2, HIGH);
      digitalWrite(STEPPER_PIN_3, LOW);
      digitalWrite(STEPPER_PIN_4, LOW);
      break;
      case 2:
      digitalWrite(STEPPER_PIN_1, LOW);
      digitalWrite(STEPPER_PIN_2, LOW);
      digitalWrite(STEPPER_PIN_3, HIGH);
      digitalWrite(STEPPER_PIN_4, LOW);
      break;
      case 3:
      digitalWrite(STEPPER_PIN_1, LOW);
      digitalWrite(STEPPER_PIN_2, LOW);
      digitalWrite(STEPPER_PIN_3, LOW);
      digitalWrite(STEPPER_PIN_4, HIGH);
      break; 
    }
  }else{
    switch(Step_number){
      case 0:
      digitalWrite(STEPPER_PIN_1, LOW);
      digitalWrite(STEPPER_PIN_2, LOW);
      digitalWrite(STEPPER_PIN_3, LOW);
      digitalWrite(STEPPER_PIN_4, HIGH);
      break;
      case 1:
      digitalWrite(STEPPER_PIN_1, LOW);
      digitalWrite(STEPPER_PIN_2, LOW);
      digitalWrite(STEPPER_PIN_3, HIGH);
      digitalWrite(STEPPER_PIN_4, LOW);
      break; 
      case 2:
      digitalWrite(STEPPER_PIN_1, LOW);
      digitalWrite(STEPPER_PIN_2, HIGH);
      digitalWrite(STEPPER_PIN_3, LOW);
      digitalWrite(STEPPER_PIN_4, LOW);
      break;  
      case 3:
      digitalWrite(STEPPER_PIN_1, HIGH);
      digitalWrite(STEPPER_PIN_2, LOW);
      digitalWrite(STEPPER_PIN_3, LOW);
      digitalWrite(STEPPER_PIN_4, LOW);
      break; 
    }
  }

  step_number++;
  if(step_number > 3){
    step_number = 0;
  }
}

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
YouTube
× Contact us