The Last Lock
The emergency bunker door stands between you and certain death. Outside, the toxic storms rage with unprecedented fury, their chemical winds dissolving anything in their path. The mechanical lock mechanism, a relic from the old world, requires precise rotation patterns to unlock. Your fingers trace the cold metal surface, feeling the intricate gears within.
But there's a problem. The lock's manual handle snapped off decades ago, leaving only a stubborn shaft that refuses to budge under human strength. The radiation detector on your wrist clicks faster. Time is running out. You need mechanical advantage, something that can generate the precise rotational force required to turn those ancient gears.
In your salvage kit, you spot it: a stepper motor, one of the few pieces of pre-war tech that still functions reliably. Unlike the chaotic spinning of regular motors, steppers move with surgical precision, turning exact amounts on command. Perfect for delicate lock mechanisms that demand specific rotation patterns.
You wire the motor to your HERO Board, your fingers working quickly as the storm intensifies. Each connection must be perfect. Each line of code must execute flawlessly. Because in the wasteland, there are no second chances, and precision isn't just important—it's survival.
What You'll Learn
When you finish this lesson, you'll be able to:
- Control stepper motors for precise rotational movement
- Understand the difference between stepper motors and regular DC motors
- Implement bidirectional rotation with exact step control
- Use the Stepper library to manage complex motor sequences
- Calculate steps per revolution for different motor types
- Apply stepper motors to real-world precision applications
Understanding Stepper Motors
Think of the difference between a regular motor and a stepper motor like the difference between a skateboard and a chess piece. A regular DC motor spins continuously, like a skateboard wheel rolling down a hill—fast, but with little control over where it stops. A stepper motor moves like a chess piece: one precise square at a time, stopping exactly where you command it.
Inside a stepper motor, electromagnets arranged around the rotor create magnetic fields that pull the motor shaft in discrete steps. Each electrical pulse moves the motor one step forward (or backward). This design makes steppers perfect for applications requiring exact positioning: 3D printers moving print heads to precise coordinates, robotic arms reaching specific angles, or camera gimbals tracking subjects with smooth, controlled motion.
The magic number for most hobby stepper motors is 2048 steps per full revolution. This means the motor divides a complete 360-degree rotation into 2048 tiny movements, each about 0.18 degrees. This level of precision transforms crude electrical signals into surgical mechanical control—exactly what you need when every degree of rotation matters.
Unlike DC motors that require complex feedback systems to know their position, steppers are inherently self-tracking. Send 1024 steps, and you know the motor has turned exactly half a revolution. No sensors needed, no guesswork involved. Pure, predictable mechanical precision.
Wiring the Stepper Motor
Stepper motors require four control pins because they contain two separate coils that must be energized in specific sequences. Think of it like a two-person rowing team\u2014both rowers must coordinate their strokes to move the boat forward smoothly.

- Pin 8 to IN1: Controls the first coil's positive terminal. This pin energizes one electromagnet inside the motor.
- Pin 9 to IN2: Controls the first coil's negative terminal. Together with IN1, this creates the magnetic field for coil A.
- Pin 10 to IN3: Controls the second coil's positive terminal. This manages the second electromagnet pair.
- Pin 11 to IN4: Controls the second coil's negative terminal. Completes the control circuit for coil B.
- Power connections: The motor driver board handles the heavy current requirements that would damage your HERO Board's pins.
Never connect a stepper motor directly to your HERO Board pins. The motor draws more current than the microcontroller can safely provide. Always use a motor driver board (like the ULN2003) to handle the power requirements.
Code Walkthrough: Setting Up the Stepper
Library and Constants
#include
const int stepsPerRevolution = 2048;
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);