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:
- Unipolar Stepper Motors: These have five or six wires and use a center-tapped coil configuration.
- 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:
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.
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.
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:
- Step 1: Coil 1 positive, Coil 2 negative (binary: 1001)
- Step 2: Coil 1 positive, Coil 2 positive (binary: 1010)
- Step 3: Coil 1 negative, Coil 2 positive (binary: 0110)
- 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:
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:
- PIC16f877A LED Blink Code
- How to Control an LED Using a Switch with PIC16F877A
- ADC Example Code for PIC16F877A
- How to use UART with PIC16F877A
- How to Calculate the PR2 register value in PIC16F877A
- DC Motor Control with PIC16F877A PWM Signals
- PIC16F877A Timers Explained: Understanding and Using Timers in Embedded Projects
- Controlling Servo Motors with PIC16F877A: Applications in Robotics