7  Stepper Motors

In 60 Seconds

Stepper motors move in precise discrete steps (typically 1.8 degrees per step) and provide open-loop position control without feedback sensors. They excel in applications requiring exact positioning like 3D printers and CNC machines, but need acceleration profiles to prevent missed steps and draw continuous holding current even when stationary.

Key Concepts
  • Stepper Motor Operation: A brushless DC motor that moves in discrete angular steps by sequentially energizing coils; each step is a fixed angle (0.9, 1.8, or 3.6 degrees typically); enables precise open-loop position control without a position sensor
  • Step Modes: Full step (one coil at a time): maximum torque, coarser resolution; half step (alternating one and two coils): smoother motion, 2x resolution; microstepping (variable current in adjacent coils): very smooth motion, up to 256x resolution per full step
  • Stepper Motor Driver IC: Dedicated ICs (A4988, DRV8825, TMC2208) that handle the coil energization sequence, current limiting, and microstepping; accept simple STEP/DIR logic signals from the MCU, simplifying firmware to just controlling pulse rate and direction
  • Holding Torque: The torque a stepper motor exerts when powered but stationary to resist external forces trying to move the shaft; enables passive position holding without a brake; drops significantly when power is removed (detent torque is much lower)
  • Steps Per Revolution: The number of full steps to complete one full rotation; a standard 200-step motor (1.8 degree per step) needs 200 steps for 360 degrees; with 16x microstepping, this becomes 3200 microsteps per revolution
  • Resonance and Mid-Frequency Instability: At certain speeds, the step frequency matches the rotor’s natural mechanical resonance frequency, causing vibration, noise, and missed steps; mitigated by microstepping, mechanical damping, or driving through the resonant speed range quickly
  • Reference Current (Vref) Setting: Stepper drivers like A4988 and DRV8825 require setting the maximum coil current via a potentiometer (Vref); incorrect Vref causes overheating (too high) or insufficient torque and missed steps (too low); calculate from motor’s rated current per phase
  • Acceleration Profiles: Ramping step frequency from low to high (acceleration) and high to low (deceleration) prevents step loss at high speeds; the maximum speed without losing steps depends on load, current setting, and supply voltage; libraries like AccelStepper implement S-curve profiles automatically

Learning Objectives

After completing this chapter, you will be able to:

  • Describe stepper motor construction and differentiate full-step, half-step, and microstepping sequences
  • Control stepper motors with ULN2003 and A4988/DRV8825 drivers
  • Implement acceleration profiles to prevent missed steps
  • Apply the AccelStepper library to generate smooth trapezoidal motion profiles
  • Configure microstepping for increased resolution
  • Calculate torque, resolution, and travel time when sizing stepper motors for precision applications

A stepper motor moves in tiny, precise clicks – like the second hand on a clock ticking from one mark to the next. Each electrical pulse makes the motor rotate by exactly one small step (often just 1.8 degrees). By counting these steps, you always know exactly where the motor is positioned without needing any additional sensors. This is why stepper motors are used in 3D printers and CNC machines where precise movement is essential.

7.1 Stepper Motor Fundamentals

Stepper motors move in discrete steps, providing precise position control without feedback sensors (open-loop control).

How It Works: Stepper Motor Step-by-Step Operation

Think of a stepper motor like a clock’s second hand that can only move to specific tick marks. Here’s what happens with each electrical pulse:

Step 1: Energize First Coil - Electricity flows through coil A, creating a magnetic field that pulls the rotor’s permanent magnet teeth to align with coil A’s poles. The rotor snaps to position 0°.

Step 2: Switch to Next Coil - Coil A turns off, coil B turns on. The magnetic field shifts 90°, and the rotor follows, snapping to position 90° (or 1.8° for a 200-step motor).

Step 3: Continue the Sequence - By energizing coils in order (A → B → C → D → A…), the rotor steps around the circle precisely. Each pulse = one fixed angular movement.

Real-World Analogy: Imagine a gear with 200 teeth locked between 4 electromagnets positioned around a circle. Turn on the north electromagnet, and the gear’s nearest tooth snaps to it. Turn off north, turn on east – the gear rotates exactly one tooth. No encoder needed; you count pulses and know the exact position.

Why No Feedback Needed? Each step is mechanically fixed by the motor’s tooth count and coil positions. Send 100 pulses → rotor moves exactly 100 steps (unless it skips due to overload).

Characteristics:

  • Precise angular positioning (typically 1.8 deg/step = 200 steps/rev)
  • Open-loop control (no encoder needed)
  • High holding torque at standstill
  • Step-by-step movement (can be jerky at low speeds)
  • Higher power consumption than servos

A NEMA 17 stepper with 200 steps/revolution moves \(\theta = 360° / 200 = 1.8°\) per step. With a lead screw (2mm pitch), one full rotation moves the carriage \(d = 2\) mm, so each step moves \(d_{step} = 2/200 = 0.01\) mm = 10 micrometers. At 1/16 microstepping (3200 steps/rev), resolution becomes \(0.01 / 16 = 0.000625\) mm = 0.625 micrometers per microstep. To move 50mm requires \(n = 50 / 0.01 = 5000\) full steps. At 800 steps/s, travel time is \(t = 5000/800 = 6.25\) s.

7.1.1 Step Sequences

Full-Step Sequence (28BYJ-48 with ULN2003):

Step IN1 IN2 IN3 IN4
1 1 0 0 0
2 0 1 0 0
3 0 0 1 0
4 0 0 0 1

Half-Step Sequence (smoother, double resolution):

Step IN1 IN2 IN3 IN4
1 1 0 0 0
2 1 1 0 0
3 0 1 0 0
4 0 1 1 0
5 0 0 1 0
6 0 0 1 1
7 0 0 0 1
8 1 0 0 1

7.2 28BYJ-48 with ULN2003

The 28BYJ-48 is a popular low-cost stepper for hobby projects.

Specifications:

  • Steps per revolution: 2048 (with 64:1 gearbox)
  • Operating voltage: 5V DC
  • Current: 200-300mA
  • Step angle: 5.625 deg/64 = 0.18 deg effective
#include <AccelStepper.h>

// 28BYJ-48 with 64:1 gearbox = 2048 steps per revolution
AccelStepper stepper(AccelStepper::HALF4WIRE, 19, 5, 18, 17);
const int STEPS_PER_REV = 2048;

void setup() {
  Serial.begin(115200);
  stepper.setMaxSpeed(1000);       // Steps per second
  stepper.setAcceleration(500);    // Steps per second^2
  Serial.println("Commands: 0-360 = angle, h = home, f = full rotation");
}

void loop() {
  if (Serial.available()) {
    String input = Serial.readStringUntil('\n');
    input.trim();

    if (input == "h") {
      stepper.moveTo(0);                                  // Home
    } else if (input == "f") {
      stepper.move(STEPS_PER_REV);                        // Full rotation
    } else {
      int angle = input.toInt();
      if (angle >= 0 && angle <= 360) {
        stepper.moveTo((long)angle * STEPS_PER_REV / 360);  // Go to angle
        Serial.printf("Moving to %d degrees\n", angle);
      }
    }
  }
  stepper.run();  // Must be called frequently — handles acceleration
}

7.3 NEMA 17 with A4988/DRV8825

For higher performance, NEMA 17 steppers with A4988 or DRV8825 drivers are the standard choice.

NEMA 17 Specifications:

  • Steps per revolution: 200 (1.8 deg/step)
  • Holding torque: 40+ kg-cm
  • Operating current: 1.2-2.0A per phase
  • Voltage: 12-24V

A4988 Driver Features:

  • Microstepping: 1, 1/2, 1/4, 1/8, 1/16
  • Current limit adjustment
  • Sleep mode for power saving
  • Step/direction interface

Microstepping Settings:

MS1 MS2 MS3 Resolution Steps/Rev
0 0 0 Full step 200
1 0 0 1/2 step 400
0 1 0 1/4 step 800
1 1 0 1/8 step 1600
1 1 1 1/16 step 3200
#include <AccelStepper.h>

#define STEP_PIN 25
#define DIR_PIN 26
#define ENABLE_PIN 27

// NEMA 17 with 1/16 microstepping = 3200 steps per revolution
AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);

const int STEPS_PER_REV = 3200;

void setup() {
  Serial.begin(115200);

  pinMode(ENABLE_PIN, OUTPUT);
  digitalWrite(ENABLE_PIN, LOW);  // Enable driver (active LOW)

  stepper.setMaxSpeed(3000);      // Steps per second
  stepper.setAcceleration(1000);  // Steps per second^2

  Serial.println("NEMA 17 Stepper Ready");
}

void loop() {
  if (Serial.available()) {
    char cmd = Serial.read();

    switch(cmd) {
      case 'f':  // Forward one revolution
        stepper.move(STEPS_PER_REV);
        break;
      case 'b':  // Backward one revolution
        stepper.move(-STEPS_PER_REV);
        break;
      case 'h':  // Home position
        stepper.moveTo(0);
        break;
      case 's':  // Stop
        stepper.stop();
        break;
      case 'd':  // Disable (power saving)
        digitalWrite(ENABLE_PIN, HIGH);
        break;
      case 'e':  // Enable
        digitalWrite(ENABLE_PIN, LOW);
        break;
    }
  }

  stepper.run();
}

7.4 Acceleration Profiles

Why Acceleration Matters

Steppers cannot instantly change speed. Attempting to start at full speed causes missed steps because the rotor can’t keep up with the magnetic field changes.

Trapezoidal velocity profile:

  1. Acceleration phase: Gradually increase speed
  2. Constant velocity: Maintain max speed during travel
  3. Deceleration: Gradually decrease speed to prevent overshoot

The AccelStepper library handles this automatically!

Worked Example: Sizing a Stepper for a Linear Actuator

Scenario: You need to move a 3D printer’s Z-axis platform weighing 2 kg through 200 mm of travel using a lead screw with 8 mm pitch (one revolution = 8 mm linear travel).

Step 1 – Calculate required revolutions:

  • Total travel: 200 mm
  • Lead screw pitch: 8 mm/rev
  • Revolutions needed: 200 / 8 = 25 revolutions

Step 2 – Calculate required steps (NEMA 17 at 1/16 microstepping):

  • Steps per revolution: 200 full steps x 16 microsteps = 3,200
  • Total steps: 25 x 3,200 = 80,000 steps

Step 3 – Calculate linear resolution:

  • Resolution per microstep: 8 mm / 3,200 = 0.0025 mm (2.5 micrometers)
  • This exceeds typical 3D printing needs (50-100 micrometers per layer)

Step 4 – Calculate required torque:

  • Force to lift 2 kg: F = m x g = 2 x 9.81 = 19.62 N
  • Lead screw torque: T = F x pitch / (2 x pi x efficiency)
  • Assuming 40% efficiency: T = 19.62 x 0.008 / (2 x 3.14159 x 0.4) = 0.0625 Nm = 6.25 Ncm
  • With 2x safety margin: 12.5 Ncm – a standard NEMA 17 (40+ Ncm) has ample torque

Step 5 – Estimate travel time at 1,000 steps/sec max speed with 500 steps/sec2 acceleration:

  • Acceleration time to max speed: t_accel = v_max / a = 1000 / 500 = 2 seconds
  • Acceleration distance: s_accel = 1/2 × a × t2 = 0.5 x 500 x 4 = 1,000 steps
  • Deceleration distance: s_decel = 1,000 steps (same)
  • Constant-speed distance: 80,000 - 2,000 = 78,000 steps
  • Constant-speed time: t_const = 78,000 / 1000 = 78 seconds
  • Total time: 2 seconds (accel) + 78 seconds (constant) + 2 seconds (decel) = 82 seconds for 200 mm travel
Connection: Stepper Motors and PID Control

Stepper motors provide open-loop positioning, but many IoT applications combine them with closed-loop PID controllers for higher reliability. When a stepper drives a process variable (temperature via a mixing valve, flow rate via a metering pump), the PID controller commands position changes while the stepper executes them precisely. See PID Control Fundamentals for the control theory behind this pairing.

// Manual trapezoidal profile (if not using AccelStepper)
void moveWithAcceleration(int targetSteps, int maxSpeed, int acceleration) {
  int currentSpeed = 0;
  int position = 0;
  int direction = (targetSteps > 0) ? 1 : -1;
  targetSteps = abs(targetSteps);

  // Calculate distances for accel/decel phases
  int accelSteps = (maxSpeed * maxSpeed) / (2 * acceleration);
  int decelSteps = accelSteps;
  int constSteps = targetSteps - accelSteps - decelSteps;

  if (constSteps < 0) {
    // Can't reach max speed, use triangular profile
    accelSteps = targetSteps / 2;
    decelSteps = targetSteps - accelSteps;
    constSteps = 0;
    maxSpeed = sqrt(2 * acceleration * accelSteps);
  }

  // Acceleration phase
  for (int i = 1; i <= accelSteps; i++) {
    currentSpeed = sqrt(2 * acceleration * i);
    step(direction);
    delayMicroseconds(1000000 / currentSpeed);
  }

  // Constant speed phase
  for (int i = 0; i < constSteps; i++) {
    step(direction);
    delayMicroseconds(1000000 / maxSpeed);
  }

  // Deceleration phase
  for (int i = decelSteps; i > 0; i--) {
    currentSpeed = sqrt(2 * acceleration * i);
    step(direction);
    delayMicroseconds(1000000 / currentSpeed);
  }
}

7.5 Common Pitfalls

Pitfall: Undersized Current Limiting for Stepper Motors

The Mistake: Setting the stepper driver current limit to the motor’s rated current (e.g., 2.0A) without considering that stepper motors are rated for maximum holding torque at stall, not continuous operation.

Why It Happens: Datasheets list “rated current per phase” which represents the maximum the motor can handle when stationary with proper heat sinking.

The Fix: Start with 70-80% of rated current (e.g., 1.4-1.6A for a 2.0A motor) and increase only if torque is insufficient. Monitor motor temperature after 30 minutes of continuous operation - the case should not exceed 70-80C.

Pitfall: Using Open-Loop Control for Precision Applications

The Mistake: Assuming a stepper motor will always reach the commanded position without feedback, then discovering position errors accumulate over time.

Why It Happens: Stepper motors are marketed as “precise” (1.8 deg/step), creating false confidence. In reality, missed steps from overloading, mechanical binding, or acceleration too fast cause cumulative drift.

The Fix: For precision applications, add encoder verification or use limit switches for homing/calibration routines.

7.6 Interactive Lab: Stepper Precision

Challenges:

  1. Make the stepper rotate exactly 90 degrees
  2. Implement a clock with the second hand driven by the stepper
  3. Add buttons for forward/reverse step control

7.7 Knowledge Check

    1. Acceleration profiles reduce power consumption
    1. The rotor cannot keep up with sudden magnetic field changes, causing missed steps and lost position
    1. Stepper drivers require a warm-up period
    1. It makes the motor quieter

Answer: B) The rotor cannot keep up with sudden magnetic field changes, causing missed steps and lost position. Stepper motors have physical inertia. If the magnetic field switches faster than the rotor can follow, the rotor falls out of sync with the coil energizing sequence, resulting in missed steps. Since stepper motors use open-loop control (no feedback), these missed steps cause cumulative position errors that cannot be detected without external sensors.

    1. 200 steps per revolution with higher torque
    1. 3200 steps per revolution for smoother, higher-resolution movement
    1. 16 steps per revolution for faster rotation
    1. 200 steps per revolution with less noise

Answer: B) 3200 steps per revolution for smoother, higher-resolution movement. Microstepping divides each full step into smaller increments by varying the current to each coil. At 1/16 microstepping, each of the 200 full steps becomes 16 microsteps, yielding 200 x 16 = 3200 steps per revolution. This provides smoother motion and finer positioning (0.1125 degrees per microstep) at the cost of slightly reduced holding torque per microstep.

Key Takeaway

Stepper motors provide precise, repeatable positioning through discrete steps without requiring feedback sensors (open-loop control). The key to reliable operation is proper acceleration profiles to prevent missed steps, appropriate current limiting to prevent overheating, and selecting the right driver (ULN2003 for small steppers, A4988/DRV8825 for NEMA 17). For applications where accumulated position error is unacceptable, add encoder feedback or implement periodic homing routines with limit switches.

“I’m the most precise mover on the team!” announced Stepper Stella proudly. “Watch me take exactly 200 steps to go all the way around – tick, tick, tick, tick…”

“Why do you move in little ticks instead of spinning smoothly like DC Danny?” asked Lila the LED.

“Because each tick is exactly 1.8 degrees!” Stella explained. “If Max tells me to take 50 steps, I’ve turned exactly 90 degrees. No guessing, no feedback needed – I just count my steps!”

“That’s how 3D printers work!” said Sammy the Sensor excitedly. “They use stepper motors just like Stella to move the print head left, right, forward, and backward by exact amounts, building objects layer by layer!”

“But I have a weakness,” Stella admitted. “If something pushes against me too hard while I’m stepping, I can miss a step. And since I don’t have a sensor inside me like Servo Sam does, I won’t even know I’ve made a mistake! My count will be off from then on.”

“That’s why you need to start slowly,” Max the Microcontroller advised. “If I try to make Stella go from zero to super-fast instantly, her little rotor can’t keep up with the magnets switching, and she’ll stumble and lose her place.”

“It’s like running,” Bella the Battery added. “You can’t sprint from a standstill – you have to accelerate gradually. Stella does the same thing: start slow, speed up, then slow down again before stopping. We call it a trapezoidal speed profile!”

“And I do use a lot of energy even when standing still,” Stella confessed. “I need current through my coils to hold my position. So for battery projects, think carefully about whether you need me or Servo Sam!”

Concept Relationships: Stepper Motors in the IoT Ecosystem
Concept Relationship Why It Matters
PID Control Steppers provide position; PID controls process variables Stepper moves valve to exact position; PID determines target position based on temperature sensor feedback
Duty Cycle (PWM) Microstepping uses PWM-like current control Variable current ratios between coils create intermediate positions for smoother motion
DC Motors Trade precision for simplicity DC: fast rotation, no position tracking. Stepper: precise position, slower speed
Servo Motors Trade open-loop for closed-loop Servo: built-in encoder, self-correcting. Stepper: no feedback, cumulative error risk
Motor Drivers ULN2003, A4988 translate logic to power Microcontroller sends step pulses; driver handles high-current coil switching

Cross-Module Connections:

7.8 See Also

Related Actuator Topics:

  • Servo Motors - Closed-loop alternative with position feedback
  • DC Motors - Continuous rotation without position control
  • Motor Drivers and Control - Driver IC theory and application

Control Systems:

  • PID Control Fundamentals - Combining steppers with feedback control
  • Actuator Control Strategies - Open-loop vs closed-loop design patterns

Practical Implementation:

  • Arduino Motor Control - Complete code examples for ESP32/Arduino
  • 3D Printing Applications - Stepper-based motion systems

Time: 30 minutes | Difficulty: Intermediate | Hardware: 28BYJ-48 + ULN2003, ESP32, 5V power supply

Objective: Measure how stepper torque decreases as speed increases, and discover the “don’t start too fast” rule through hands-on experimentation.

What You’ll Need:

  • 28BYJ-48 stepper motor + ULN2003 driver board
  • ESP32 or Arduino
  • 5V 1A power supply
  • Small object to attach to motor shaft (paper disc with tape)

Procedure:

  1. Baseline Torque Test at 100 RPM:
    • Load the provided code, set speed = 100 (RPM)
    • Attach a paper disc to the shaft with a small weight (coin) taped at the edge
    • Start motor and observe smooth rotation
  2. Increase Speed to 500 RPM:
    • Change speed = 500
    • Motor should still rotate, but feel slightly weaker when you gently resist the shaft
  3. Increase Speed to 1000 RPM:
    • Change speed = 1000
    • Motor may vibrate, make noise, or stall completely
    • Why? At high speed, the rotor can’t keep up with the magnetic field switching – missed steps occur
  4. Test Instant Start at 1000 RPM:
    • Set speed to 1000 RPM but remove the setAcceleration() line
    • Motor will likely buzz but not rotate (stalled at startup)
    • Why? No acceleration means trying to go from 0 to full speed instantly – rotor has too much inertia
  5. Add Acceleration Profile:
    • Restore stepper.setAcceleration(500)
    • Motor now successfully ramps up to 1000 RPM
    • Why? Gradual acceleration lets the rotor keep pace with the field

What to Observe:

  • At low speeds (100-300 RPM), torque is high and smooth
  • At medium speeds (300-600 RPM), slight vibration and reduced torque
  • At high speeds (600+ RPM), significant torque loss and risk of stalling
  • Without acceleration, high-speed starts always fail

Hint: For maximum torque, keep speeds under 300 RPM for 28BYJ-48. For faster rotation, use NEMA 17 with higher current capacity.

Solution Code:

#include <AccelStepper.h>

#define MOTOR_PIN1 19
#define MOTOR_PIN2 18
#define MOTOR_PIN3 5
#define MOTOR_PIN4 17

AccelStepper stepper(AccelStepper::HALF4WIRE,
                     MOTOR_PIN1, MOTOR_PIN3, MOTOR_PIN2, MOTOR_PIN4);

const int STEPS_PER_REV = 2048;

void setup() {
  Serial.begin(115200);

  int speed_rpm = 100; // Change this: try 100, 300, 500, 1000
  int steps_per_sec = (speed_rpm * STEPS_PER_REV) / 60;

  stepper.setMaxSpeed(steps_per_sec);
  stepper.setAcceleration(500); // Try commenting this out for instant-start test

  Serial.print("Speed: ");
  Serial.print(speed_rpm);
  Serial.println(" RPM");
}

void loop() {
  stepper.moveTo(STEPS_PER_REV * 10); // Rotate 10 full revolutions
  while (stepper.distanceToGo() != 0) {
    stepper.run();
  }
  delay(2000);
  stepper.moveTo(0); // Return to start
  while (stepper.distanceToGo() != 0) {
    stepper.run();
  }
  delay(2000);
}

Extension Challenge: Add a tachometer (hall effect sensor + magnet on shaft) to measure actual RPM and compare to commanded RPM – discover at what speed missed steps begin!

7.9 What’s Next?

Now that you can control stepper motors with acceleration profiles and microstepping, explore these related topics to deepen your actuator and control systems knowledge.

Chapter Description
Relays and Solenoids On/off switching actuators for high-power loads and electromagnetic linear motion
Servo Motors Closed-loop alternative with built-in position feedback for angular control
DC Motors Continuous rotation motors with speed control via PWM
PWM Control Pulse-width modulation techniques used in microstepping current control
PID Control Fundamentals Combining stepper positioning with closed-loop feedback control
Actuator Safety Thermal management, current limiting, and fail-safe design for motor systems