If you want something to just spin recklessly, a DC motor works. If you want an arm to sweep back and forth, use a servo. But what if you need something to turn exactly 42 degrees and stop on a dime, like driving the axis of a 3D printer?
For that obsessive level of precision, you need a Stepper Motor. It doesnât just spin; it meticulously clicks from one exact position to the next. It is the control freak of the motor world.
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.