Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 37 ยท Sensors and Actuators

Stepper Motors

Use stepper motors with Arduino: how steps and coils work, full and half stepping, the 28BYJ-48 with ULN2003, the Stepper library, A4988 drivers and timing.

A DC motor spins freely, and a servo goes to an angle within a limited range. A stepper motor moves in small, precise steps: each pulse to the controller turns the shaft by a fixed angle, typically 1.8 degrees (200 steps per revolution) or less. Count the pulses and you know the position without any feedback sensor. That is why steppers drive 3D printers, CNC machines, camera sliders and plotters.

No motor or board runs on this site. The Arduino code is shown read-only; the C programs are labelled stand-ins that reproduce the coil patterns and the step arithmetic.

How a stepper works

Inside are electromagnet coils arranged around a toothed rotor. Energizing the coils in a repeating sequence pulls the rotor from one position to the next, like a rotating magnetic field it follows. A common unipolar motor with four coil wires is driven by changing which of the four coils are on:

Step Coil 1 Coil 2 Coil 3 Coil 4
1 on off off off
2 off on off off
3 off off on off
4 off off off on

Stepping through the table forward turns the shaft one way; going backward reverses it. This is full stepping (wave drive). Energizing two coils together gives more torque, and alternating one and two coils creates half steps, doubling the resolution with eight phases.

The motor draws far too much current for pins, so a driver sits between: the ULN2003 board for the small 28BYJ-48, or modules such as the A4988 and DRV8825 for NEMA motors in 3D printers.

The 28BYJ-48 with ULN2003

This cheap 5 V motor has a gearbox: about 2048 full steps (4096 half steps) per output revolution. It is slow and weak but great for learning. The Stepper library controls it:

C++
#include <Stepper.h>

const int STEPS_PER_REV = 2048;
// Pin order for a 28BYJ-48 is IN1, IN3, IN2, IN4
Stepper motor(STEPS_PER_REV, 8, 10, 9, 11);

void setup() {
  motor.setSpeed(10);             // revolutions per minute
}

void loop() {
  motor.step(STEPS_PER_REV);      // one full turn clockwise
  delay(500);
  motor.step(-STEPS_PER_REV);     // one full turn back
  delay(500);
}

setSpeed(rpm) only stores a delay between steps. step(n) is blocking: your sketch waits until all n steps are done. For a sketch that must keep reading buttons, use the AccelStepper library (run() called every loop moves one step when it is due) or write your own timed stepper.

Note the unusual pin order. Wire a 28BYJ-48 wrongly and it vibrates without turning. The order is easy to confirm on the library's examples.

C Stand-in: half-step coil pattern (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Step arithmetic

Positions are arithmetic on steps. For a motor with S steps per revolution:

  • steps for an angle: steps = angle * S / 360
  • speed in rpm to delay per step: delay_us = 60,000,000 / (S * rpm)
  • distance on a belt or lead screw: steps = distance / distance_per_step

The motor cannot go arbitrarily fast. Beyond a certain rate it loses steps because the rotor cannot keep up with the changing field, and in an open-loop system you will not know. Accelerate gradually, and keep the speed below the torque curve of your motor.

C Stand-in: step counts and delays (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Drivers with STEP and DIR: A4988 and DRV8825

NEMA 17 motors in printers are driven by modules with just two control signals. Each pulse on STEP moves one (micro)step; DIR sets direction. The driver handles the coil currents, limits them to protect the motor (you tune it with a small potentiometer) and supports microstepping (1/2, 1/4, ... 1/16 or more), which gives smoother, quieter motion.

C++
const int STEP_PIN = 3;
const int DIR_PIN = 4;

void setup() {
  pinMode(STEP_PIN, OUTPUT);
  pinMode(DIR_PIN, OUTPUT);
  digitalWrite(DIR_PIN, HIGH);        // direction
}

void loop() {
  for (int i = 0; i < 200; i++) {     // 200 pulses = one turn at full steps
    digitalWrite(STEP_PIN, HIGH);
    delayMicroseconds(800);
    digitalWrite(STEP_PIN, LOW);
    delayMicroseconds(800);
  }
  delay(1000);
}

The shorter the pulse delay, the faster the motor. At 1/16 microstepping you need 3200 pulses per revolution, so your code must generate pulses quickly; this is a good reason to use a library for acceleration ramps.

Practical tips

  • Always set the driver current limit before connecting a valuable motor, and never disconnect a motor while powered: coil spikes can kill the driver.
  • Power the motor from its own supply (8 to 35 V for typical drivers), with a 100 microfarad electrolytic capacitor close to the driver.
  • A stepper holds position by keeping the coils energized, which makes it hot; disable the driver when idle if holding is not needed.
  • If it stutters instead of turning, the speed is too high, the coil order is wrong, or the supply is too weak.
  • For position feedback or the ability to know about lost steps, choose a motor with an encoder or use a servo instead (see servo motors).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A motor has 200 full steps per revolution. How many degrees is one step?

    360 degrees divided by 200 steps is 1.8 degrees per step.
  2. 2. What does motor.step(100) do in the Stepper library?

    step() is blocking: your sketch waits until all steps have been made. For concurrent tasks use a non-blocking approach such as AccelStepper.
  3. 3. What are the two control signals of an A4988 driver?

    Each pulse on STEP makes one step; DIR sets the direction.
  4. 4. Why might an open-loop stepper silently lose position?

    Open-loop systems only count commanded steps. If the rotor misses steps there is no feedback to reveal it.

Summary

  • A stepper moves a fixed angle per step, so position equals counted steps (open loop).
  • Coil sequences (full, half step) turn the rotor; drivers such as the ULN2003, L298N or A4988 carry the current.
  • Convert angles and speeds to steps and delays with simple arithmetic; accelerate gradually.
  • The Stepper library blocks; use timed code or AccelStepper when the sketch must stay responsive.

Next: The Serial Monitor and Printing Data