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.

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 :


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
   written By :Mohannad Rawashdeh

#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()
pinMode(IN1, OUTPUT); 
pinMode(IN2, OUTPUT); 
pinMode(IN3, OUTPUT); 
pinMode(IN4, OUTPUT); 
// delay(1000);

void loop()
  currentMillis = micros();

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

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

Great instructable! I have a question though. <br>In the ULN2003 datasheet says that it's output is inverted. Do we need to invert the input first? Thank you!
<p>Thanks! Everything worked right out of the box and this tut!</p>
WOW , looks fantastic ! <br>well done :)
<p>100% OK!!!</p>
<p>Thanks for your code. I managed to have my Uno control the same <br>stepper motor, whereas I could not with other examples. The code could <br>be made simpler though.</p><p>Here is my attempt, using arrays, renaming variables and simplifying control </p><p><br><br>/*<br> BYJ48 Stepper motor code<br> Connect :<br> IN1 &gt;&gt; D8<br> IN2 &gt;&gt; D9<br> IN3 &gt;&gt; D10<br> IN4 &gt;&gt; D11<br> VCC ... 5V Prefer to use external 5V Source<br> Gnd<br> written By :Mohannad Rawashdeh<br> https://www.instructables.com/member/Mohannad+Rawashdeh/<br> 28/9/2013<br> */<br><br>#define IN1 8<br>#define IN2 9<br>#define IN3 10<br>#define IN4 11<br>const int NBSTEPS = 4096;<br>const int STEPTIME = 900;<br>int Step = 0;<br>boolean Clockwise = true;<br><br>int arrayDefault[4] = {LOW, LOW, LOW, LOW};<br><br>int stepsMatrix[8][4] = {<br> {LOW, LOW, LOW, HIGH},<br> {LOW, LOW, HIGH, HIGH},<br> {LOW, LOW, HIGH, LOW},<br> {LOW, HIGH, HIGH, LOW},<br> {LOW, HIGH, LOW, LOW},<br> {HIGH, HIGH, LOW, LOW},<br> {HIGH, LOW, LOW, LOW},<br> {HIGH, LOW, LOW, HIGH},<br>};<br><br>unsigned long lastTime;<br>unsigned long time; <br><br>void setup(){<br> Serial.begin(9600);<br> Serial.println(&quot;Starting...&quot;);<br> pinMode(IN1, OUTPUT); <br> pinMode(IN2, OUTPUT); <br> pinMode(IN3, OUTPUT); <br> pinMode(IN4, OUTPUT); <br>}<br><br>void loop(){<br> unsigned long currentMicros;<br> int stepsLeft = NBSTEPS;<br> time = 0;<br> while(stepsLeft &gt; 0){<br> currentMicros = micros();<br> if(currentMicros - lastTime &gt;= STEPTIME){<br> stepper(); <br> time += micros() - lastTime;<br> lastTime = micros();<br> stepsLeft--;<br> }<br> }<br> Serial.println(time);<br> Serial.println(&quot;Wait...!&quot;);<br> delay(2000);<br> Clockwise = !Clockwise;<br> stepsLeft = NBSTEPS;<br>}<br><br>void writeStep(int outArray[4]){<br> for (int i=0;i &lt; 4; i++){<br> digitalWrite(IN1 + i, outArray[i]);<br> }<br>}<br><br>void stepper(){<br> if ((Step &gt;= 0) &amp;&amp; (Step &lt; 8)){<br> writeStep(stepsMatrix[Step]);<br> }else{<br> writeStep(arrayDefault);<br> }<br> setDirection();<br>} <br><br>void setDirection(){<br> (Clockwise) ? (Step++) : (Step--);<br><br> if(Step&gt;7){<br> Step=0;<br> }else if(Step&lt;0){<br> Step=7; <br> }<br>}</p><p>structures.</p>
<p>I used your code to support two stepper motors to run at the same time. It may be generalized for multiple number of motors, but I needed two only, so this is good enough for me. It does not have STEPTIME parameter, hence no time checking. Also, it does not have writeStep function, it is woven into the stepper function. It needs to be interlaced for each motor, so it could not xist on its own.</p><p>There is a print statement in the setDirection routine. If you take it out, the motor does not run properly. I tried to replace it with some delayMicroseconds values, ranging from 10 to 100, but not successful. I think that is the timer for the motor. May be one can try some non-blocking counters to replace the print statement.</p><p>Here is the code:</p><p>/*<br>BYJ48 Stepper motor code<br>Stepper 1 :<br>IN1 &gt;&gt; D2<br>IN2 &gt;&gt; D3<br>IN3 &gt;&gt; D4<br>IN4 &gt;&gt; D5<br><br>Stepper 2 :<br>IN5 &gt;&gt; D6<br>IN6 &gt;&gt; D7<br>IN7 &gt;&gt; D8<br>IN8 &gt;&gt; D9<br><br>VCC ... 5V Prefer to use external 5V Source<br>Gnd<br>written By :Mohannad Rawashdeh<br>https://www.instructables.com/member/Mohannad+Rawashdeh/<br>28/9/2013<br><br>Modified by Kadir Haldenbilen for two Stepper Motor support<br>28/01/2017<br>*/<br><br>#define IN1 2<br>#define IN2 3<br>#define IN3 4<br>#define IN4 5<br>#define IN5 6<br>#define IN6 7<br>#define IN7 8<br>#define IN8 9<br>const int NBSTEPS = 1024;<br>int Steps[2] = {0,7};<br>int Clockwise[2] = {1, 0};<br><br>int stepsMatrix[8][4] = {<br>{LOW, LOW, LOW, HIGH},<br>{LOW, LOW, HIGH, HIGH},<br>{LOW, LOW, HIGH, LOW},<br>{LOW, HIGH, HIGH, LOW},<br>{LOW, HIGH, LOW, LOW},<br>{HIGH, HIGH, LOW, LOW},<br>{HIGH, LOW, LOW, LOW},<br>{HIGH, LOW, LOW, HIGH},<br>};<br><br>unsigned long lastTime;<br><br>void setup(){<br>Serial.begin(115200);<br>Serial.println(&quot;Starting...&quot;);<br>pinMode(IN1, OUTPUT);<br>pinMode(IN2, OUTPUT);<br>pinMode(IN3, OUTPUT);<br>pinMode(IN4, OUTPUT);<br>pinMode(IN5, OUTPUT);<br>pinMode(IN6, OUTPUT);<br>pinMode(IN7, OUTPUT);<br>pinMode(IN8, OUTPUT);<br>}<br><br>void loop(){<br>int stepsLeft = NBSTEPS;<br>while(stepsLeft &gt; 0){<br> stepper();<br> stepsLeft--;<br>}<br>reset();<br>Serial.println(&quot;Wait...!&quot;);<br>delay(2000);<br>Clockwise[0] = 1-Clockwise[0];<br>Clockwise[1] = 1-Clockwise[1];<br>Steps[0] = 7-Clockwise[0]*7;<br>Steps[1] = 7-Clockwise[1]*7;<br>stepsLeft = NBSTEPS;<br>}<br><br>void stepper(){<br> for (int i=0;i &lt; 4; i++){<br> for (int j=0; j&lt;2; j++) {<br> int INP = j*4 + 2;<br> digitalWrite(INP + i, stepsMatrix[Steps[j]][i]);<br> setDirection(j);<br> }<br> }<br><br>}void reset(){<br> for (int i=0;i &lt; 4; i++){<br> for (int j=0; j&lt;2; j++) {<br> int INP = j*4 + 2;<br> digitalWrite(INP + i, LOW);<br> }<br> }<br>}<br><br>void setDirection(int j){<br> if (Clockwise[j] == 1) Steps[j]++;<br> else Steps[j]--;<br> //Following print statement (or an appropriate delay) needed for proper rotation<br> Serial.print(&quot; Steps[j] before : &quot;);Serial.println(Steps[j]);<br> if(Steps[j]&gt;7) Steps[j]=0;<br> else if(Steps[j]&lt;0) Steps[j]=7;<br>}</p>
<p>great tutorial! very helpful, thanks</p>
Well done ! congratulations
Hi <br>On my project; i use a sg90 servo, ultrasonic sensor and stepper motor. I wanna drive stepper motor by servo angle. Servo rotating 0-180 degrees. If ultrasonic sensor sense a threat near the 30 cm at which angle stepper motor goes to angle. How can i do this.<br>Thank you
<p>Very helpful.</p>
<p>can you give me the code</p>
<p>hi can i have the code</p>
<p>Tak det var godt nok en del nemmere end jeg havde troet.</p><p> MVH</p><p>Bjarne</p>
<p>Thank you so much for this tutorial!</p>
<p>I made one with two motors and used it to make tiny vertical drawbot.See it here :) http://pappmaskin.no/2016/08/pen-plotter-1/</p>
<p>Nice work. </p>
<p>Very cool</p>
<p>Thank you for this workaround to make this stepper work.</p><p>While googling around, I also found this simpler code that also works. Sharing here in case someone wants to use it.</p><p>https://github.com/bdargham/BYJ48-Stepper</p>
<p>thanks for clarity on using this motor/driver board.</p><p>you ar a big help to me in learning how to use this.</p>
<p>there's a library that works well here:<br>http://forum.arduino.cc/index.php?topic=422336.0</p>
<p>Hello friends.... I have Stepper Motor NEMA23 10KgCm torque with 5A <br>Microstepping Drive and arduino uno. i need to rotate stepper motor with<br> different speeds can you give me coding of arduino and how to connect <br>with each other please help me friends</p>
<p>Hello friends.... I have Stepper Motor NEMA23 10KgCm torque with 5A Microstepping Drive and arduino uno. i need to rotate stepper motor with different speeds can you give me coding of arduino and how to connect with each other please help me friends</p>
<p>How would you modify the code to run two stepper motors at once?</p>
<p>Hi! Great information... I have two questions: </p><p>1) I am trying to use one of these motors to operate a speedometer needle, I am modifying the delivered rotation of the stepper by a fixed adjustment (pindisp) see below.</p><p>The question I have is if I have sequential &quot;calls&quot; to the motor as it is supposed to sweep around, how do I avoid it going back to zero each time? Can I simply send it a negative or positive offset? (i.e read the proposed value of (STEPS_PER_OUTPUT_REVOLUTION * PinDisp), subtract the last one and send the result to the motor)</p><p>If the Steps2Take value is (say) 200 and the needle rotates to that,</p><p>If the next Steps2Take value is (say) 180, do I simple send Sets2Take=-20 (minus 20)</p><p>2) is there a 12v eqiuvalent since my controller is 5v or 12v?</p><p>=========</p><p>Steps2Take = STEPS_PER_OUTPUT_REVOLUTION * PinDisp; // Rotate CW for 1 rev x correction</p><p> small_stepper.setSpeed(1000);</p><p> small_stepper.step(Steps2Take);</p><p> delay(100);</p><p>====</p><p>Great job </p><p>Kind Regards </p><p>Dr B </p><p>Southampton UK</p>
<p>Hi, I have followed your instructions and it worked fine. I have built on your code and make what I think to be a neater implementation. Here is my github repo with it in, hope someone finds it useful.</p><p>https://github.com/mattcow12/BYJ48-Stepper/tree/master</p>
<p>Thanks for this code! Can you tell me which value do I need to change to make the output shaft rotate faster?</p>
<p>Very useful tutorial. It helped me understand how to drive a stepper motor, and specifically the one I had. I also had a go at re-writing the code in my own style.</p><p><a href="https://github.com/bdargham/BYJ48-Stepper" rel="nofollow">https://github.com/bdargham/BYJ48-Stepper</a></p>
<p>That is cleaner code. It works as planned, and it's easier to understand and execute; especially given the proper use of comments. Thanks for posting it.</p><p>Any thoughts on modifying it for use with a pot? Basically: 'sweep with pot position'... I can't find clean code for this really anywhere. Especially with this specific hobby stepper.</p>
<p>Thanks for sharing your code Matthew, this is indeed useful :)</p>
<p>is any way to implement quarter step control? i try modify table and use PWM=50% on proper coils between halfsteps but doesnt work, maybe it needs more than 50%? (i cant check it in near time)</p><p>i know how to do it with 6 wire unipolar stepper,</p><p>anybody disassembly this stepper? is it easy (if possible) way to unshort middles of coils? and make unipolar 6wire stepper?</p><p>or use just ends of coils without common shorted middle, and make bipolar?</p><p>diagram form here:</p><p>https://developer.mbed.org/media/uploads/XtaticO/28byj48_01.jpg</p>
<p>Thank you! It works fine - even with USB-power source. </p>
<p>Thank you very much, it works!! But the stepper motor gets very hot, I am using 12v. Is it ok?</p>
<p>Hi rabiuls,</p><p>If you're using the motor driver based on the ULN2003 then no, it's not ok to power it at 12 VDC. That driver is not a current regulator. If you take a look to the motor specs it's rated at 5 VDC and has a DC resistance 50&Omega; &plusmn; 7%(25℃) meaning the motor will dissipate about 500 mW at 5 VDC (current is limited to 100 mA). If you increase the supply voltage to 12 VDC, the power dissipation will be 2,88 watts (current is boosted to 240 mA) !!! Your motor (and your driver) will eventually break (you're literally frying both the motor and the driver )!!! If you want to power your motor with a higher voltage supply than the rated voltage of 5 VDC you'll need a motor driver that has current limit protection like for example the &quot;EasyDriver - Stepper Motor Driver&quot; from Sparkfun or other similar.</p>
<p>It is very common for stepper motors to get hot. Many 3d printer and CNC mill designs include heatsinks and fans on each stepper motor, and in robust designs a temperature sensor is added to each stepper to allow the control circuits to shut down before the motor is damaged.</p><p>The ULN2003 is pretty much just an amperage amplifier that steps up the Arduino's power signal to be enough for the motor. Specialized stepper motor drivers have different amperage settings for 'moving' and 'holding' states, which can drastically lower heat and power usage. They are a bit more expensive, though.</p><p>With that all said, the typical use for this stepper motor does not have a<br> dedicated fan, and generally no temperature based shut-off. It is probably not getting too hot for its design, but if you are concerned, adding a 40mm fan ( like this one: http://www.digikey.com/product-detail/en/sunon-fans/HA40101V4-000U-999/259-1616-ND/3694183 ) wouldn't hurt.</p>
<p>Depends on your amperage, though it is normal for steppers to get a little warm, so you're probably fine</p>
<p>hello </p><p>Its mostako, I am very new to stepper motor control with Arduino. </p><p>I need to rotate my stepper motor 28byj-48, ULN2003 driver IC both in forward and in reverse direction when the respective push button is pressed.</p><p>I have I have a stepper motor, ULN2003, arduino and two push button</p><p>Please provide me a code for arduino such that i can rotate the stepper in both direction only when push button is pressed</p><p>My email id is mostako1@gmail.com </p>
<p>Thanks, just what I needed to make sure my board was good. I changed the pins to the ones for a nano and it started right up.</p>
<p>Cool project! Only took minutes to get it going! Thank you!!!!</p>
<p>This was great! Helped me a lot - thank you muchly!</p>
<p>Thank you for posting this. My steppers had the direction problem so your fix is greatly appreciated.</p>
<p>thanks for tutorial :)</p>
<p>Worked perfectly... can you please put comments on your sketch code, to understand the logic.</p><p>I would like to control speed and rotate it 360deg </p>
Can you plz add commentals in the code to explain it<br>
<p>My setup would do nothing until i realised the pin marked &quot;+&quot; wasn't actually connected to anything (on the motor board). When i connected 5v to both the two pins previously jumpered together (like your image above) it burst into life. Do i have a badly designed motor board?</p>
<p>on further examination i find this: pin 1 (marked &quot;-&quot;) is ground, as it should be). Pin 2 (marked &quot;+&quot;) not connected. Pin 3 is connected to &quot;com&quot; on uln chip, so should be 5v. Pin 4 connected through the LEDs to the motor a to the uln outputs. I put 5v on this aIso. Implies you can split the power to drive a different power stepper? Odd, what do people think?</p>
<p>Many thanks, works well.</p>
<p>Hello,</p><p>And thanks for your help. Now i understand :-)</p><p>I try to do a program who control my motor with a pusher button.</p><p>When i push the button, i would that my motor do a step of 25&deg;.</p><p>Could you help me?</p>
<p>Made it using a ULN2003APG IC instead of the breakout board. The MB-101 Breadboard power supply feeds the motor.</p>
<p>i connected the uln board 2003 to an 9v battery but its not powering on<br>is the board dead?</p><p>red wire +</p><p>black wire -</p>
<p>My stepper motor seem to be broken.. or do i have a power problem? It only turns when i give it a little help... rest works fine.... it only vibrates if i dont help it turn with my fingers.. so guess its broken... that sucks</p>

About This Instructable




Bio: 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/
More by Mohannad Rawashdeh:TTP224 Digital Touch Sensor Applications Using Arduino RF 315MHz/433MHz Wireless and Arduino...again! Motor Driver BTS7960 43A 
Add instructable to: