A stepper motor is easy to move with Raspberry Pi Pico if you are willing to block the CPU with delays. The harder—and more useful—problem is generating accurate STEP pulses while the rest of the firmware keeps reading sensors, updating communications and responding to inputs.

The solution is to separate motion timing from application timing. On RP2040 you can do that with hardware timers, PWM or PIO instead of a long loop full of sleep_us() calls.

Why the blocking version fails as the project grows

for (int i = 0; i < 2000; ++i) {
    gpio_put(STEP_PIN, 1);
    sleep_us(500);
    gpio_put(STEP_PIN, 0);
    sleep_us(500);
}

This may rotate the motor correctly, but the CPU spends the entire move waiting between edges. During that time your code may not service a user interface, network connection, second axis or application state machine on schedule.

A real motion subsystem should accept a target, generate pulses independently, report progress, and let the main application continue running.

Understand what the stepper driver expects

Drivers such as A4988, DRV8825 and TMC2209 usually expose a STEP/DIR interface. The host sets the direction level, then each valid STEP pulse advances the driver by one microstep according to the configured microstepping mode.

The driver’s datasheet defines minimum STEP high/low times, DIR setup/hold time, supply limits and current-setting rules. Those values—not a random delay from an example sketch—define the legal pulse waveform.

Method 1: schedule pulses with the RP2040 timer

The Pico SDK exposes the RP2040 hardware timer and higher-level alarm/repeating-timer APIs. RP2040 has a 64-bit microsecond counter and hardware alarms that can generate interrupts.

A timer callback can toggle the STEP output or schedule the next edge without stopping the main loop:

volatile uint32_t steps_remaining;

bool step_timer_callback(struct repeating_timer *t)
{
    if (steps_remaining == 0) {
        return false;
    }

    gpio_put(STEP_PIN, 1);
    busy_wait_us_32(STEP_HIGH_US);
    gpio_put(STEP_PIN, 0);

    --steps_remaining;
    return true;
}

This is already better than blocking the entire move, but keep callbacks short. Do not perform serial formatting, dynamic allocation or large calculations inside a time-critical callback.

A better timer design schedules edges, not complete application work

For higher step rates, avoid spending much of the callback busy-waiting for the pulse width. One approach is to alternate rising and falling edges with alarms, or use PWM/PIO to generate the physical pulse waveform while software only updates the requested rate.

The timer should own timing. The application should own decisions.

Method 2: use PWM for constant-speed motion

The RP2040 PWM peripheral runs independently of the CPU. Raspberry Pi documents eight PWM slices on RP2040, each capable of driving two PWM outputs.

If the stepper driver advances on a STEP edge, a square wave can generate a constant stream of steps. Set the PWM frequency to the required step rate and stop the PWM after the requested count using an interrupt or another counting mechanism.

PWM is strong when:

  • Step frequency is steady for a period.
  • The application needs low CPU overhead.
  • You can count completed pulses separately.
  • Acceleration updates happen at a much lower rate than individual pulses.

Method 3: use PIO for deterministic pulse generation

RP2040’s Programmable I/O (PIO) is designed for deterministic I/O tasks that would otherwise consume CPU time. A PIO state machine can generate STEP pulses with precise timing while a CPU core calculates acceleration, handles sensors or processes communication.

This becomes especially useful when:

  • You need multiple coordinated axes.
  • Step frequency is high.
  • Pulse timing must remain stable while the CPU is busy.
  • You want to feed a queue of timing values through FIFOs/DMA.

Do not jump to PIO before the architecture needs it

PIO is powerful, but a timer-driven design may be easier to maintain for one slow axis. Start with the simplest hardware-assisted method that meets the timing requirement, then move pulse generation into PIO when CPU jitter or multi-axis coordination becomes a real constraint.

Acceleration matters more than beginners expect

A stepper can miss steps even when every electrical pulse is valid if you command a high rate instantly. Rotor inertia and load torque limit how quickly the motor can accelerate.

Instead of jumping from 0 to the final step frequency, generate a ramp:

  1. Start with a longer step interval.
  2. Gradually reduce the interval as the motor accelerates.
  3. Maintain the cruise interval.
  4. Increase the interval again before stopping.

A trapezoidal velocity profile is a common starting point. More advanced motion systems use S-curve profiles to reduce jerk.

Keep the motion state explicit

typedef enum {
    MOTION_IDLE,
    MOTION_ACCEL,
    MOTION_CRUISE,
    MOTION_DECEL
} motion_state_t;

typedef struct {
    motion_state_t state;
    int32_t position_steps;
    int32_t target_steps;
    uint32_t step_interval_us;
} axis_t;

Now the application can ask the axis to move without executing the entire move inside one function.

Do not count commanded steps as guaranteed physical position

An open-loop stepper driver does not confirm that the rotor actually moved. If torque is insufficient, acceleration is too aggressive, current is set incorrectly or the supply collapses, commanded position and physical position diverge.

For machines that must know actual position, add homing switches, an encoder or another position reference appropriate to the risk.

Current limiting is mandatory

A stepper driver is not just a digital pulse translator. It controls coil current. Configure the driver’s current limit according to the motor and driver documentation.

Do not infer a safe current from the power-supply current rating. The driver regulates phase current, and incorrect setup can overheat the motor or driver.

Power integrity still matters

Motor current does not come from the Pico’s 3.3 V pin. Use a motor supply suitable for the driver and motor, keep grounds referenced correctly, and follow the driver’s decoupling/layout requirements.

If the Pico resets or misses steps when motion begins, read why stepper motors make Raspberry Pi Pico projects reset or miss steps.

Non-blocking application pattern

int main(void)
{
    motion_init();

    while (true) {
        motion_update_command();
        read_buttons();
        service_serial();
        update_display();

        if (motion_done()) {
            handle_move_complete();
        }

        tight_loop_contents();
    }
}

The important point is that motion_update_command() does not emit thousands of STEP pulses itself. Hardware timers, PWM or PIO handle the time-critical pulse train.

How to measure whether the design is truly non-blocking

Use a logic analyzer and capture:

  • STEP.
  • DIR.
  • A debug GPIO toggled by the main loop.
  • Optional limit-switch or communication signals.

During a long move, verify that STEP timing remains stable while the main-loop debug signal continues changing. Our logic-analyzer workflow explains how to measure digital timing correctly.

Failure tests before calling it finished

  1. Command the longest expected move.
  2. Run communication traffic during motion.
  3. Exercise buttons and sensors while stepping.
  4. Test minimum and maximum step rates.
  5. Reverse direction repeatedly.
  6. Test acceleration with the real mechanical load.
  7. Monitor driver and motor temperature.
  8. Power-cycle during motion and verify safe startup state.
  9. Trigger limit switches at different speeds.
  10. Verify homing/recovery after an interrupted move.

Primary references

Bottom line

Do not make the CPU wait for every step interval. Use a timer for simple scheduled motion, PWM for steady pulse trains, or PIO for deterministic high-rate/multi-axis generation. Keep acceleration and motion state in software while hardware handles pulse timing. That is what turns a stepper demo into an embedded motion subsystem.