16F Unipolar Stepper Motor Example

 

    Also see ULN2003 - a guide to how to use a ULN2003 in other applications.

    The ULN2003 is a very useful and powerful IC that helps interface the PIC micro with high current devices such as motors, and relays. Although the PIC can supply a load with up to 25mA, this is clearly not enough for many applications. As the ULN2003 has almost everything built in, the only additional thing you may require is a diode or 2 for applications with inductive loads (to negate back EMF).

    The max current output for the ULN2003 is 600mA peak, and 500mA continuous (per channel), and only requires a logic level to drive it. For AUD$0.47 per chip, you cant really beat it.

    Lets take a moment to talk about Unipolar Steppers. They are much simpler to interface with than bipolar steppers (as the circuits do not need to change polarity), and more popular overall. Unipolar stepper motors are recognized by their center-tapped windings. The number of phases is twice the number of coils, since each coil is divided in two. So the diagram below, which has two center-tapped coils, represents the connection of a 4-phase unipolar stepper motor and the step sequence to turn it clockwise.

    The steps in which the Unipolar take can be controlled in 2 formats, Full and Half. Although half steps provide a more precise range of movement, it comes at a cost - torque. The following diagram shows CW rotation with both methods

Graphical examples (Left is full step, right is half step):

Unipolar Stepper Operation Stepper Phases animation half step

    The following diagram shows how a ULN2003 is interfaced with a PIC and a Unipolar Stepper motor. Being an inductive load, back EMF protection diodes should be placed in the driver circuit. Note, Pin 8 on the ULN2003 is connected to ground.

Upper circuit is full step, lower is half step

Note the PIC's power supply/oscillator are not shown

Click here too see this circuit in action!

    The program for a full step Unipolar controller would be something like the following (Note, the delay between steps would depend on your steppers maximum step rate and a certain speed)

Device = 16F876
XTAL = 4

ALL_DIGITAL = TRUE

TRISB = %00000000        ' Make PortB all outputs
PORTB = %00000000        ' Set them all low

Main:

PortB = %00001001        ' Start first step 

Delayms 80               ' Delay

PortB = %00001100        ' Start second step

Delayms 80               ' Delay

PortB = %00000110        ' Start third step

Delayms 80               ' Delay

PortB = %00000011        ' Start forth step

Delayms 80               ' Delay

Goto Main                ' Loop for ever

 

    But what about half step? its almost just as easy, the only difference is that there are twice as many steps to get the same range of movement. There is algorithm out there for the sequence, but to keep it easy, I'll use the following example,

Device = 16F876
XTAL = 4

ALL_DIGITAL = TRUE

TRISB = %00000000      ' Make PortB all outputs
PORTB = %00000000      ' Set them all low

Main:

PortB = %00001000      ' Start first step 

Delayms 80             ' Delay

PortB = %00001100      ' Start second step

Delayms 80             ' Delay

PortB = %00000100      ' Start third step

Delayms 80             ' Delay

PortB = %00000110      ' Start forth step

Delayms 80             ' Delay

PortB = %00000010      ' Start fifth step 

Delayms 80             ' Delay

PortB = %00000011      ' Start sixth step

Delayms 80             ' Delay

PortB = %00000001      ' Start seventh step

Delayms 80             ' Delay

PortB = %00001001      ' Start eighth step

Delayms 80             ' Delay

Goto Main              ' Loop for ever

 

Skip Navigation Links.

Collapse Site Tutorial IndexSite Tutorial Index
Collapse 16F PIC Examples16F PIC Examples
LED's
Switches
Keypads
LCD's
7 Seg Displays
ADC
DAC
Collapse MotorsMotors
DC Motors
Servo's
Bipolar Stepper
Unipolar Stepper
Expand ThermometersThermometers
DS1307 RTC Chip
DS275
EEPROM's
RF Modules
RGB LED's
Code Snippets
Expand Code SnippetsCode Snippets
Expand 18F PIC Examples18F PIC Examples
Expand Handy TipsHandy Tips