In this post we will go over how to use the 28byj-48 stepper motor with the ULN2003 motor driver. We’ll focus on basics, module pins, how to connect to Arduino, programming in Arduino IDE, and testing.
What is a Stepper Motor?
Stepper motors are brushless DC electric motors that rotate its shaft in discrete steps of equal angular rotation. This is possible due to the fact that the motor has coils in groups and can we to energize them in a specific sequence to get one step (i.e. an incremental rotational step). This very nature of stepper motors allow us to move a specific number of steps without having a position/displacement sensor, while maintaining good accuracy on the position and speed of rotation of the motor.
28BYJ-48 Stepper Motor
The 28BYJ-48 stepper motor is a great stepper motor for a wide range of applications. It’s also a great motor for anyone looking to learn about stepper motors.
Rated Voltage: 5V DC
Gear Ratio: 64:1
Net steps per revolution: 2048 (~0.1758deg/step)
*Motor has 32 steps/revolution, and the gear ratio is 64:1, resulting in a net steps/revolution of 32*64=2048.
Coil Type: Unipolar


ULN2003 Motor Controller
The ULN2003 motor controller is used to interface your stepper motor (in this case the 28BYJ-48) and your microcontroller (in this case an Arduino UNO). The motor controller accepts four digital inputs from the microcontroller (IN1 – IN4) and the 5 cables coming from the 28BYJ-48 stepper motor. There is also the power input, which can be set to 5V or 12V. For the specific motor in this example we would go with 5V. The motor controller also has 4 step indicator LEDs so you can see the state of the coils of the motor as it rotates. When running fast it can be hard to see.

Connecting ULN2003, 28BYJ-48, and Arduino UNO
ULN2003 – IN1 through IN 4: Used to drive motor phases. Connect to Arduino UNO digital input/output pins 8, 9, 10, and 11.
ULN2003 Power In: Connect to 5V and ground
ULN2003 Motor Pins: Connect the motor cables. The 28BYJ-48 normally comes with a connector that matches the ULN2003 connection terminal. They only go in on a specific orientation, so it’s hard to make a mistake.
ULN2003 / 28BYJI Examples
List of Examples
- Required Arduino Libraries
- Example 1 – Move from A to B by a specific number of steps with specific speed and acceleration
- Example 2 – Human input control (via rotary encoder)
Required Arduino Libraries
To complete the examples below you will need to install the AccelStepper Arduino library (written by Mike McCauley). To install it, open Arduino IDE, go to the top menu, click on “Sketch”, then “Include Library”, “Manage Libraries…”. Then type “AccelStepper”, then click install on the row for the AccelStepper library.
Example 1 – Move from A to B by a specific number of steps
In this example we will define the speed and acceleration at which we want the stepper motor to rotate and then make it rotate by a specified number of steps, stop, then rotate in the opposite direction in a specified number of steps. Specifically, we will do 3 rotations in one direction (meaning total steps will be the steps per revolution multiplied by 3 revolutions), stop and complete 3 rotations in the opposite direction.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
//Arduino Code - Move from A to B by a specific number of steps with set speed and acceleration // Include the Arduino Stepper.h library: #include <AccelStepper.h> //Include the AccelStepper library // Define the motor pins: #define MP1 8 // IN1 on the ULN2003 #define MP2 9 // IN2 on the ULN2003 #define MP3 10 // IN3 on the ULN2003 #define MP4 11 // IN4 on the ULN2003 #define MotorInterfaceType 8 // Define the interface type as 8 = 4 wires * step factor (2 for half step) AccelStepper stepper = AccelStepper(MotorInterfaceType, MP1, MP3, MP2, MP4);//Define the pin sequence (IN1-IN3-IN2-IN4) const int SPR = 2048;//Steps per revolution void setup() { stepper.setMaxSpeed(1000);//Set the maximum motor speed in steps per second stepper.setAcceleration(200);//Set the maximum acceleration in steps per second^2 } void loop() { stepper.moveTo(3*SPR); //Set the target motor position (i.e. turn motor for 3 full revolutions) stepper.runToPosition(); // Run the motor to the target position delay(1000); stepper.moveTo(-3*SPR);//Same as above: Set the target motor position (i.e. turn motor for 3 full revolutions) stepper.runToPosition(); // Run the motor to the target position delay(1000); } |
Example 2 – Move the stepper motor by controlling with a rotary encoder
As the example title says, in this example we will control the rotation of a stepper motor using a rotary encoder. For details on how to use a rotary encoder, please take a look at my earlier post.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
//Arduino Code - Move stepper motor based on movement of rotary encoder // Include the Arduino Stepper.h library: #include <AccelStepper.h> //Include the AccelStepper library // Define the motor pins: #define MP1 8 // IN1 on the ULN2003 #define MP2 9 // IN2 on the ULN2003 #define MP3 10 // IN3 on the ULN2003 #define MP4 11 // IN4 on the ULN2003 #define MotorInterfaceType 8 // Define the interface type as 8 = 4 wires * step factor (2 for half step) AccelStepper stepper = AccelStepper(MotorInterfaceType, MP1, MP3, MP2, MP4);//Define the pin sequence (IN1-IN3-IN2-IN4) const int SPR = 2048;//Steps per revolution int DT = 4; //GPIO #4-DT on encoder (Output B) int CLK = 5; //GPIO #5-CLK on encoder (Output A) int counter = 0; int angle = 0; int aState; int aLastState; void setup() { stepper.setMaxSpeed(1000);//Set the maximum motor speed in steps per second stepper.setAcceleration(1000);//Set the maximum acceleration in steps per second^2 pinMode(CLK, INPUT_PULLUP); pinMode(DT, INPUT_PULLUP); aLastState = digitalRead(CLK); } void loop() { aState = digitalRead(CLK); //Encoder rotation tracking if (aState != aLastState) { if (digitalRead(DT) != aState) { counter ++; angle ++; } else { counter--; angle --; } if (counter >= 100 ) { counter = 100; } if (counter <= -100 ) { counter = -100; } aLastState = aState; } stepper.moveTo(200*counter); //Set the target motor position (multiply by whatever number you want to define how much to move per encoder pulse) stepper.runToPosition(); // Run the motor to the target position } |
Components used in this example
*As an Amazon & Ebay Associate I earn from qualifying purchases.
Component | Link |
Arduino UNO | https://amzn.to/3uYVAMC https://ebay.us/veZdKX |
Stepper Motor (28BYJ-48) with motor controller (ULN2003) | https://amzn.to/3kX6JKD https://ebay.us/hOrkrQ |
Itís hard to find knowledgeable people for this topic, but you seem like you know what youíre talking about! Thanks
Itís nearly impossible to find educated people about this subject, however, you seem like you know what youíre talking about! Thanks
Im pretty pleased to discover this page. I wanted to thank you for ones time for this particularly wonderful read!! I definitely liked every little bit of it and I have you bookmarked to look at new stuff on your blog.
I’m often to blogging and i really appreciate your content. The article has actually peaks my interest. I’m going to bookmark your web site and maintain checking for brand spanking new information.
I like the efforts you have put in this, regards for all the great content.
Nice post. I learn something totally new and challenging on websites