If you want an engine to just spin continuously, a standard DC motor works perfectly. If you want an arm to sweep precisely back and forth to specific angles, a servo motor is your best friend. But what if you want something that can turn exactly 360 degrees and stop precisely on a dime, rotate a conveyor belt a specific distance, or drive the axis of a 3D printer?
For that, you need a Stepper Motor.
A stepper motor and its companion driver board allow your Arduino to control rotation with step-by-step precision.
Unlike standard DC motors that spin continuously when power is applied, a stepper motor rotates in distinct, discrete “steps.” Inside the motor casing, there is a central rotor shaped like a gear, surrounded by multiple electromagnetic coils.
By energizing these coils in a specific sequence, the motor is pulled magnetically from one position to the next. Because the rotation is based on these physical “teeth” and coil alignments, you always know exactly how far the motor has turned by simply counting the number of steps you’ve commanded it to take.
We’ll be using the ubiquitous 28BYJ-48 Stepper Motor, paired with a ULN2003 Motor Driver board. Just like with DC motors, the Arduino cannot provide enough current directly from its pins to power the coils inside the stepper. The ULN2003 sits in the middle, taking the weak digital step signals from the Arduino and switching on the heavier current required by the motor.
The ULN2003 board makes wiring the 28BYJ-48 incredibly simple.
Arduino makes controlling stepper motors incredibly easy using the built-in Stepper.h library. Let’s write a simple program to make the motor spin one full revolution in one direction, pause, and then spin back.
#include <Stepper.h>
// The 28BYJ-48 motor has 2048 steps per full revolution
const int stepsPerRevolution = 2048;
// Initialize the stepper library on pins 8 through 11:
// Note the pin order: 8, 10, 9, 11 to match the coil sequence
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);
void setup() {
// Set the motor speed to 10 RPM
myStepper.setSpeed(10);
Serial.begin(9600);
}
void loop() {
Serial.println("Clockwise Rotation");
// Step forward 2048 steps (1 full revolution)
myStepper.step(stepsPerRevolution);
delay(500);
Serial.println("Counter-Clockwise Rotation");
// Step backward 2048 steps
myStepper.step(-stepsPerRevolution);
delay(500);
}
#include <Stepper.h>: This line imports the necessary library.stepsPerRevolution = 2048: The 28BYJ-48 motor uses internal gearing, resulting in 2048 tiny steps needed to make the outer shaft complete one full 360-degree rotation.Stepper myStepper(...): We create a Stepper object. Note the pin order! Due to how the internal coils of the 28BYJ-48 align with the ULN2003 pins, we pass them as 8, 10, 9, 11 rather than strictly numerical.setSpeed(10): Sets the rotation speed to 10 Revolutions Per Minute (RPM).step(stepsPerRevolution): Tells the motor to take 2048 steps forward. Using a negative number (-stepsPerRevolution) reverses the direction.Upload this code, and watch the precise, deliberate rotation of your new stepper motor! Steppers are the fundamental building blocks of robotics, CNC machines, and 3D printers.