AutomateDirectUSAMotion LibraryIndustrial monitors & PCs

Troubleshooting

Why a stepper motor will not turn from microcontroller pins alone

Servo and stepper motion Published 9 min read By AutomateDirectUSA engineering team

Short answer

A microcontroller pin sources about 25 mA and a full port about 100 mA, while a stepper phase draws 100 mA to 2 A, so directly wired pins cannot energize the windings — the motor stays still until a ULN2003, L293D or A4988 driver stage carries the phase current. Three firmware faults pile on: loops initialize with == instead of =, delay counters overwrite the step count, and the buttons are read from PORTB while TRISD configures them on PORTD.

On this page
  1. Why will a stepper not turn when it is wired straight to microcontroller pins?
  2. Which firmware bug stops the step loop before it starts?
  3. Why do the push buttons on RD0 and RD1 do nothing?
  4. Why does the step sequence never finish even after the loops initialize?
  5. How much current does a stepper phase need next to what a pin can give?
  6. What driver stage carries the phase current a pin cannot?
  7. What does the corrected program look like?
  8. How do you confirm the fix before connecting the motor?
  9. Questions asked

A stepper wired straight to microcontroller output pins will not rotate, and in this program the buttons are dead too, because the faults stack: the pins cannot supply the winding current, and three firmware bugs stop the step pattern before it reaches the port correctly. A PIC16-class output pin is rated about 25 mA and a whole port about 100 mA, while a stepper phase needs 100 mA to 2 A — no bit pattern fixes a shortfall that size. The firmware bugs are independent and each one alone is enough: comparison operators where assignments belong in the loop initializers, buttons polled on PORTB while TRISD configures them on PORTD, and one counter shared by the step loop and every delay loop. Work through them in the order below, cheapest check first, then add the driver stage the hardware was always going to need.

Why will a stepper not turn when it is wired straight to microcontroller pins? #

Pins cannot do it: a PIC16-class datasheet rates a standard output pin at up to 25 mA and a full port at roughly 100 mA, while a stepper phase needs 100 mA to 2 A, so the windings never build enough field to move the rotor.

The port is doing what it is told. In the simulator the four port bits flip through the step pattern and the pins show active while the motor side stays dead — that asymmetry is the signature of a current shortfall, not a logic fault. A winding is a low-resistance coil, not a logic load: at standstill its current is the supply voltage divided by the winding resistance, so as an example a 5 V supply across a 20 ohm winding asks for 250 mA from a pin rated at roughly a tenth of that. The pin clamps or sags, the coil field collapses to a fraction of what the rotor needs, and the shaft holds still.

The confirming check: disconnect the motor, meter one winding's resistance, compute supply voltage divided by that resistance, and set the result against the pin rating in the datasheet of your exact part number. When the first number dwarfs the second, the missing piece is a driver stage, not a different bit pattern.

Which firmware bug stops the step loop before it starts? #

Every loop initializer in this program uses the comparison operator instead of assignment: for(i==0;i<50;i++) compares i with 0 and never writes 0 into it, so the step counter starts at whatever value memory holds and the 50-step sequence exits immediately or runs a garbage count.

The first slot of a for statement is an expression slot, and in C an equality test is a legal expression: it evaluates, produces a result, and the result is discarded, so the compiler takes the line without complaint. Nothing in the function ever writes 0 into i, so a local variable starts at an indeterminate value and the loop bound is tested against garbage. The fix is to drop the second equals sign in every initializer: for(i=0;i<50;i++) for the outer loop, and the same change in all four inner delay loops, which repeat the mistake with the speed bound.

The confirming check: compile with warnings enabled and look for the unused-comparison-result and used-before-assignment class of diagnostics, then watch i in the simulator while the function runs — before the fix it starts at a nonsense value; after it, it climbs 0 through 49.

Why do the push buttons on RD0 and RD1 do nothing? #

The buttons are wired to RD0 and RD1 and configured as inputs by TRISD = 0b00000011, but the program polls PORTB.F0 and PORTB.F1, so it reads two floating pins on the wrong port and turn() is never called.

The hardware side is already right: TRISD = 0b00000011 makes RD0 and RD1 inputs, which is what buttons wired to those pins need. The reads are what fail. if(PORTB.F0) and if(PORTB.F1) sample RB0 and RB1, which TRISB = 0b00001111 leaves as inputs but which connect to nothing, so the program tests two floating pins and neither button press ever reaches turn(). Read the port the buttons are actually wired to: if(PORTD.F0) for the first button and if(PORTD.F1) for the second.

The confirming check: in the simulator, press each on-screen button and watch the PORTD bit change state while the program takes its branch; before the fix the read never moves, because it samples a port no button touches. If the buttons switch to ground, the pins also need the pull-ups your board provides — an input left floating reads noise on either port.

Why does the step sequence never finish even after the loops initialize? #

The same counter i drives both the 50-step outer loop and every 1 ms delay loop, so each inner loop overwrites the step count and the sequence never completes — give the delay loops their own variable j and leave i to count steps.

Fixing the initializer is not enough on its own, because turn() declares one counter, i, and gives it two jobs. The outer loop counts the 50 steps with it; each inner delay loop also runs on i, rewriting it 0, 1, 2 and upward to the speed value. When the inner loop hands control back, the outer counter no longer holds the step number — the sequence restarts, stops short or runs wild, and the shaft never accumulates its 50 steps. Give the delays their own counter: declare int i, j;, step with i, delay with j.

The confirming check: after both loop fixes, one button press drives the outer counter cleanly 0 through 49 and returns; before the counter fix, the count resets a few steps into the sequence even though the initializer is correct.

How much current does a stepper phase need next to what a pin can give? #

Worked example: a small unipolar stepper rated 500 mA per phase needs the current of 20 output pins for one winding — 500 mA / 25 mA = 20 — and with two phases energized the shortfall against a four-pin port is 1,000 mA against 100 mA.

Take the example inputs: a small unipolar stepper whose datasheet lists 500 mA per phase (a common rating for hobby-size motors — read yours from your motor's datasheet), a PIC16-class pin rated 25 mA, and a full-step pattern that holds two phases on at once. The arithmetic:

  • One phase against one pin: 500 mA / 25 mA = 20 — a single winding wants the output of 20 pins.
  • Two phases together: 2 x 500 mA = 1,000 mA of winding current while the pattern holds two coils on.
  • What the four motor pins can source: 4 x 25 mA = 100 mA, which is also the whole-port figure in the PIC16 datasheet.
  • The shortfall: 1,000 mA / 100 mA = 10 — the pins deliver a tenth of the current the two energized windings ask for.

A winding's pull-in force scales with ampere-turns, so at a tenth of the current the rotor sits still or hums without taking a step. This is a calculated example for those inputs, not a measured result; a motor listed at 1 A per phase moves the shortfall to 40 pin-equivalents per winding.

What driver stage carries the phase current a pin cannot? #

Put a driver between the pins and the windings: a ULN2003 Darlington array for low-current unipolar motors, an L293D dual H-bridge for small bipolar motors, or an A4988- or DRV8825-class chopper driver when you want the winding current set and held with microstepping.

Each stage takes the port pattern at logic levels and switches the winding current from the motor supply:

  • ULN2003: seven Darlington channels in one package. The port bits drive the inputs, the channels sink the coil current for a unipolar motor's common leads, and the built-in clamp diodes catch the windings' turn-off spike. Check the per-channel rating on its datasheet against your phase current.
  • L293D: dual H-bridge with integrated flyback diodes for a small bipolar motor — the four port bits drive the two input pairs and the enable pins are tied active.
  • A4988- or DRV8825-class chopper drivers: the firmware shrinks to step and direction pulses, the driver generates the phase sequence itself, and a reference setting caps the winding current — the stage that also buys microstepping.

Match the driver's continuous current rating to the motor's rated phase current: a driver undersized for the motor overheats or current-limits into a stall, and that fault has its own diagnostic path in When a Stepper Driver Is Rated Below the Motor Current. For a wiring example that puts the chopper stage and the bridge stage on a microcontroller, a stepper control guide covering the A4988 and L293D driver stages walks the two circuits end to end.

One safety note: wire the motor supply to the driver only, and power the bench down before changing any connection between microcontroller, driver and motor — a motor-supply wire into a port pin destroys the port on the next power-up.

What does the corrected program look like? #

The corrected program keeps the original structure and changes three things: every initializer assigns (for(i=0;i<50;i++), never for(i==0;i<50;i++)), the delay loops count on their own variable j, and the buttons are read from PORTD while TRISB = 0b00001111 keeps RB4 through RB7 as outputs into the driver inputs.

void turn(int direction, int speed)
{
    int i, j;                      /* i counts steps, j counts delay ticks */

    if (direction == 0)            /* forward */
    {
        for (i = 0; i < 50; i++)
        {
            PORTB = 0b10010000;    /* RB7 and RB4 on */
            for (j = 0; j < speed; j++) delay_ms(1);
            PORTB = 0b11000000;    /* RB7 and RB6 on */
            for (j = 0; j < speed; j++) delay_ms(1);
            PORTB = 0b01100000;    /* RB6 and RB5 on */
            for (j = 0; j < speed; j++) delay_ms(1);
            PORTB = 0b00110000;    /* RB5 and RB4 on */
            for (j = 0; j < speed; j++) delay_ms(1);
        }
    }
    else if (direction == 1)       /* reverse: the same four patterns, run backward */
    {
        for (i = 0; i < 50; i++)
        {
            PORTB = 0b00110000;
            for (j = 0; j < speed; j++) delay_ms(1);
            PORTB = 0b01100000;
            for (j = 0; j < speed; j++) delay_ms(1);
            PORTB = 0b11000000;
            for (j = 0; j < speed; j++) delay_ms(1);
            PORTB = 0b10010000;
            for (j = 0; j < speed; j++) delay_ms(1);
        }
    }
}

void main()
{
    TRISB = 0b00001111;            /* RB4-RB7 outputs into the driver inputs */
    TRISD = 0b00000011;            /* RD0-RD1 inputs from the push buttons */
    PORTB = 0x00;

    while (1)
    {
        if (PORTD.F0)              /* first button, on RD0, reads from PORTD */
        {
            turn(1, 10);
        }
        if (PORTD.F1)              /* second button, on RD1, reads from PORTD */
        {
            turn(0, 20);
        }
        delay_ms(1000);
    }
}

The register and port names — TRISB, TRISD, PORTB, PORTD and the millisecond delay call — are the names the compiler provides for this part family; i and j are this example's own counters, declared with their type. The four step patterns on the upper nibble are the original two-coil-on sequence, run forward in one direction case and reversed in the other.

How do you confirm the fix before connecting the motor? #

Prove the logic in simulation and the current on the bench before the motor goes anywhere near the pins: the sequence must cycle on LEDs or the simulator's port view, and the driver output must show the phase current with a meter before the windings connect.

Work down the list in order; each step is cheaper than the one after it:

  1. Compile with warnings enabled and clear every initializer diagnostic: each for(i==0;i<50;i++) becomes for(i=0;i<50;i++), in the outer loop and in all four delay loops.
  2. In the simulator, press each button and confirm the PORTD bit toggles and the program takes the branch; before the port fix, the read sat on PORTB and never moved.
  3. Run one button press with LEDs through resistors on the four driver inputs: the pattern must step 0b10010000, 0b11000000, 0b01100000, 0b00110000 in order, fifty cycles, and reverse while the second button is held.
  4. Power the driver from its motor supply and measure one coil's current at standstill; it should sit near the motor's rated phase current from its datasheet, not near a logic-pin figure.
  5. Only then connect the motor, press both buttons, and confirm the shaft takes fifty full steps per press and reverses on the other button.

When the meter shows the phase current and the shaft counts out its steps on both buttons, the causes on this page are exhausted; missed steps at speed, driver heating and noise on long lines belong to driver sizing and tuning, not to the pins.

Questions asked

Does a circuit simulator show the missing current, or only the logic?

Only the logic. A simulator drives its on-screen parts from pin states, so a port bit shows active whether the real pin could carry a few milliamps or a full winding load. The pin-current limit is a silicon rating the simulator does not model, which is why a sequence can look perfect in simulation and still leave the real shaft motionless. Prove the port pattern in the simulator and prove the current on the bench with a driver stage and a meter.

Is any stepper small enough to wire straight to output pins?

Only one whose datasheet phase current sits below the pin rating, and that rules out nearly every motor sold as a stepper. A winding is a low-resistance coil: its standstill current is supply voltage divided by winding resistance, and hobby motors land far above what a logic output may source. Treat a driver stage between the pins and the windings as mandatory, and take both the pin limit and the phase current from the datasheets of the exact parts you hold.

Why does the full-step pattern set two port bits at a time?

That is two-coil-on full-step excitation. Patterns 1001, 1100, 0110 and 0011 on the upper nibble energize two adjacent windings at once, which holds the rotor between pole positions and gives more torque than energizing a single winding. Running the four patterns in reverse order reverses the shaft, which is exactly how the two direction cases in the program differ.

Why does the comparison-in-initializer bug compile without an error?

Because the first slot of a for statement accepts any expression and a comparison is a valid one: the compiler evaluates it, discards the result and moves on, so nothing about the line is illegal. Some compilers flag the pattern with a warning about a comparison whose result is unused, or a variable read before it is assigned, which is why the first check on the bench is a clean compile with warnings enabled.

Keep reading

  • Troubleshooting

    Why a Closed Loop Stepper Will Not Move Above 200 kHz

    The axis sits dead because the kernel exceeds the drive's documented step input limit, making each high or low state narrower than the input can latch. Set the kernel to half the ceiling, derive steps per mm from the drive's command resolution divided by the screw's travel per revolution, and verify the encoder A-channel polarity at the drive terminals.

    Servo and stepper motion

  • Explainer

    Why a Stepper Motor Runs Hot: Winding Connection and Phase Current

    Copper loss sets stepper heat: current squared times winding resistance, summed over the energized sections. The driver's current setpoint and the winding connection decide it; the supply voltage mostly sets torque at speed. Wire the motor half-coil, bipolar parallel or bipolar series for the torque-speed curve the axis needs, set current to that connection's rating with the driver's documented setting equation, drop the current when the axis idles, and judge the result by stabilized case temperature and position retention.

    Servo and stepper motion

  • Troubleshooting

    Why an EtherCAT CSP Stepper Axis Enables but Does Not Move

    Holding torque on Servo On proves only that the power stage energizes the windings. In cyclic synchronous position mode the axis moves only when the drive accepts each new 0x607A target and no internal limit refuses it, so check Statusword bit 11 first, then the drive's speed, current and software position limits, then the position unit scaling between controller and drive.

    Servo and stepper motion