Unipolar Stepper Motor Control Example with PIC12F1822 Microcontroller




This topic shows how to drive 5V unipolar stepper motor in 3 modes one-phase, two-phase and half step. The microcontroller used in this project is Microchip PIC12F1822 and the motor drive circuit is ULN2003.

Unipolar Stepper Motor Control Example with PIC12F1822 Microcontroller
Usually the unipolar stepper motor has 5 wires one for motor supply and the other for coils. This motor has 4 coils and they are connected as shown in the figure below:

Unipolar Stepper Motor Control Example with PIC12F1822  circuit:
All the three control modes have the same circuit schematic as shown below.

Here PIC12F1822 uses its internal oscillator.
The potentiometer connected to AN0 is used to control motor speed and direction of rotation.
Unipolar Stepper Motor Control Example with PIC12F1822 CCS C code:

Unipolar Stepper Motor Control Example with PIC12F1822 Microcontroller schematics
One Phase On Mode (Full Step mode):
In one phase control mode only one coil is energized at once. This mode produces smooth motion with low power consumption. Control sequence has 4 steps as shown in the table below:

CCS C code:

/* Unipolar stepper Motor control using PIC12F1822 (one-phase full step mode) CCS PIC C code
   http://ccspicc.blogspot.com/
   [email protected]
*/
#include <12F1822.h>
#fuses NOMCLR INTRC_IO PLL_SW
#device ADC = 8                                       // Set ADC resolution to 8-bit
#use delay(clock=32000000)
#use fast_io(A)
unsigned int8 i, step_number = 0;
void stepper(int8 step){
  switch(step){
    case 0:
      output_a(0b100000);
    break;
    case 1:
      output_a(0b010000);
    break;
    case 2:
      output_a(0b000100);
    break;
    case 3:
      output_a(0b000010);
    break;
  }
}
void main() {
  setup_oscillator(OSC_8MHZ | OSC_PLL_ON);            // Set internal oscillator to 32MHz (8MHz and PLL)
  output_a(0);
  set_tris_a(1);                                      // Configure RA0 pin as input 
  setup_adc(ADC_CLOCK_DIV_32);                        // Set ADC conversion time to 32Tosc
  setup_adc_ports(sAN0);                              // Configure AN0 pin as analog
  set_adc_channel(0);                                 // Select channel AN0
  while(TRUE){
    output_a(0);
    i = read_adc();                                   // Read from AN0 and store in i
    while(i >= 128){                                  // Move motor in direction 1
      step_number++;
      if(step_number > 3) 
        step_number = 0;
      stepper(step_number);
      delay_ms(257 - i);
      i = read_adc();                                 // Read from AN0 and store in i
    }
    while(i < 128){                                   // Move motor in direction 2
      if(step_number < 1) 
        step_number = 4;
      step_number--;
      stepper(step_number);
      delay_ms(i + 2);
      i = read_adc();                                 // Read from AN0 and store in i
    }
  }
}

Two Phases On Mode (Alternate Full Step mode):
In two-phase mode two coils are energized. This mode produces high torque but its motion is not smooth like the one phase mode. The following table shows this mode sequence:

READ  IR Proximity Motor Control using PIC18F4520

Read more: Unipolar Stepper Motor Control Example with PIC12F1822 Microcontroller




Current Project / Post can also be found using:

  • Motor Speed Controlling by PIC with CCSC code

Leave a Comment

*
= 3 + 0

Read previous post:
OCTAVO SYSTEMS RELEASES OSD3358-SM-RED BEAGLEBONE BLACK COMPATIBLE BOARD
OCTAVO SYSTEMS RELEASES OSD3358-SM-RED BEAGLEBONE BLACK COMPATIBLE BOARD

Octavo Systems back in 2017 released their OSD335x-SM System-In-Package device, a powerful ARM Cortex®-A8 SIP-based package. The OSD335x-SM was a device of its...

Close
Scroll to top