Tuesday, May 24, 2016

Interfacing unipolar stepper motor with PIC18F4550 microcontroller


Unipolar stepper motor drive with PIC18F4550 microcontroller 
Unipolar 5V stepper motor 
After interfacing PIC18F4550 microcontroller with bipolar stepper motor in the following topic:
CD-ROM Bipolar stepper motor drive using PIC18F4550 and CCS PIC C
Now let's see how to drive a unipolar stepper motor using PIC18F4550 and CCS PIC C compiler.
Usually the bipolar stepper motor has 2 colis and therefore it has 4 wires, and the unipolar has 4 coils which are connected as shown in the following figure:
Unipolar stepper motor coils PIC18F4550
The unipolar stepper motor can be controlled in full step mode or half step mode, the usual method is the full step driving mode which gives higher torque. The following table shows driving sequences:
Unipolar stepper motor driving sequence PIC18F4550
Interfacing PIC18F4550 with unipolar stepper motor circuit:
In the circuit there are 2 pushbuttons which are connected to RB0 and RB1 pins, they are used to choose motor rotation direction. PortB internal pull-ups are enabled in the software.
To control the stepper motor speed a potentiometer (10K) is used and it is connected to analog channel 0(AN0).
ULN2003 (or ULN2004) chip is used to energize the stepper motor coils.
The ULN2003(ULN2004) is a Darlington transistor array which contains seven open collector Darlington pairs with common emitters. For stepper motor controller we need 4 transistors form this chip which means 4 inputs and 4 outputs are needed.

Interfacing unipolar stepper motor with PIC18F4550 ULN2003 circuit CCS PIC C
Instead of the ULN2003 chip, another chip can be used which is L293D dual H-bridge circuits as shown in the circuit schematic below.
For the L293D chip VS voltage always +5V and VSS voltage is the same as the motor voltage for example if the motor voltage is 12V, VSS should be connected to +12V power supply.

Interfacing unipolar stepper motor with PIC18F4550 L293D circuit CCS PIC C
PIC18F4550 internal oscillator is used (8MHz) and MCLR pin function is disabled.
Interfacing PIC18F4550 with unipolar stepper motor CCS C code:
A pot connected to AN0 is used to control the speed of the stepper motor. The microcontroller reads the analog data from AN0 and uses the digital value to change the delay between motor driving sequences.

// Interfacing PIC18F4550 with unipolar stepper motor CCS C code
// http://ccspicc.blogspot.com/
// electronnote@gmail.com

#include <18F4550.h>
#fuses NOMCLR INTRC_IO
#use delay(clock = 8000000)
#use fast_io(B)
#use fast_io(D)

unsigned int8 speed_;
void main()
{
   setup_oscillator(OSC_8MHZ);              // Set internal oscillator to 8MHz
   setup_adc(ADC_CLOCK_DIV_8);              // Set ADC conversion time to 8Tosc
   setup_adc_ports(AN0);                    // Configure RA0(AN0)as analog
   set_adc_channel(0);                      // Select channel 0 input
   port_b_pullups(TRUE);
   output_b(0);
   set_tris_b(3);
   output_d(0);
   set_tris_d(0);
   delay_ms(100);
   while(TRUE)
   {
      output_d(0);
      while( ! input(PIN_B0))
      {
         speed_ = read_adc();
         if(speed_ < 2)
            speed_ = 2;
         output_d(0b00000011);
         delay_ms(speed_);
         output_d(0b00000110);
         delay_ms(speed_);
         output_d(0b00001100);
         delay_ms(speed_);
         output_d(0b00001001);
         delay_ms(speed_);
      }
      while( ! input(PIN_B1))
      {
         speed_ = read_adc();
         if(speed_ < 2)
            speed_ = 2;
         output_d(0b00001001);
         delay_ms(speed_);
         output_d(0b00001100);
         delay_ms(speed_);
         output_d(0b00000110);
         delay_ms(speed_);
         output_d(0b00000011);
         delay_ms(speed_);
      }
   }
}

Interfacing PIC18F4550 with unipolar stepper motor video:
The following video shows project hardware circuit.