Lab: Controlling a Stepper Motor With an H-Bridge

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 h-bridge or a Texas Instruments SN754410 h-bridge.

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

A short solderless breadboard with two rows of holes along each side. There are no components mounted on the board. The board is oriented sideways so that the long rows of holes are on the top and bottom of the image.
A short solderless breadboard.
An Arduino Uno. The USB connector is facing to the left, so that the digital pins are on the top of the image, and the analog pins are on the bottom.
An Arduino Uno.
Three 22AWG solid core hookup wires are shown here. Each is about 6cm long. The top one is black; the middle one is red; the bottom one is blue. All three have stripped ends, approximately 4 to 5mm on each end.
22AWG solid core hookup wires.
A DC power jack. It pairs with a plug with a 2.1mm inside diameter, 5.5mm outside diameter plug, and has screw terminals on the back so that you can attach wires to it.
A DC Power Jack
DC Power Supply. Shown here is a +12 Volt 1 Amp Center Positive DC power supply with a 2.1mm male jack. This size fits the Arduino Uno's female jack.
DC Power Supply
This component is a small rectangle with eight pins on each of the two long sides. There is a small round depression on the upper left corner of the rectangle, and a semicirular dip in the middle of the top.
H-bridge IC, L293D
Photo of a stepper motor. This motor is approximately 2 inches (5cm) on diameter, with an off-center shaft at the top, and wires protruding from the bottom. You can tell a stepper motor from a DC motor because steppers have at least four wires, while regular DC motors have two.
a stepper motor

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:

An Arduino Uno on the left connected to a solderless breadboard, right. The Uno's 5V output hole is connected to the red column of holes on the far left side of the breadboard. The Uno's ground hole is connected to the blue column on the left of the board. The red and blue columns on the left of the breadboard are connected to the red and blue columns on the right side of the breadboard with red and black wires, respectively. These columns on the side of a breadboard are commonly called the buses. The red line is the voltage bus, and the black or blue line is the ground bus.
An Arduino Uno on the left connected to a solderless breadboard, right.

Made with Fritzing

Set up the H-bridge

This component is a small rectangle with eight pins on each of the two long sides. There is a small round depression on the upper left corner of the rectangle, and a semicirular dip in the middle of the top.
H-bridge IC, L293D

This example uses an H-bridge integrated circuit, the Texas Instruments L293NE or Texas Instruments SN754410. Many distributors such as Digikey, SparkFun, Mouser and Jameco sell them.

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 drawing of an h-bridge connected to an Arduino for driving a stepper motor. The Arduino's Vin is connected to the positive side of a DC power jack. The negative side of the jack is connected to the Arduino's ground. The stepper's pins 1,9, and 16 are connected to the Arduino's +5 volt output. Pins 4,5,12,and 13 are connected to ground. Pin 8 is connected to the positive side of the DC power jack. The control pins are not yet connected.
Schematic view of an h-bridge connected to an Arduino for driving a stepper motor.
Breadboard view of an h-bridge connected to an Arduino for driving a stepper motor. The Arduino and breadboard are connected as described in the previous breadboard view. The H-bridge straddles the middle of the breadboard in rows 10 through 16. The H-bridge's pins 1, 9, and 16 are connected to the left or right voltage buses of the breadboard, whichever is closest to their side. An external DC power jack connects to the right side ground bus of the breadboard from its negative terminal, and its positive terminal connects to pin 8 of the H-bridge.
Breadboard diagram of an H-bridge and an Arduino wired for control of a stepper.

Made with Fritzing

How the stepper motor works

The stepper motor has two coils to control it. Each coil has a center connection as well, and the center connections are joined together, which is what makes this a unipolar stepper. If you don’t connect the center connection, then the motor will work very much like a bipolar stepper, each coil operating independently. This is how you’ll use it for this exercise. Each coil will connect to one side of the H-bridge. The pink and orange wires (wires number 2 and 4) are connected tothe first coil. They will connect to one side of the bridge, while the yellow and blue wires (wires number 3 and 5) are the other coil, and will connect to the other side of the bridge. In this case, the red wire, pin 1, will not be used.

Schematic drawing of a stepper motor. A circle represents the motor, and two coils to the left and bottom of the circle represent the coils. The ends of the left coil are labeled pink and orange. The ends of the bottom coil are labeled yellow and blue. The middles of both coils are connected together, and labeled red. The red connection will not be used in this example.
Schematic drawing of a stepper motor.

Made with Fritzing

Connect the motor to the H-bridge

Connect the motor to the H-bridge as shown below:

Schematic drawing of an h-bridge connected to an Arduino for driving a stepper motor. The drawing is similar to the previous schematic, and this time a stepper motor has been added. The motor's pink wire (wire 4) is connected to pin 3 of the H-bridge. The motor's orange wire (wire 2) is connected to pin 6 of the H-bridge. The motor's yellow wire (wire 3) is connected to pin 11 of the H-bridge. The motor's blue wire (wire 5) is connected to pin 14 of the H-bridge. The motor's red wire (wire 1) is not connected to anything.
Schematic of an Ardiuino and an H-bridge, with the stepper motor added.

 

Breadboard view of an h-bridge connected to an Arduino for driving a stepper motor. The board is wired similarly to the previous breadboard view, and this time a stepper motor has been added. The motor's pink wire (wire 4) is connected to the same row as pin 3 of the H-bridge. The motor's orange wire (wire 2) is connected to the same row as pin 6 of the H-bridge. The motor's yellow wire (wire 3) is connected to the same row as pin 11 of the H-bridge. The motor's blue wire (wire 5) is connected to the same row as pin 14 of the H-bridge. THe motor's red wire (wire 1) is not connected to anything.
Breadboard view of an h-bridge connected to an Arduino, with the stepper motor added.

Note that the H-bridge’s DC power is coming from the 12V DC connector. It shares a common ground with the Arduino, though. You could also use the Arduino’s DC power jack and power the motor from the Vin pin.

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 shown below:

Schematic drawing of an h-bridge and stepper motor connected to an Arduino. The drawing is similar to the previous schematic. This time a control wires for the H-bridge have been added. Pin 2 of the H-bridge connects to digital pin 8 of the Arduino. Pin 7 of the H-bridge connects to digital pin 9. Pin 10 of the H-bridge connects to digital pin 10, and pin 15 of the H-bridge connects to digital pin 11.
Schematic drawing of an h-bridge and stepper motor connected to an Arduino., with the control wires added.
Breadboard drawing of an h-bridge and stepper motor connected to an Arduino. Similar to the previous breadboard view, the control wires for the H-bridge have been added. Pin 2 of the H-bridge connects to digital pin 8 of the Arduino. Pin 7 of the H-bridge connects to digital pin 9. Pin 10 of the H-bridge connects to digital pin 10, and pin 15 of the H-bridge connects to digital pin 11.
Breadboard drawing of an h-bridge and stepper motor connected to an Arduino, with the control wires added.

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.

Unipolar Stepper Control

The steps above showed you how to control a bipolar stepper, but the motor shown was actually a unipolar motor. Remember the red wire you didn’t connect? That wire connects the two coils and can act as a common power source or ground wire. To use the motor as a unipolar motor, try connecting that wire (wire 1) of the motor to the +12V power supply from the DC power jack.

Note for 3.3V Microcontrollers

The H-bridge used in this lab are great if you’re using an Uno or other microcontroller that operates at 5 volts, but if you’re using one of the more modern Arduino or Arduino-compatible boards, it probably operates at 3.3 volts. For example, all of the M0-based controllers, like the MKR series or the Adafruit Feather M0 series, and the ESP8266 all operate at 3.3 volts. They do not supply a high enough logic voltage for the L293 H-bridge. For these boards, you might want to use a 3.3-volt compatible board, like the Toshiba TB6612FNG H-bridge. There’s both a  Sparkfun breakout board and an Adafruit breakout board for this part, and A Pololu breakout board as well. It can handle a motor  supply voltage up to 15V, and  it operates on a logic voltage of 2.7–5.5V. It can control an output current of 1.2A. It’s similar to the L393, in that it has two H-bridges, each with two logic inputs and two motor outputs. The enable inputs on the Toshiba part are called PWM inputs, because they are expected to be used for speed control like you saw with the L293 above. There’s also a Standby pin that you have to connect to voltage through a 10-kilohm pullup resistor to activate the h-bridges.

Schematic of TB6612 H-bridge controlling a stepper
Schematic of TB6612 H-bridge controlling a stepper
Breadboard view of TB6612 H-bridge controlling a stepper
Breadboard view of TB6612 H-bridge controlling a stepper
Originally written on August 23, 2014 by Benedetta Piantella Simeonidis
Last modified on October 30, 2018 by Tom Igoe