BYJ48 Stepper Motor

Introduction: BYJ48 Stepper Motor

About: Maker, PCB designer , electronics instructor from Jordan just one word ! I Adore electronics follow me on FB https://www.facebook.com/Mohannad-Rawashdeh-Raw-774983565988641/

Stepper Motor  is a motor controlled by a series of electromagnetic coils. The center shaft has a series of magnets mounted on it, and the coils surrounding the shaft are alternately given current or not, creating magnetic fields which repulse or attract the magnets on the shaft, causing the motor to rotate.

This design allows for very precious control of the motor,There are two basic types of stepper motors, unipolar steppers and bipolar steppers .

In This instructable , I will talk about an Unipolar Stepper Motor 28-BYJ48 .

The unipolar stepper motor has five or six wires and four coils (actually two coils divided by center connections on each coil). The center connections of the coils are tied together and used as the power connection. They are called unipolar steppers because power always comes in on this one pole.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Specification , Motor Driver

There are many Types of Drivers , L293 , ULN2003 , A3967SLB , And More , 

The 28-BYJ48 Even comes with Breakout using ULN2003 As a Motor driver chip .

Specification for this Motor  " And you can download datasheet from the attachment "

Rated voltage : 5VDC
Number of Phase 4
Speed Variation Ratio 1/64
Stride Angle 5.625° /64
Frequency 100Hz
DC resistance 50Ω±7%(25℃)
Idle In-traction Frequency > 600Hz
Idle Out-traction Frequency > 1000Hz
In-traction Torque >34.3mN.m(120Hz)
Self-positioning Torque >34.3mN.m
Friction torque 600-1200 gf.cm
Pull in torque 300 gf.cm
Insulation grade A

and the schematics of  This breakout shown like the Pictures on the attachment 

Note that if you want to use L293 Instead of ULN2003 , You will need to leave Red wire No connection .


Materials :

you will need :

1) Arduino Board .

2) BYJ48 Stepper Motor 5v

3) ULN2003 Moror driver Module

4) Jumper .

5) 5v voltage source  "Optional" .



Step 2: Arduino Code .

The Arduino IDE Support a Library for Stepper Motor , Very Easy to use , After Connect Motor with arduino You can Upload the Sketch on to the arduino .

But ...

You must take something  in consider :

This Motor has a Gear ratio of 64 , and Stride Angle 5.625°  so this motor has a 4096 Steps .

 steps = Number of steps in One Revolution  * Gear ratio   .

 steps= (360°/5.625°)*64"Gear ratio" = 64 * 64 =4096 . this value will substitute it on The arduino Sketch 

For adafruit Stepper Motor , the Stride Angle 7.5° and Gear ratio is 16 , So number of steps in 1 Revolution is :

steps in One Revolution  = 360 / 7.5 = 48   .

steps= 48 * 16 = 768 

That's will be different depend on what motor you are using , So check The Datasheet for Your stepper Motor to calibrate this values 

28-BYJ48 Motor Datasheet .

Motor Driver ULN2003 BreakOut Connected To Arduino From IN1 - IN4 To D8 - D11 Respectively 

To Power you Motor ,  Recommanded to use external Power Supply with 5V-500mA  at least , Don't power it directly from arduino Board 5V .

Step 3: Library Direction Issue ... and How to Fix It .

When You Upload the sketch to the arduino , The Motor will Be rotate in  one direction By type the command :

  step(steps);

So you must Put the Number of step to turn the motor .

The reference  said You can put the positive value to turn one direction, negative to turn the other.

If that's  OK With Your stepper Motor , You  don't need to read the following .

If Not , Your Motor turn to same direction even you Put the steps Positive Value or negative , What is the issue ?

This Motor need to operate as the Table on the attachment .

the arduino Stepper Library need to modify to match this requirement .

I wrote a code which is allow to this motor to Move clockwise and counter clock wise 

Code in the next step :

Step 4: Modify Code

the final code for this Stepper motor :

/*
   BYJ48 Stepper motor code
   Connect :
   IN1 >> D8
   IN2 >> D9
   IN3 >> D10
   IN4 >> D11
   VCC ... 5V Prefer to use external 5V Source
   Gnd
   written By :Mohannad Rawashdeh
https://www.instructables.com/member/Mohannad+Rawashdeh/
     28/9/2013
  */

#define IN1  8
#define IN2  9
#define IN3  10
#define IN4  11
int Steps = 0;
boolean Direction = 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("Wait...!");
  delay(2000);
  Direction=!Direction;
  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; 
}
SetDirection();
}

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



22 People Made This Project!

Recommendations

  • Finish It Already Speed Challenge

    Finish It Already Speed Challenge
  • Arduino Contest 2020

    Arduino Contest 2020
  • First Time Author Contest

    First Time Author Contest

116 Discussions

0
penged
penged

Question 8 months ago

I would like to control 2 steppers with this code. Can I simply add 4 other digital pins for the other motor? It does not matter to me if they are synced up.

0
IDC1
IDC1

1 year ago

hello, if I want to make rotation only 90 degres, how the code ? thanks..

0
malanirarfa
malanirarfa

1 year ago

sir. I need an explanation about this Sketsh. especially the "time" that appears in the serial monitor and the explanation for "int xw" which I bold below



/*
BYJ48 Stepper motor code
Connect :
IN1 >> D8
IN2 >> D9
IN3 >> D10
IN4 >> D11
VCC ... 5V Prefer to use external 5V Source
Gnd
written By :Mohannad Rawashdeh
https://www.instructables.com/member/Mohannad+Rawa...
28/9/2013
*/

#define IN1 8
#define IN2 9
#define IN3 10
#define IN4 11
int Steps = 0;
boolean Direction = 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("Wait...!");
delay(1000);
Direction=!Direction;
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;

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

0
Necrokdns
Necrokdns

1 year ago

How can i change this code to use an analog sensor?

0
VAMC2
VAMC2

1 year ago

HELLO BRO, if i change 28byj 48 stepper motor form unipolar to bipolar then how can i find steps per rotation after modified, is it same or it changes to new value??? waiting for your answer bro

1
m4biz
m4biz

Question 2 years ago on Introduction

Hi.

You sayed "... To Power you Motor , Recommanded to use external Power Supply with 5V-500mA at least , Don't power it directly from arduino Board 5V ..." but in the photo attached to your article I don't see any external source. Why?

0
JesperB10
JesperB10

Answer 1 year ago

As m4biz sais it would be good to know why and when you need to connect with external power. Also an image of how to connect with external power would be nice to get started faster for people who are very new to the Arduino.

0
IanM343
IanM343

Reply 1 year ago

I too would appreciate an image and details of how to connect the power source, please!

0
IanM343
IanM343

Reply 1 year ago

I would also appreciate an image of how it is connected to an external source!

1
Mohannad Rawashdeh
Mohannad Rawashdeh

Answer 1 year ago

because in my case I ran the motor without load connected to the shaft so it will need a few milli amps to works

if you want to connect an external load to the shaft so you need to use external power supply

0
rafaelnfs
rafaelnfs

1 year ago

Excelente post, tengo un par por ahi; no sabia de cuantos pasos eran :)
gracias por tu magnifico post.

1
curtis63
curtis63

2 years ago

EXCELLENT POST !!!

I cut the code size down and modified it so you can specify Speed and don't need direction. Just call 'stepper()' and give it the number of steps you want to rotate. Use negative numbers to reverse the direction. You'll probably need to change the values for IN1-IN4 to match the pin numbers on your Arduino:

#define IN1 5

#define IN2 4

#define IN3 3

#define IN4 2

#define FULL_ROTATION 4076

const int phases1[] = {0, 0, 0, 0, 0, 1, 1, 1};

const int phases2[] = {0, 0, 0, 1, 1, 1, 0, 0};

const int phases3[] = {0, 1, 1, 1, 0, 0, 0, 0};

const int phases4[] = {1, 1, 0, 0, 0, 0, 0, 1};

int Phase = 0;

int Speed = 100; //MUST BE 1 - 100

void setup()

{

pinMode(IN1, OUTPUT);

pinMode(IN2, OUTPUT);

pinMode(IN3, OUTPUT);

pinMode(IN4, OUTPUT);

Serial.begin(115200);

}

void loop()

{

stepper(FULL_ROTATION);

stepper(-FULL_ROTATION);

}

void stepper(int count)

{

int rotationDirection = count < 1 ? -1 : 1;

count *= rotationDirection;

for (int x = 0; x < count; x++)

{

digitalWrite(IN1, phases1[Phase]);

digitalWrite(IN2, phases2[Phase]);

digitalWrite(IN3, phases3[Phase]);

digitalWrite(IN4, phases4[Phase]);

IncrementPhase(rotationDirection);

delay(100/Speed);

}

}

void IncrementPhase(int rotationDirection)

{

Phase += 8;

Phase += rotationDirection;

Phase %= 8;

}

0
Grimravus
Grimravus

Reply 2 years ago

Thanks Curtis! This is lovely, elegant code, and it's now my go-to for stepper control -- works like a charm!

0
SuharshT
SuharshT

Reply 2 years ago

Good job mate!

0
Grimravus
Grimravus

2 years ago

Great Post! Great explanation - you helped me get a better understanding of fine control of unipolar steppers. Well done!

0
rsmda
rsmda

2 years ago

I need a stepping motor 28BYJ48 12VDC 1/32 NO.171029A. All I can find is one that's 5VDC 1/64

F65M3G8HMMF4YRG.LARGE.jpg
1
ChrisM930
ChrisM930

Question 2 years ago

I would be grateful if someone could help me, I have 2 of these motors and 2 drivers, I would like both motors to start clockwise 8 turns at 10 revs increasing to 18 revs for a further 8 turns and then the same in anti-clockwise and go back to start pause for 20 seconds and then repeat and so on.

Thank's in advance.

0
ShubhamM116
ShubhamM116

2 years ago

I want to run 24byj48 12v dc stepper motor at 100 pps then what is it's code....

0
yaputraj1
yaputraj1

2 years ago

Can I use 9v battery (external) to supply power for the motor via driver? Well, you said min 5v 0.5 amp, so I guess it's not a problem. Am I right?

Or is it better if I use 4 common 1.5 v batteries?

0
lotfichohra0
lotfichohra0

2 years ago

Hi guys, I want help from you, i need to made elevator with steppr motor j48 and control diraction with push buttons and to screen the number of etage in lcd so i need circuit diagram + code plaese if you can help me , im waiting you help