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:
- How to solder up a connector
- Electronic components
- How DC Motors work
- How to use transistors to control high current loads with Arduino
- What is an H-Bridge and how it works
- What is an Arduino Library
Things You’ll Need
Arduino Nano 33 IoT A solderless breadboard with two rows of holes along each side. Flexible jumper wires. Motor Driver (H-bridge), model TB6612FNG a stepper motor A DC Power Jack DC Power Supply
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.

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.
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.
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.
You can change the direction and speed of the motor using the motor driver. The truth table below shows how the motor driver works.
AI1 | AI2 | PWMA | B1 | B2 | PWMB | Coil 1 | Coil 2 | |||
---|---|---|---|---|---|---|---|---|---|---|
H | L | H | – | – | L | Direction 1 | Off | |||
L | H | H | – | – | L | Direction 2 | Off | |||
L | L | – | – | – | L | Off | Off | |||
H | H | – | – | – | L | Off | Off | |||
– | – | L | H | L | H | Off | Direction 1 | |||
– | – | L | L | H | H | Off | Direction 2 | |||
– | – | L | H | H | H | Off | Off | |||
– | – | L | L | L | H | Off | Off |
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.



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.