As an Amazon Associate we earn from qualifying purchases.
A bipolar stepper motor moves in fixed steps, so you can control its position without feedback sensors. This guide shows how to drive one from a PIC16F877A microcontroller using an L293D H-bridge driver. It covers the wiring, the logic behind the step sequence, a working program and a few practical tips.
The setup is simple. Four pins of PORTB (RB7, RB6, RB5 and RB4) switch the motor coils through the L293D. The PIC only produces logic signals. The driver supplies the current the coils need.
How a bipolar stepper motor works
A bipolar stepper has two coils, usually called coil A and coil B, and four wires. To turn the shaft, you energize the coils in a repeating order. Each change in the pattern moves the rotor one step.
The current in a bipolar motor must flow in both directions through each coil. That is why you need an H-bridge. A single H-bridge can send current through a coil either way, depending on which of its two inputs is high.
You can find the coil pairs by measuring resistance with a multimeter. Two wires that show a low resistance between them belong to the same coil. Wires from different coils show no connection.
What you need
- PIC16F877A microcontroller with a 16 MHz crystal and its usual support parts
- L293D dual H-bridge driver
- Bipolar stepper motor and a suitable motor power supply
- Programmer and a compiler that accepts the code below (HI-TECH C style,
#include <pic.h>) - Breadboard or PCB and jumper wires
Note: If the motor will run for a long time or draw a lot of current, use an H-bridge driver rated above 1 A. The L293D is rated at 600 mA continuous per channel, so a larger motor needs a stronger driver.
Wiring the PIC to the L293D
The program drives RB7, RB6, RB5 and RB4. Connect them to the L293D inputs like this:
- RB7 to IN1 (pin 2) and RB6 to IN2 (pin 7), which control coil A through OUT1 (pin 3) and OUT2 (pin 6)
- RB5 to IN3 (pin 10) and RB4 to IN4 (pin 15), which control coil B through OUT3 (pin 11) and OUT4 (pin 14)
- Enable pins 1 and 9 to +5 V, otherwise the outputs stay off
- Pin 16 (logic supply) to +5 V
- Pin 8 (motor supply) to the motor voltage
- The four ground pins (4, 5, 12 and 13) to ground, shared with the PIC
Connect one coil of the motor across OUT1 and OUT2, and the other coil across OUT3 and OUT4. The L293D has internal clamp diodes, so you do not need to add external ones for a basic setup. Always connect the grounds of the PIC and the motor supply together.
RB6 and RB7 also serve as programming pins (PGC and PGD) on this chip. If your programmer has trouble connecting, disconnect these two wires from the driver while you program the chip.
The step sequence
The program uses a simple full-step sequence, often called wave drive. Only one input is high at a time, so only one coil is energized per step. The four steps are:
- RB7 high: coil A is energized in one direction
- RB5 high: coil B is energized in one direction
- RB6 high: coil A is energized in the opposite direction
- RB4 high: coil B is energized in the opposite direction
After step 4 the sequence starts over from step 1. Running it in reverse order turns the motor the other way.
Program
This is the original program. It sends the four steps once, with a 200 ms pause between them, and then stops in an endless loop.
#include<pic.h>
#define _XTAL_FREQ 16000000
__CONFIG(0X3F7A);
void main()
{
TRISB=0X0;
RB7=1; RB6=0; RB5=0; RB4=0; //1
__delay_ms(200);
PORTB=0X0;
RB7=0; RB6=0; RB5=1; RB4=0;
__delay_ms(200);
PORTB=0X0;
RB7=0; RB6=1; RB5=0; RB4=0;
__delay_ms(200);
PORTB=0X0;
RB7=0; RB6=0; RB5=0; RB4=1;
__delay_ms(200);
PORTB=0X0;
while(1);
}
What the code does
_XTAL_FREQtells the compiler the oscillator runs at 16 MHz, so__delay_ms()waits the correct time.__CONFIG(0X3F7A)sets the configuration bits. This value selects an HS oscillator and turns off the watchdog timer and low-voltage programming. Check it against your own crystal and board.TRISB=0X0makes all PORTB pins outputs.- Each group of four assignments sets one step.
PORTB=0X0switches all coils off before the next step. while(1);stops the program, so the motor only moves four steps.
Making the motor turn continuously
Because the original program ends after four steps, it is only good for testing the wiring. For continuous rotation, put the steps in a table and repeat them in a loop. The version below uses the same pins and the same order:
#include<pic.h>
#define _XTAL_FREQ 16000000
__CONFIG(0X3F7A);
const unsigned char steps[4] = {0x80, 0x20, 0x40, 0x10};
void main()
{
unsigned char i;
TRISB = 0x00;
PORTB = 0x00;
while(1)
{
for(i = 0; i < 4; i++)
{
PORTB = steps[i];
__delay_ms(20);
}
}
}
The values 0x80, 0x20, 0x40 and 0x10 set RB7, RB5, RB6 and RB4 in turn. To reverse the direction, loop through the table from the last entry to the first.
Choosing the delay
The delay between steps sets the speed. A long delay such as 200 ms lets you watch each step, which helps when you check the wiring. A shorter delay makes the motor turn faster.
If the delay is too short, the motor will hum, vibrate or skip steps instead of turning. Reduce the delay gradually until you find a value that runs smoothly with your motor and supply voltage.
Troubleshooting
- Motor only vibrates: One coil is probably swapped with the other. Check which wires form each pair, or increase the step delay.
- Motor does not move: Make sure enable pins 1 and 9 are high and that the motor supply is connected to pin 8.
- Motor turns the wrong way: Reverse the step order, or swap the two wires of one coil.
- Driver gets hot: The motor draws more current than the L293D can handle. Use a stronger driver.
- PIC resets or behaves oddly: Check the shared ground and add a capacitor across the motor supply.
Related guides
If you prefer Arduino, our guide to bipolar stepper motor control with an L293D driver covers the same driver circuit. For the basics of the library approach, see this introduction to Arduino stepper motor control. If your motor lacks torque at higher speeds, read how to increase stepper motor voltage safely. You can also browse more PIC microcontroller projects on this site.
FAQ
Can I use the L293D for any bipolar stepper motor?
No. The L293D suits small motors with low current needs. Check the motor’s rated current per coil, and choose a driver that can supply it comfortably.
Why does the program set PORTB to zero between steps?
It turns off every coil before the next pattern is applied, so two patterns never overlap. The loop version above assigns the whole port in one operation, which gives a clean change without this extra step.
How do I make the motor go faster?
Reduce the delay between steps. Raising the motor supply voltage within the motor and driver limits can also help it keep up at higher speeds.
Can I use a different PIC pin group?
Yes. Any four output pins work, as long as you update the pin names or port values in the code and wire them to the matching L293D inputs.