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. The common feature to all stepper motors is that they have two coils in the motor rather than one. You control the stepper by energizing one coil, then reversing its polarity, then doing the same to the other coil. To do this, you can use a dual H-bridge driver like the TB6612FNG that you used in the DC motors and H-bridge lab. This lab shows you how to set up stepper motor using an 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

Figures 1-7 are the parts you’ll need for this exercise.

Prepare the breadboard

Connect power and ground on the breadboard to power and ground from the microcontroller. On the Arduino module, use the 5V or 3.3V (depending on your model) and any of the ground connections, as shown in Figures 8 and 9.

An Arduino Uno on the left connected to a solderless breadboard, right.
Figure 8. Breadboard drawing of an Arduino Uno on the left connected to a solderless breadboard on the right

Figure 8 shows 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.


Arduino Nano on a breadboard.
Figure 9. Breadboard view of an Arduino Nano mounted on a solderless breadboard.

As shown in Figure 9, the Nano is mounted at the top of the breadboard, straddling the center divide, with its USB connector facing up. The top pins of the Nano are in row 1 of the breadboard.

The Nano, like all Dual-Inline Package (DIP) modules, has its physical pins numbered in a U shape, from top left to bottom left, to bottom right to top right. The Nano’s 3.3V pin (physical pin 2) is connected to the left side red column of the breadboard. The Nano’s GND pin (physical pin 14) is connected to the left side black column. 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. The blue columns (ground buses) are connected together at the bottom of the breadboard with a black wire. The red columns (voltage buses) are connected together at the bottom of the breadboard with a red wire.

Made with Fritzing

Set up the Motor Driver

A stepper motor is basically two motor coils in one motor,which allows you to turn the motor in steps. For more on this, see this stepper motor page. This tutorial uses a Toshiba motor driver, the TB6612FNG, which can actually drive two DC motors or the two coils of a stepper motor. There’s a  Sparkfun breakout board, an Adafruit breakout board, and a Pololu breakout board for this part as well.

How the stepper motor works

The stepper motor has two coils to control it as shown in Figure 10. 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 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 to the first coil. They will connect to one channel of the bridge (channel A), while the yellow and blue wires (wires number 3 and 5) are the other coil, and will connect to the other channel of the bridge (channel B). 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.
Figure 10. Schematic drawing of a stepper motor.

How The Motor Driver Works

The TB6612FNG 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 has two motor driver circuits, each with two logic inputs and two motor outputs. Each motor driver has a PWM input, because they are expected to be used for speed control for the motor by pulse width modulating this pin. You won’t be using the PWM pins for this exercise though. There’s also a Standby pin that you have to connect to voltage through a 10-kilohm pullup resistor to activate the driver circuits.

The motor driver has the following pins. The pin numbers shown here are for the Sparkfun breakout board. The order of the pins will be different for the Adafruit and Pololu boards. The Pins are numbered here in a DIP fashion, in a U-shape from top left to bottom left, then bottom right to top right.

  • VMOT – motor voltage supply input, up to 15V.
  • Vcc – logic voltage supply  input, 2.7-5.5V
  • Gnd – ground
  • AO1 – A channel output 1. This is the first motor terminal for the first motor driver
  • AO2 – A channel output 2.  This is the second motor terminal for the first motor driver
  • BO2 – B channel output 2.  This is the second motor terminal for the second motor driver
  • BO1 – B channel output 1.  This is the first motor terminal for the second motor driver
  • Gnd – ground
  • Gnd – ground
  • PWMB – B Channel PWM Enable. This pin controls the speed for channel B, regardless of the channel’s direction
  • BI2 – B channel input 2.  This controls B channel output 2. To control that pin, take this pin HIGH or LOW.
  • BI1 – B channel input 1.  This controls B channel output 1. To control that pin, take this pin HIGH or LOW.
  • Stdby – enables both drivers when you take it HIGH  and disables them when you take it LOW
  • AI1 – A channel input 1.  This controls A channel output 1. To control that pin, take this pin HIGH or LOW.
  • AI2 – A channel input 2.  This controls A channel output 2. To control that pin, take this pin HIGH or LOW.
  • PWMA – A Channel PWM Enable. This pin controls the speed for channel A, regardless of the channel’s direction

Figure 11 shows the Sparkfun board, and Figures 12 and 13 show the Pololu board front and back. The Pololu board is labeled on the back. You can see that both boards have the same pins, even though the layouts are different.

Photo of a Motor Driver (H-bridge), model TB6612FNG
Figure 11. Motor Driver (H-bridge), model TB6612FNG

Photo of a motor driver, Pololu's TB6612FNG Dual Motor Driver Carrier (front view of the board)
Figure 12. Pololu’s TB6612FNG Dual Motor Driver Carrier (front view of the board)
Photo of a motor driver, Pololu's TB6612FNG Dual Motor Driver Carrier (back of the board)
Figure 13. Pololu’s TB6612FNG Dual Motor Driver Carrier (back of the board)

You can change the direction and speed of the motor using the motor driver. The truth table below shows how the motor driver works.

AI1AI2PWMAB1B2PWMBCoil 1Coil 2
HLHLDirection 1Off
LHHLDirection 2Off
LLLOffOff
HHLOffOff
LHLHOffDirection 1
LLHHOffDirection 2
LHHHOffOff
LLLHOffOff
Table 1. States of the TB6612FNG and the coil states

For this lab, the PWMA and PWMB pins connect to Vcc so that the driver circuits stay fully energized. The motor logic pins are also connected to designated digital pins on your Arduino so you can set them HIGH and LOW to turn the motor in one direction, or LOW and HIGH to turn it in the other direction. The motor supply voltage connects to the voltage source for the motor, which is usually an external power supply.

Connect the H-bridge and Motor

Connect the H-bridge as shown in Figure 14 – 16. Note that the H-bridge’s VMOT is connected to the external Vin DC power connector. It shares a common ground with the Arduino, though. This assumes your motor operates on a voltage higher than your Arduino. Make sure you match the voltage and amperage of your DC power supply to the voltage and amperage of the motor.

The motor shown in the images here is a 5V Small Reduction Stepper Motor, 32-Step, with 1:16 Gearing. That means the motor’s got 32 steps per revolution, but the gearbox on top multiplies that by 16, making it 512 steps per revolution. The link shown is to Adafruit’s version, but you can find the same motor from other retailers. Look for the model number 28-BYJ48 and the voltage designation 5V.

This motor nominally runs on 5 volts. It will run as low as 3.3 volts if you give it enough current (about 110 mA), and it will run on up to 12V, but it will run hot. It’s always a good idea to match your supply to your motor. The circuit diagrams here show how to run it on an external DC supply, but it’s up to you to match that supply to the motor. Keep in mind that if you have, say, a 12-Volt power supply and a 5-volt motor, you can add a 5-volt voltage regulator, as shown in the breadboard lab.

Schematic view of an h-bridge connected to an Arduino for driving a stepper motor.
Figure 14. Schematic view of an h-bridge connected to an Arduino for driving a stepper motor.
Figure 15. Breadboard diagram of an H-bridge and an Arduino Uno wired for control of a stepper.
Figure 16. Breadboard diagram of an H-bridge and an Arduino Nano 33 IoT wired for control of a stepper.

Made with Fritzing

Once you have the motor and the driver 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.

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 Vin power supply from the DC power jack.