Lab: Controlling a Stepper Motor With an H-Bridge

 

Last edited 23 August 2014 by Benedetta Piantella

Introduction

Stepper motors are motors that have multiple coils in them, so that they can be moved in small increments or steps. Stepper motors are typically either unipolar or bipolar, meaning that they have either one main power connection or two. Whether a stepper is unipolar or bipolar, however, you can control it with an H-bridge. This lab shows you how to set up a unipolar stepper motor using an H-Bridge. You can use the same control circuit with a bipolar motor too, however. The H-bridge used in this circuit is a basic one, the Texas Instruments L293NE or a Texas Instruments SN754410.

What You’ll Need to Know

To get the most out of this lab, you should be familiar with the following concepts. You can check how to do so in the links below:

Things You’ll Need

For this lab you'll need:
Solderless breadboard Hook-up wire Arduino Stepper motor
Solderless Breadboard 22-AWG hookup wire Arduino microcontroller Unipolar stepper motor
L293NE H-bridge DC power supply DC motor
L293NE or SN754410 H-bridge 12V DC power supply DC power jack

Prepare the breadboard

Connect power and ground on the breadboard to power and ground from the microcontroller. On the Arduino module, use the 5V and any of the ground connections:

Made with Fritzing

Set up the H-bridge

L293NE H-bridge

This example uses an H-bridge integrated circuit, the Texas Instruments L293NE or Texas Instruments SN754410. There is one in your Physical Computing Kit, and the NYU Book Store and many distributors such as Digikey, SparkFun, Mouser and Jameco sell them as well.

The H-bridge will be used in a manner very similar to the DC Motor Control lab. But because the stepper has two coils instead of one, it’ll be as if you were driving two motors with the H-bridge.

Connect the H-bridge

Connect the H-bridge as shown below:

Schematic view H-Bridge power and ground connections

Connect the motor to the H-bridge

Connect the motor to the H-bridge as follows:

Note that the H-bridge’s DC power is coming from the 12V DC connector. It shares a common ground with the Arduino, though.

Connect the H-Bridge to the microcontroller

The H bridge’s control inputs are connected to the microcontroller’s input pins digital 8 through 11 as follows:

Once you have those connected, you’re ready to program the microcontroller.

Program the microcontroller

Program the microcontroller to run the stepper motor through the H-bridge using the stepper library. For your first program, it’s a good idea to run the stepper one step at a time, to see that all the wires are connected correctly. If they are, the stepper will step one step forward at a time, every half second, using the code below:

#include <Stepper.h>

const int stepsPerRevolution = 512;  // change this to fit the number of steps per revolution
                                     // for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9,10,11);            

int stepCount = 0;         // number of steps the motor has taken

void setup() {
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one step:
  myStepper.step(1);
  Serial.print("steps:" );
  Serial.println(stepCount);
  stepCount++;
  delay(500);
}

Once you’ve got that working, try making the stepper move one whole revolution at a time. The number of steps per revolution will depend on your individual stepper, so check the data sheet for the number of steps per revolution:

#include <Stepper.h>

const int stepsPerRevolution = 512;  // change this to fit the number of steps per revolution
                                     // for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9,10,11);            

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(10);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one revolution  in one direction:
   Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(500);

   // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(-stepsPerRevolution);
  delay(500);
}

With a high-step-count stepper, you may need to change the speed. If the motor steps are run too fast, the motor coils don’t have a chance to energize and de-energize in order to step the motor.

Attach something to the stepper

If you want to mount an arm or pointer to the stepper motor, you need to make a hole for the pointer that fits the shaft perfectly. You could measure this with a caliper. Here is an SVG file of an arrow with a shaft mounting hole perfectly sized for the stepper used in this lab.