In this post I will try to explain the key features of interfacing stepper motors with a micro-controller. The micro controller I used was a PIC 16F887 and the bipolar stepper motors were NEMA replicas. It was a fun experience. The route I present was a bit dangerous due to power supply problems and not so cost effective due to deadlines. A complete part list is given in the end of the project. So let's begin!
Basic Steps
1. The tools
2. The electronics
3. Theory
4. Practical Part
5. Final Appearance
1)The tools.
For this project the tools I needed were a micro-controller and a programmer for it. I chose the PIC 16F887 due to its number of I/O ports and functionality, you also can get them for free as samples from the Microchip website! The programmer was the PICkit3 board. I could have gone DIY for the programmer but to avoid soldering problems and save some time, I bought a compatible and tested one.
Another part of the programming is the suite. I downloaded and installed MPLABX for my system which runs Linux. It is the same procedure for windows users , don't worry. It is free for download from the Microchip website. In order to actually program the micro-controller a compiler is needed. I prefer C programming for the device, but you can choose upon Basic or Assembly as alternatives. After I installed the XC8 compiler, which is not pre-installed as default, I was ready to go!
2) The electronics.
The project consists of four parts. The micro-controller, the driving circuit, the stepper motor and the power supply. Just to note, the micro-controller can be also an Arduino or AVR, the theory is the same, the application is specific though. So if you know how to code an Arduino for example this project is also for you.
The driving circuit is a two way situation. Either you make one from individual components or buy an already assembled driver board. My choice was to buy an assembled one due to the cost difference of building one, which was accountable. The driver I chose was an L298 because it was compatible with my motors. This IC can drive also other types such as DC motors. More information can be obtained from its datasheet.
The stepper motors I bought were bipolar. To clarify there are two types of stepper motors unipolar and bipolar. The common way to identify is the number of wires, 6 or 5 for unipolar and 4 for bipolar respectively. Mine were similar to the NEMA17 motors, with ratings 5V 1.3A.
In order to drive the motors a high current power supply has to be chosen. It is important to have a different supply for the motors and the micro-controller to avoid problems. The perfect power supply for me was an old computer PSU that I customized for this project.There are a lot of tutorials about DIY power supplies out there. I will also give tips on this topic later on.
To sum up:
Motors
3x Bipolar steppers 5V @ 1.3 A
3x L298 Motor Drivers.
Target Board
1x PIC 16F887 or other micro-controller.
1x 330 Ohm Resistor.
1x 7805 Voltage stabilizer (12V to 5V).
To sum up:
Motors
3x Bipolar steppers 5V @ 1.3 A
3x L298 Motor Drivers.
Target Board
1x PIC 16F887 or other micro-controller.
1x 330 Ohm Resistor.
1x 7805 Voltage stabilizer (12V to 5V).
2x Capacitors
2x Female 40-pin headers
2x Female 40-pin headers
40 Male Jumper Wires
Naked Wire for soldering (It's not a PCB!)
Controller
6x Switch buttons
7x 10K Resistors
7x Capacitors
Naked Wire for soldering (It's not a PCB!)
Controller
6x Switch buttons
7x 10K Resistors
7x Capacitors
Power Supply
1x Computer PSU (from 200W to 400W is ok!)
1x Computer PSU (from 200W to 400W is ok!)
Programmer
Pickit 2 development kit
Also.
1 pinned board.
1x Solder Iron.
1x Solder (or two depends on your ability)
3) Theory
Stepper Motor
These are motors which are widely used in industrial and home appliances. They operate with DC and AC power and have different sizes. Their basic operation is hidden in their name. Compared to DC motors which operate in rotation continuously, these motors rotate in steps. Every step equals to a certain predefined amount of degrees. Let's say that a motor does 200 steps for one full rotation. So 360/200 is 1.8 degrees per step. They can still work as DC motors depending on the sequence of voltages applied.
Microcontroller
The choice of this microcontroller was due to the many onboard ports for user manipulation. This 8-bit microcontroller is packed in a 40-pin package. The PIC16F887 features 256 bytes of EEPROM data memory, self programming, an ICD, 2 Comparators, 14 channels of 10-bit Analog-to-Digital (A/D) converter, 1 capture/compare/PWM and 1 Enhanced capture/compare/PWM functions, a synchronous serial port that can be configured as either 3-wire Serial Peripheral Interface (SPI) or the 2-wire Inter-Integrated Circuit (I²C) bus and an Enhanced Universal Asynchronous Receiver Transmitter (EUSART). All of these features make it ideal for more advanced level A/D applications. The following picture is from the user manual, which can be found here.
Also.
1 pinned board.
1x Solder Iron.
1x Solder (or two depends on your ability)
3) Theory
Stepper Motor
These are motors which are widely used in industrial and home appliances. They operate with DC and AC power and have different sizes. Their basic operation is hidden in their name. Compared to DC motors which operate in rotation continuously, these motors rotate in steps. Every step equals to a certain predefined amount of degrees. Let's say that a motor does 200 steps for one full rotation. So 360/200 is 1.8 degrees per step. They can still work as DC motors depending on the sequence of voltages applied.
Microcontroller
The choice of this microcontroller was due to the many onboard ports for user manipulation. This 8-bit microcontroller is packed in a 40-pin package. The PIC16F887 features 256 bytes of EEPROM data memory, self programming, an ICD, 2 Comparators, 14 channels of 10-bit Analog-to-Digital (A/D) converter, 1 capture/compare/PWM and 1 Enhanced capture/compare/PWM functions, a synchronous serial port that can be configured as either 3-wire Serial Peripheral Interface (SPI) or the 2-wire Inter-Integrated Circuit (I²C) bus and an Enhanced Universal Asynchronous Receiver Transmitter (EUSART). All of these features make it ideal for more advanced level A/D applications. The following picture is from the user manual, which can be found here.
Motor driver
The L293 consists of 4 half bridges. You can use the L293 in any combination shown, you aren't restricted to those examples. You can create 2 full bridges with them, 4 half bridges or any combination you like. If you need to control 1 direction, you can do so with 1 half bridge.
If you need to control 2 directions, you need a full bridge.If you want to control a stepper motor, you need a full bridge per coil pair.
Most hobby setups with steppers will require 2 full bridges, so one entire L293, which will probably be the reason for the existence of this package (except for the hobby part, very few hardware is produced just for hobbyists).
As you might see from this reply, there's a multitude of motor types available, and they aren't all to be powered the same way.
There are motors available that are made to rotate in a specific direction (most of those also have a gear attached).
But sometimes you just don't have a need for a motor to run in 2 directions.
So then you do not need to use a more complicated way of connecting them.
So you need to know what motor you are going to use before you are designing your electronics.
You need to know the motor anyway, because you also need to know the voltage, but more importantly the (stall) current it will take.
You might find out this driver isn't the best choice for your motor type, or maybe it is.
The next table does not supply the manual! You should read the L298 manual to know exactly what is needed in terms of voltage and current. This is a simple table for reference.
The following table shows the interconnection from ports to stepper
motor wires. Moreover the sequences needed for driving the motors
clockwise and counter clockwise. Your port connections can be different
as always, it is your choice. This is a simple guide for the connections
to remember. To elaborate the outputs of PORTB connect to the L298
driver inputs. And the outputs of the driver to the stepper motor windings accordingly.
Note: The sequences of pulses are not for all motors , took me a while to figure out my motor sequence because, they were NEMA replicas which are not exactly the same!
To detect the wiring type of your steppers you can use your multimeter to identify the correct windings of coil. Put settings to resistance scale, depends on the resistance but the less reading means the same winding and vice versa. You can also check the manual for reference, always the safest approach.
4)Practical Part
PSU Hacking

Blue is -12v
White is -5v
Grey is Power_OK (useful for on indicator LED to ground)
Green is Power_On (short to ground to power on)
Purple is +5v Stand-By (useful for memory circuit; or plugged in LED to ground)
Brown is +3.3v sense (must be connected to orange)
Pink is +5v sense (if present; must be connected to red)
The first step of this process is to open the metalic case. Remove the screws to see the wiring.
Programming
Coding is an essential part of the project. The following code is very simple in its functionality in terms of operation and was developed for demonstration purposes. It can be reduced to individual sections with a careful look. The standard parts are:
Control pad
The control pad consists of six push buttons and resistors and capacitors. Each button is connected to a pull up resistor. the capacitor serves as power conditioner for each button. They are arranged in pairs the upper buttons move the motors clockwise and the downside others the counter clockwise rotation.
Interconnections
5) Final appearance

Blue is -12v
White is -5v
Grey is Power_OK (useful for on indicator LED to ground)
Green is Power_On (short to ground to power on)
Purple is +5v Stand-By (useful for memory circuit; or plugged in LED to ground)
Brown is +3.3v sense (must be connected to orange)
Pink is +5v sense (if present; must be connected to red)
The first step of this process is to open the metalic case. Remove the screws to see the wiring.
Programming
Coding is an essential part of the project. The following code is very simple in its functionality in terms of operation and was developed for demonstration purposes. It can be reduced to individual sections with a careful look. The standard parts are:
- Configuration part
- M1,M2,M3 Forward/Reverse control
- Button Control Part
#include<xc.h> #define _XTAL_FREQ 4000000 // BEGIN CONFIG #pragma config FOSC = XT // Oscillator Selection bits (HS oscillator) #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled) #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled) #pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled) #pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming) #pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off) #pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control) #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off) //END CONFIG void M1Forward(int step){ int i; for(i=0; i<step; i++){ PORTB =0b00001000; __delay_ms(30); PORTB =0b00000010; __delay_ms(30); PORTB =0b00000100; __delay_ms(30); PORTB =0b00000001; __delay_ms(30); } // PORTB =0b00000000; } void M1Reverse(int step){ int i; for(i=0; i<step; i++){ PORTB =0b00000001; __delay_ms(40); PORTB =0b00000100; __delay_ms(40); PORTB =0b00000010; __delay_ms(40); PORTB =0b00001000; __delay_ms(40); } // PORTB =0b00000000; } void M2Forward(int step){ int i; for(i=0; i<step; i++){ PORTB =0b10000000; __delay_ms(30); PORTB =0b00100000; __delay_ms(30); PORTB =0b01000000; __delay_ms(30); PORTB =0b00010000; __delay_ms(30); } // PORTB =0b00000000; } void M2Reverse(int step){ int i; for(i=0; i<step; i++){ PORTB =0b00010000; __delay_ms(40); PORTB =0b01000000; __delay_ms(40); PORTB =0b00100000; __delay_ms(40); PORTB =0b10000000; __delay_ms(40); } // PORTB =0b00000000; } void M3Forward(int step){ int i; for(i=0; i<step; i++){ PORTC =0b00001000; __delay_ms(30); PORTC =0b00000010; __delay_ms(30); PORTC =0b00000100; __delay_ms(30); PORTC =0b00000001 ; __delay_ms(30); } // PORTC =0b00000000; } void M3Reverse(int step){ int i; for(i=0; i<step; i++){ PORTC =0b00000001; __delay_ms(40); PORTC =0b00000100; __delay_ms(40); PORTC =0b00000010; __delay_ms(40); PORTC =0b00001000; __delay_ms(40); } // PORTC =0b00000000; } void main() { // PORT B as output port TRISB = 0; PORTB = 0b00000000; //PORTD as input TRISD=1; RD0=1; RD1=1; RD2=1; RD3=1; RD4=1; RD5=1; TRISC=0; PORTC=0; //complete cycle is 200/4=50 reps /*int StepPerRev=200; int cycle=StepPerRev/4; //360 degrees int half=cycle/2; //180 degrees int quarter=cycle/4; //90 degrees int counter=0; */ while(1){ //Motor 1 Forward and reverse buttons if (RD0 == 0){ //If the switch is pressed __delay_ms(50); //Switch if (RD0 == 0)//If the switch is still pressed { M1Forward(1); }; }; if (RD1 == 0) //If the switch is pressed { __delay_ms(50); //Switch if (RD1 == 0)//If the switch is still pressed { M1Reverse(1); }; }; //Motor 2 Forward and reverse buttons if (RD2 == 0){ //If the switch is pressed __delay_ms(50); //Switch if (RD2 == 0)//If the switch is still pressed { M2Forward(1); }; }; if (RD3 == 0) //If the switch is pressed { __delay_ms(50); //Switch if (RD3 == 0)//If the switch is still pressed { M2Reverse(1); }; }; //Motor 3 Forward and reverse buttons if (RD4 == 0){ //If the switch is pressed __delay_ms(50); //Switch if (RD4 == 0)//If the switch is still pressed { M3Forward(1); }; }; if (RD5 == 0) //If the switch is pressed { __delay_ms(50); //Switch if (RD5 == 0)//If the switch is still pressed { M3Reverse(1); }; }; } }
Control pad
The control pad consists of six push buttons and resistors and capacitors. Each button is connected to a pull up resistor. the capacitor serves as power conditioner for each button. They are arranged in pairs the upper buttons move the motors clockwise and the downside others the counter clockwise rotation.
Interconnections
5) Final appearance







Comments