In a previous guide, we’ve discussed how to use servos to control physical objects with your Arduino. Step motors can take your projects further, letting you rotate a motor with more precision and stability than you get from servo motors. This guide will show you how to control a stepper motor with Arduino boards.
Arduino step motors work similarly to servos, but with some key differences that make it easier to gain precise control over the motor’s movements. In a step motor, four coils form a ring around a rotor. Each coil is activated in succession, magnetically drawing the rotor towards it, rotating it 90 degrees. While this makes stepper motors slower than servos, they’re also much more precise, allowing for extremely controlled movement with minimal vibration. There are many types of Arduino step motors, but for this project we’ll be using the common 28BYJ-48.
To control this step motor, you’ll also need a microcontroller that will handle all the complex movements. The step motor uses a high current, which means an IC like the ULN2003 is needed to control it. This driver board allows the low current Arduino to control the high current step motor. While you can find the IC on its own and wire it up manually, it’s a lot easier to find a driver board with the IC integrated directly into it.
We’ll also be using the built-in Stepper.h library which will make sending instructions to the step motor much simpler. This library will let you set the speed or number of steps that your motor will take with just a single line of code. This library is built into the Arduino IDE, so you won’t need to do anything special to enable it, but you can check out the documentation at the link above to see how it will work.
It’s also important to point out that since the step motor draws a high current, it’s best to use a separate power source for your final projects, rather than powering it off the Arduino. For the simple demonstration sketch in this guide, we’ll be using the 5V pin on the Arduino, but it can cause performance problems or even damage your board if you expand this project without carefully managing your power consumption. To keep everything working properly, make sure to power your step motor separately.
If you choose to get the ULN2003 driver board (which we highly recommend for this project), then the components for this project get a lot simpler. If not, you’ll need more wiring for your breadboard. In general, here’s what you’ll need:
![]() |
|
Step motor (we’ll be using a 28BYJ-48) |
![]() |
Step motor driver board (we’ll use the ULN2003) |
![]() |
![]() |
|
![]() |
|
Power source, such as a 9V battery (to expand your project) |
![]() |
As explained above, the extra power source is to stabilize your step motor if you decide to expand this project. The demonstration sketch below should work safely with your Arduino Uno, but if you add more components that can draw extra power from your Arduino, be sure to swap out the Arduino power for an external power source.
For this project, we’re simply going to rotate the step motor one direction, and then the other. Take the code below and upload it to your Arduino.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include #define STEPS 2038 Stepper stepper(STEPS, 8, 10, 9, 11); void setup() { } void loop() { stepper.setSpeed(1); stepper.step(2038); delay(1000); stepper.setSpeed(5); stepper.step(-2038); } |
Now, let’s break down the code step by step (so to speak!).
1 2 3 4 5 |
#include #define STEPS 2038 Stepper stepper(STEPS, 8, 10, 9, 11); |
The first line here will tell the sketch to include the Stepper.h library, which you’ll need for controlling the step motor. Next, you’ll define the steps variable, which will determine how many steps are required for a full revolution. In this step motor’s case, this number is 2038, but check the datasheet for your motor, if you’re using a different component.
Finally, we’ll define a stepper object and assign it to pins 8, 10, 9, and 11, which will control the coils inside the step motor independently.
1 2 3 4 5 6 7 |
void loop() { stepper.setSpeed(1); stepper.step(2038); delay(1000); stepper.setSpeed(5); stepper.step(-2038); } |
Inside the main loop, we’ll use two commands to set the speed the motor should turn at, and how far it should go. In this case, we’ll spin it for 2038 (or a full revolution) at 1 rotation per minute (or rpm). This will show you the baseline speed that the step motor will turn.
Next, the code will delay for one second before turning back in the opposite direction (as indicated by using a negative value on the number of steps to take), but this time we’ll change the speed to 5rpm. Now, you’ll see the step motor move more quickly. This can help you get a frame of reference for how to control the motion of your step motor.
If you’re using the recommended driver board, then wiring for this project is very simple. You’ll only need six wires. However, keep in mind that this project will be drawing power from the Arduino. If you expand this project at all, substitute a separate power source (such as a 9V battery, but check the requirements of your step motor for precise power requirements) in the places below where power and ground are connected to the Arduino.
To follow our demo sketch, connect these wires:
Once these parts are connected, you should be able to control the motor with just a few lines of code in your Arduino. Once you’ve connected a separate power source to your driver board, try experimenting with the code to turn based on input from a sensor or changing the speed or direction at which they turn.