Form WhatsApp

Controlling a Stepper Motor with PIC16F877A

shape image

Controlling a Stepper Motor with PIC16F877A

Stepper motors are widely used in applications requiring precise control of motion, such as robotic car, CNC machines, and 3D printers. In this guide, we will walk through how to control a stepper motor using the PIC16F877A microcontroller. The PIC16F877A is popular due to its versatility, ease of programming, and availability, making it a great choice for controlling stepper motors.

What is a Stepper Motor?

A stepper motor is a type of motor that moves in discrete steps rather than continuous rotation. These steps are achieved by energizing coils inside the motor in a sequence. The number of steps and their accuracy is determined by the motor's design, making it ideal for precision applications.

There are two common types of stepper motors:

  1. Unipolar Stepper Motors: These have five or six wires and use a center-tapped coil configuration.
  2. Bipolar Stepper Motors: These have four wires and use two coils without center taps.

Here we will be using a NEMA17 stepper motor with 4 wires, it is a bipolar stepper motor. Bipolar stepper motors have two coils and require a different driving technique compared to unipolar motors. You will need to use a H-bridge driver like the L298N or similar to control the current direction through the coils.

Here is how to control a bipolar stepper motor using the PIC16F877A with an L298N motor driver.

Components Required:

  • PIC16F877A microcontroller
  • NEMA17 Bipolar stepper motor (4 wires)
  • L298N motor driver module
  • External 12V power supply (for the motor)
  • Crystal oscillator (for PIC clock, 16 MHz recommended)
  • Capacitors (22pF for crystal oscillator)
  • Breadboard or PCB for connections
  • Jumper wires

Circuit Connections:

  1. L298N Motor Driver:

    • Connect the two IN1, IN2, IN3, and IN4 pins of the L298N to the pins RB0, RB1, RB2, and RB3 of the PIC16F877A.
    • Connect the stepper motor’s two coils to the OUT1, OUT2, OUT3, and OUT4 pins of the L298N driver.
    • Provide a 12V external power supply to the L298N’s 12V pin (VCC) and connect the GND pins to the PIC’s ground.
  2. PIC16F877A:

    • Connect a 16 MHz crystal oscillator to the OSC1 and OSC2 pins of the PIC, with two 22pF capacitors connected to ground.
    • Provide a 5V regulated power supply to the PIC’s VDD and VSS pins.

    The circuit diagram of interfacing stepper motor, L298N motor driver shield and the PIC16F877A is shown below.

    Controlling a Stepper Motor with PIC16F877A


Stepper Motor Control Sequence:

For bipolar stepper motors, you will need to control the polarity of the current flowing through the coils. The typical 4-step sequence to rotate the motor is:

  1. Step 1: Coil 1 positive, Coil 2 negative (binary: 1001)
  2. Step 2: Coil 1 positive, Coil 2 positive (binary: 1010)
  3. Step 3: Coil 1 negative, Coil 2 positive (binary: 0110)
  4. Step 4: Coil 1 negative, Coil 2 negative (binary: 0101)

This sequence will move the stepper motor one full step in the forward direction. To reverse the direction, apply the sequence in reverse order.

Code for Controlling Bipolar Stepper Motor:

#include <xc.h>

// Configuration bits
#pragma config FOSC = HS // High-speed oscillator
#pragma config WDTE = OFF // Watchdog Timer disabled
#pragma config PWRTE = ON // Power-up Timer enabled
#pragma config BOREN = ON // Brown-out Reset enabled
#pragma config LVP = OFF // Low Voltage Programming disabled
#pragma config CPD = OFF // Data EEPROM Memory Code Protection disabled
#pragma config WRT = OFF // Flash Program Memory Write Protection disabled
#pragma config CP = OFF // Flash Program Memory Code Protection disabled

#define _XTAL_FREQ 16000000 // Define the crystal oscillator frequency

// Function to step the motor forward
void stepForward(){
// Sequence to move stepper motor forward
PORTB = 0b00001001; // Step 1: Coil 1A high, Coil 2A low
__delay_ms(10);
PORTB = 0b00000101; // Step 2: Coil 1A high, Coil 2B high
__delay_ms(10);
PORTB = 0b00000110; // Step 3: Coil 1B high, Coil 2B high
__delay_ms(10);
PORTB = 0b00001010; // Step 4: Coil 1B high, Coil 2A high
__delay_ms(10);
}

// Function to step the motor backward
void stepBackward(){
// Reverse sequence to move stepper motor backward
PORTB = 0b00001010; // Step 4: Coil 1B high, Coil 2A high
__delay_ms(10);
PORTB = 0b00000110; // Step 3: Coil 1B high, Coil 2B high
__delay_ms(10);
PORTB = 0b00000101; // Step 2: Coil 1A high, Coil 2B high
__delay_ms(10);
PORTB = 0b00001001; // Step 1: Coil 1A high, Coil 2A low
__delay_ms(10);
}

void main(){
TRISB = 0x00; // Set PORTB as output

while(1){
// Rotate stepper motor forward
for(int i = 0; i < 100; i++){ // Rotate 100 steps forward
stepForward();
}
__delay_ms(1000); // Wait for 1 second

// Rotate stepper motor backward
for(int i = 0; i < 100; i++){ // Rotate 100 steps backward
stepBackward();
}
__delay_ms(1000); // Wait for 1 second
}
}


Explanation of the Code:

  • The code initializes the PORTB pins (RB0–RB3) as outputs, which control the stepper motor through the L298N driver.
  • The stepForward function applies the 4-step sequence required to rotate the motor in the forward direction.
  • The stepBackward function applies the same sequence in reverse to rotate the motor in the opposite direction.
  • The motor is rotated 100 steps forward, then 100 steps backward, with a 1-second delay in between.

Tuning the Delay:

The __delay_ms(10) provides a delay of 10 milliseconds between steps, which determines the motor's speed. You can adjust this delay to control the stepper motor’s rotation speed. A smaller delay will result in faster rotation, while a larger delay will slow it down.

Video Demonstration

Below is a video showing how the stepper motor is connected to the L298N motor driver module, the PIC16F877A and how the circuit actually works.

Conclusion:

By using the PIC16F877A and an L298N motor driver, you can easily control a bipolar stepper motor like the NEMA17. This setup allows precise control of motor steps, making it suitable for applications such as CNC machines, 3D printers, and robotic arms. You can further modify the code to control the motor’s speed, direction, or integrate it into more complex systems.

Further Reading:

 

Post a Comment

© Copyright ee-diary

Form WhatsApp

This order requires the WhatsApp application.

Order now