Stepper Motor Drive using H-bridge

 Objective

The stepper motor is useful in applications that have a high torque requirement, as well as in applications where very precise movements are required. A typical stepper motor will move in 1.8 degree or 0.9 degree movements thus resulting in 200 or 400 steps per revolution respectively. A stepper motor can be driven by two H-bridges, and this project example shows how to set that up using the MPLAB Code Configurator (MCC) within MPLAB Xpress.

 Materials

Hardware Tools (Optional)

Tool About Purchase
Xpress-50px.png
MPLAB® Xpress
Development Board

Software Tools

Tool About Installers
Installation
Instructions
Windows Linux Mac OSX
MPLAB® Xpress
Cloud Integrated Development Environment

This project uses

Useful Resources

The stepper motor used in this tutorial is a bi-polar stepper motor, meaning that it is made of two coils which can turn the motor in opposite directions. See the very simplified drawing below:

figure1.jpg

Each coil of wire controls two poles around the rotor of the motor. This is a very high level overview. The image above will be useful for understanding the operation of this tutorial as you work through it.

 Connection Diagram

The first step is to build the two H-bridges necessary to control the two coils in the motor.

The circuit is as follows:

figure2.JPG

 Procedure

1

Task 1

The first bridge is run by pins RB2 through RB5. If you are using the motor listed in the Materials section of this article, the brown lead will connect to the left side of the first H-bridge and the orange lead will connect to the other side. Similarly, the red lead will connect to the left side of the second H-bridge and the yellow lead will connect to the right. If you are using a different motor, make sure the leads connect in a similar orientation to the schematic above. Because some motors will run on upwards of 12 V and much higher current than the microcontroller is able to send from one pin, the H-bridge uses MOSFETs which act as electric switches. The MOSFETs allow small current/voltages like those on the microcontroller pins to control much larger current/voltages coming from an external power supply.

To follow along with these steps, MPLAB® Xpress should be open and the user should be logged in so that the MPLAB Code Configurator plug-in can be used.

2

Task 2

Create a new project in MPLAB® Xpress for a PIC16F1855 using the MPLAB Xpress Development Board.

Instructions are below if this is your first project.

3

Task 3

Open the MPLAB® Code Configurator (MCC).

Instructions are below if this is your first project.

4

Task 4

This tutorial uses the Complementary Waveform Generator (CWG) to run each of the H-bridges. The CWG has several different application modes, but for the purposes of this tutorial we are using the forward and reverse modes only. These two modes drive all four gates of the H-bridge simultaneously, minimizing the amount of written code after the initialization. The CWG can be found under the Device Resources section of the MPLAB Xpress Code Configurator.

figure3.JPG

Because we have two H-bridges to control, we need to use two CWG modules.

figure16.JPG

5

Task 5

Double-clicking CWG1 opens the following window:

figure9.JPG

This window shows the adjustments to the CIP that have already been made. The Input Source is PWM6_OUT. The Output Mode is set to FWD Full Bridge mode because full bridge mode will enable the CWG to drive the current through the wire coil in the stepper motor in both directions. The clock source, which refers to the clock source for the Dead-Band delay, is set to FOSC. To prevent both MOSFETs on one side of the H-bridge from opening at the same time, the setup includes a dead-band delay of 2 to 3 microseconds. This is a safe dead-band time to prevent the same side MOSFETs from turning on simultaneously, but may be too high for some applications.

6

Task 6

The CWG takes an input signal to run, and the input signal used will be the PWM6 module. This can also be found under Device Resources by double-clicking PWM6:

figure4.JPG

In forward mode, the gate of Q1 is opened while the Q4 gate is sent the PWM input signal. The gates of Q2 and Q3 are closed. In reverse mode, Q1 and Q4 are closed while Q3 is opened and Q2 is sent the PWM signal. The PWM signal opens the gate proportional to the compare value of the PWM signal. This allows current to flow from the corresponding top gate, through the motor in the middle, and down through the PWM enabled gate.

figure5.jpg

What sets this tutorial apart from driving a DC motor with an H-bridge is that the stepper requires two H-bridges. Each H-bridge will drive one of the coils seen in the figure in the introduction. This way, we can drive one pole of the magnet high while driving the other side low, effectively pulling and pushing into the correct rotational position.

7

Task 7

Here is a simple explanation of the drive concept:

The green arrows represent the current controlled directionally by the CWG output signals opening select gates. Depending upon which side of the coil the CWG drives HIGH, the current in the coil will drive the magnets’ poles in a similar fashion. In the first image, the + and - poles of both magnets hold the motor in a single position.

figure1.png

Next, one coil is turned completely off, causing the motor to rotate 45 degrees to align with the magnet that still has current running through the wire. The method of rotating in 45 degree increments, as opposed to 90, is called half-stepping. This will provide a smoother motor turn, but torque is not consistent because the coils alternate on and off.

figure6.jpg

Next, both windings are turned on, but this time the B winding is charged in the opposite direction, changing the poles of the B magnet. This causes another 45 degree step in the same direction.

figure7.jpg

Finally, the A winding is now turned off and the shaft of the motor aligns with the B magnet completely. Remember, this is a very simplified diagram, and the stepper will almost always have more teeth which correspond to more poles and smaller step sizes. The number of these teeth will correspond to the resolution of the motor.

figure8.jpg

8

Task 8

The PWM module is setup as below:

figure10.JPG

Using the PWM signal, as opposed to simply a HIGH on the MOSFET gate, comes in handy in more advanced applications that involve micro-stepping. While the microstepping method is not covered in this tutorial, a great resource can be found here: AN906 "Stepper Motor Control Using the PIC16F684".

Remember to choose Timer 4 (TMR4) to support the PWM6 module. This is also found under Device Resources:

figure17.JPG

Set it up as follows: Change the Clock Source to FOSC/4 because this timer will be used with a PWM module. Change the Prescaler to 1:16 so that the period is long enough to register with the stepper motor. See these changes below:

figure11.JPG

9

Task 9

The next step is to lock the correct pins to their corresponding CWG outputs in order to match the schematic in step 1. The following Pin Configurator view shows the correct locks. To close the lock, click on it:

figure18.JPG

10

Task 10

Once these modules have been completed, it is time to Generate the initialization code and return to the MCC.

Figure%205.JPG

You can tell if the code has generated properly by the inclusion of the libraries for each CIP in the MCC Code Configurator:

figure12.JPG

Remember from step 3b that to turn the motor we must change when a coil is energized and in what direction the coil around the magnets turn on. In order to do this, we will leverage the forward and reverse settings of the Full Bridge modes. To control this we will utilize some written code in the main.c file. The code follows this format:

CWG1CON0 = 0x83;
CWG2CON0 = 0x82;
__delay_ms(50);

CWG1CON0 refers to a register that controls some settings, including forward or reverse mode, of the CWG1 module. CWG2CON0 refers to the similar register for CWG2. 0x83 sets the CWG to reverse mode, and 0x82 to forward. Finally, 0x02 or 0x03 turns off the CWG, causing the current to stop flowing through that particular coil.

With that in mind, look at the code and accompanying simplified diagram:

CWG1CON0 = 0x83;      //Reverse mode
CWG2CON0 = 0x82;      //Forward mode
__delay_ms(50);
figure19.jpg
CWG1CON0 = 0x02;      //Coil off                
CWG2CON0 = 0x82;      //Forward mode
__delay_ms(50);
figure13.jpg
CWG1CON0 = 0x82;     //Forward mode
CWG2CON0 = 0x82;     //Forward mode
__delay_ms(50);
figure14.jpg
CWG1CON0 = 0x82;      //Forward mode        
CWG2CON0 = 0x02;      //Coil off
__delay_ms(50);
figure15.jpg

Putting these together and including the rest of the code to complete one full rotation cycle results in the following code in main.c:

#include "mcc_generated_files/mcc.h"
 
/*
                         Main application
 */
void main(void)
{
    // initialize the device
    SYSTEM_Initialize();
 
    while (1)
    {
        CWG1CON0 = 0x83; //Reverse
        CWG2CON0 = 0x82; //Forward
        __delay_ms(50);
 
        CWG1CON0 = 0x02; //Off
        CWG2CON0 = 0x82; //Forward
        __delay_ms(50);
 
        CWG1CON0 = 0x82; //Forward
        CWG2CON0 = 0x82; //Forward
        __delay_ms(50);
 
        CWG1CON0 = 0x82; //Forward
        CWG2CON0 = 0x02; //Off
        __delay_ms(50);
 
        CWG1CON0 = 0x82; //Forward
        CWG2CON0 = 0x83; //Reverse
        __delay_ms(50);
 
        CWG1CON0 = 0x03; //Off
        CWG2CON0 = 0x83; //Reverse
        __delay_ms(50);
 
        CWG1CON0 = 0x83; //Reverse
        CWG2CON0 = 0x83; //Reverse
        __delay_ms(50);
 
        CWG1CON0 = 0x83; //Reverse
        CWG2CON0 = 0x03; //Off
        __delay_ms(50);
 
    }
}

11

Task 11

Generate a .hex file by clicking Make and Program Device

Figure%207.JPG

Program the MPLAB Xpress board by dragging the generated project .hex file from the downloads section of the browser and dropping the file on to the XPRESS drive.

Figure14.png

The Programmer LED on the Xpress board should quickly flash from green to red and then back to green indicating that the .hex file was successfully programmed to the PIC16F18855.

When the MPLAB Xpress board is programmed, the stepper motor will rotate in the counter-clockwise direction. However, in most cases, it is useful to drive the motor in both directions. The rotation code must be slightly altered in order to accomplish this. This is seen in the modified code below:

while (1)
    {
        CWG1CON0 = 0x82; //Forward
        CWG2CON0 = 0x02; //Off
        __delay_ms(50);
 
        CWG1CON0 = 0x82; //Forward
        CWG2CON0 = 0x82; //Forward
        __delay_ms(50);
 
        CWG1CON0 = 0x02; //Off
        CWG2CON0 = 0x82; //Forward
        __delay_ms(50);
 
        CWG1CON0 = 0x83; //Reverse
        CWG2CON0 = 0x82; //Forward
        __delay_ms(50);
 
        CWG1CON0 = 0x83; //Reverse
        CWG2CON0 = 0x03; //Off
        __delay_ms(50);
 
        CWG1CON0 = 0x83; //Reverse
        CWG2CON0 = 0x83; //Reverse
        __delay_ms(50);
 
        CWG1CON0 = 0x03; //Off
        CWG2CON0 = 0x83; //Reverse
        __delay_ms(50);
 
        CWG1CON0 = 0x82; //Forward
        CWG2CON0 = 0x83; //Reverse
        __delay_ms(50);
 
    }

 Results

The project, when programmed into the Xpress board, controls the motor speed and direction. This can be altered in many ways, but demonstrates the basic principal of controlling a stepper motor with a PIC® Microcontroller using the CWG peripheral.

 Analysis

The CWG module along with the PWM create the signals required to drive the H-bridge and thus the stepper motor. The main.c file can be changed to reverse the motor direction. Adding more delays between steps is a simple way of controlling the speed of the motor.

 Conclusions

There are many options for controlling a stepper motor, including integrated circuits dedicated to this purpose, that make driving a motor in smaller increments known as micro-stepping much easier to do. But to really understand how driving a stepper motor works, it helps to understand how to drive a stepper directly, which is the purpose of this project.

© 2016 Microchip Technology, Inc.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.