Troubleshooting
Why a stepper motor will not change direction while running
Short answer
A stepper that only reverses after a stop and restart has a command-handling fault, not a motor fault: the running direction routine loops forever and never returns, so the program never reads the direction switch again. Move motion into one repeating loop with bounded calls that return each pass, keep excitation through the change, and reverse by decelerating through zero speed, sizing the transition with T = J × α.

On this page
- Why does my stepper only reverse after I stop and restart the program?
- How does a looping direction routine block the direction switch?
- Why didn't adding a de-energize step before the reverse routine fix it?
- How do I prove the direction request never gets read again?
- How should the motion loop be structured so direction changes take effect?
- What torque does a mid-motion reversal need?
- Should a stepper ever reverse at full speed?
- Questions asked
If your stepper runs clockwise or counterclockwise without complaint but only reverses after you stop the program and start it again, suspect the program first: the running direction routine never hands control back, so the loop never reads the direction switch again. The motor, the driver and the wiring are usually fine. The fix has two halves. Restructure the control code so one repeating loop reads the request on every pass and every motion call does a finite piece of work and returns. Then treat the mechanical side with respect: decelerate through zero speed before reversing, and size that transition from the load inertia with T = J × α. The sections below confirm the diagnosis, give the restructure step by step, and work a reversal-torque example with real numbers.
Why does my stepper only reverse after I stop and restart the program? #
Because the routine that moves the motor never finishes, the program never reaches its next read of the direction switch, so your new direction request sits unread until you stop execution and start over; the motor and driver are healthy.
The usual architecture is a case structure that selects one of two direction routines, one per rotation sense. Both directions work when the sense is chosen before the run starts, and stopping the program forces a fresh selection at the next start, which is exactly why the stop-toggle-restart workaround appears to work. The deciding software quantity is the interval between successive reads of the direction switch: when the selected routine never returns, that interval is infinite, and no switch position can ever arrive. Because the fault is in command handling, diagnose it before touching current, tuning or wiring.
How does a looping direction routine block the direction switch? #
A case structure tests its selector once, when execution reaches it, and never re-tests while the chosen branch runs; a subroutine call returns only when its own code finishes, so an internal loop with no exit condition traps the caller indefinitely.
The switch can change state on the panel all it likes; nothing downstream is executing a read of it. Code written after the motion call, including the next pass that would read the switch, stays queued behind a call that never ends. Any parallel execution path keeps running, which makes the interface look alive and hides the block. The trap is easy to miss because the selection worked at start-up: the case evaluated once, correctly, and then the chosen routine claimed the program's execution path for good.
Why didn't adding a de-energize step before the reverse routine fix it? #
Because the de-energize step runs once on the way into the branch, and then the endlessly looping motion routine takes over again; reordering steps inside a branch changes the entry sequence, never whether the branch can be interrupted mid-run.
A two-step sequence, release the phases then run in the new sense, executes the release a single time and then parks inside the motion call, exactly as before. The symptom is unchanged because the unread-switch condition is unchanged. The added step does change one physical thing: de-energizing removes the motor's holding torque. A de-energized stepper cannot hold a vertical or gravity-loaded axis; keep the phases energized through the transition, or fit an independent restraint such as a brake, because software is not a holding device.
How do I prove the direction request never gets read again? #
Three checks settle it in minutes, cheapest first: log the switch read on every loop pass, watch whether execution ever comes back from the motion call, and watch the driver's direction input while you flip the switch.
- Log the read. Record the switch position on every iteration of the main loop while the motor runs. If the log freezes the moment motion starts, the loop is parked inside the motion call and the fault is confirmed.
- Trace execution past the call. Use the development environment's execution highlighting, or set a flag immediately after the motion call returns. A flag that never sets means the call owns an operation with no exit condition.
- Watch the direction line at the driver. Toggle the switch during motion and observe the driver's direction input with a scope or the controller's own output monitor. No transition while running proves the changed request never leaves the controller, and it clears wiring and driver settings in the same test.
If the log updates and the direction line still never flips, the output-generation routine itself is the blocker, and the same restructure below fixes that variant too.
How should the motion loop be structured so direction changes take effect? #
One repeating supervisory loop owns the motor: it reads the direction request on every pass, calls motion routines that each finish a finite piece of work and return, and treats the switch position as a request that only takes effect at zero speed.
- Find the endless loop. Open both direction routines and locate each motion loop and its termination condition; the one with no exit, or an exit reachable only from inside, is the blocker.
- Bound every motion call. Refactor each routine to advance a fixed number of steps at a commanded rate and then return, or move pulse generation to a separate motion task that receives commands over an explicit channel. The supervisory loop must never wait out an entire indefinite move; producing the step stream that way matches driving step and direction inputs from a controller velocity output.
- Read the request every pass. Put the switch read on the path that provably executes each iteration, and log it so a frozen loop is visible the next time.
- Separate requested direction from active direction. Record a changed request immediately, but hold the active direction until the decelerate, reverse, accelerate sequence owns the change; the switch should never command the excitation order directly.
- Make stopping and shutdown their own cases. A normal stop keeps the phases energized so holding torque stays; release the phases only in a defined shutdown state, with the restraint from the de-energize note above in place.
Treating direction as an arbitrated state rather than as a program is the same discipline reversing an axis after a belt drive retrofit in Mach3 demands: one owner decides what the direction line means and when it changes.
What torque does a mid-motion reversal need? #
Plan the change as decelerate, cross zero, reverse, accelerate, and size it with T = J × α; for the example values below, 0.0004 kg·m² of total inertia brought from 300 rpm to zero in 0.10 s, that costs 0.13 N·m of inertial torque plus the load torque.
Worked example, calculated for illustrative inputs, so substitute your own datasheet values:
- Rotor inertia 0.0001 kg·m², an illustrative value; use the figure in your motor's datasheet.
- Load inertia reflected to the motor shaft 0.0003 kg·m², meaning the load inertia divided by the square of the gear or belt ratio.
- Total J = 0.0004 kg·m². Running speed 300 rpm = 300 × 2π / 60 = 31.42 rad/s.
- Planned stop time 0.10 s, so α = 31.42 / 0.10 = 314 rad/s².
- Inertial torque T = J × α = 0.0004 kg·m² × 314 rad/s² = 0.126 N·m, about 0.13 N·m.
- Load and friction torque 0.05 N·m in this example, opposing the acceleration in the new direction, so the ramp must deliver 0.13 + 0.05 = 0.18 N·m at the ramp rate.
- Check 0.18 N·m against the available torque at that stepping rate on the motor's torque–speed curve in its datasheet. If the curve gives less, lengthen the ramp: doubling the stop time to 0.20 s halves α to 157 rad/s² and drops the total demand to 0.11 N·m.
| Reversal profile for the example load | α (rad/s²) | Inertial torque | Total demand | Decision |
|---|---|---|---|---|
| Ramp to zero in 0.10 s, then accelerate the same way | 314 | 0.13 N·m | 0.18 N·m | Works if the torque–speed curve shows at least 0.18 N·m at the ramp rate |
| Ramp to zero in 0.20 s | 157 | 0.06 N·m | 0.11 N·m | Comfortable margin, slower axis |
| Reverse commanded on the spot over 0.02 s | 3142 | 1.26 N·m | 1.31 N·m | Roughly seven times the gentle ramp; steps are lost unless the curve shows that torque at speed |
Every figure in the table is a calculated example for the stated inputs, not a measured result, and the driver needs its share of care: ramp the step rate down, set the direction line, then ramp up in the new sense, keeping the direction level stable around the step edges by at least the setup and hold times in that driver's datasheet. The same torque, inertia and ramp discipline is what sizing a stepper motor for a flywheel load takes.
Should a stepper ever reverse at full speed? #
Only when the motor's available torque at that stepping rate covers the whole swing, 1.26 N·m versus 0.18 N·m in the example above, so for this load, decelerating through zero is the decision, not an option.
The decision for each case:
- Direction only takes effect after a stop and restart. Restructure to the single supervisory loop with bounded calls; the symptom disappears without touching hardware.
- The switch is read, but the axis stalls or lands short on reversal. The reversal torque exceeds what the motor delivers at that rate; lengthen the ramp until J × α plus the load torque fits the torque–speed curve, and re-home the axis after any suspected lost-step event.
- The process genuinely needs on-the-fly reversal. Verify the curve covers the swing torque at the operating rate with margin; where it does not, the ramp through zero is not optional, whatever the software looks like.
Questions asked
Does the direction input of a step-and-direction driver take effect while the motor is running?
Yes, at the driver level. The driver latches the direction level against its setup and hold window around each step edge, and rotation follows the next step pulses in the new sense. The lockout in this fault happens upstream, in the controller program that never reads the switch again. Take the driver's setup and hold figures from its datasheet; they are short but never zero.
How do I choose the deceleration ramp for my own load?
Start from T = J × α. Take the rotor inertia from your motor datasheet, add the load inertia reflected through the gear or belt ratio squared, pick a stop time, and require the available torque at the ramp rate to exceed J × α plus the load torque. Halving the stop time doubles the angular acceleration and doubles the inertial torque, so adjust the stop time until the margin is comfortable.
Why did my axis end up in the wrong place after an abrupt reverse?
The rotor stalled or lost steps during the transition. When the demanded inertial torque plus load torque exceeds the torque available at that speed, the rotor slips behind the commanded step sequence, so the step count no longer matches the physical angle. Re-home the axis after any suspected lost-step event; an open-loop stepper cannot detect the slip on its own.
Would a closed-loop stepper have prevented this?
Not the software part. A closed-loop stepper measures rotor position with an encoder and can correct or alarm on lost steps, which helps with abrupt reversals, but it cannot read a direction switch that a looping routine never samples. The command-handling restructure is still required; the closed loop only protects the mechanical side of the change.