Lab: DC Motor Control Using an H-Bridge

Introduction

In this tutorial, you’ll learn how to control a DC motor’s direction using an H-bridge.

To reverse a DC motor, you need to be able to reverse the direction of the current in the motor. The easiest way to do this is using an H-bridge circuit. There are many different models and brands of H-Bridge. This tutorial uses one of the most basic, a Texas Instruments L293NE or a Texas Instruments SN754410.

If you simply want to turn a motor on and off, and don’t need to reverse it, for example if you’re controlling a fan, try the tutorial on controlling high current loads with transistors.

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.
Three short pieces of hookup wire: one is clad in red insulation, one in blue, and one in black. All three have exposed ends approximately 5mm long.
Three short pieces of hookup wire
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.
Photo of a toggle switch. This is a panel-mount switch, meant to be mounted in an instrument panel. It is about 0.5 in (2cm) long and has two wires protruding from it.
A switch
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
DC geared motor, hobby size. The motor shaft has a round blue hub attached.
DC Motor. Pictured motor also has a round plate attached to the shaft
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
9V battery snap. This flat component with rounded ends has two round metal mounts on one side. One is slightly bigger than the other, about half an inch (3-2cm) across. They are designed to snap onto a 9-volt battery. There is a pair of wires coming out one end that has a DC power plug attached to it. This can plug into a DC power jack.
9V battery snap with DC power plug on it, and a 9V battery.

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

Add a Digital Input (a switch)

Connect a switch to digital input 2 on the Arduino.

Schematic drawing of an Arduino and switch. The switch is attached to the +5 volt output of the Arduino. The switch's other pin is connected to digital pin 2. A 10-kilohm resistor is also connected to pin 2 on one side, and to ground on the other.
Schematic Diagram of a switch attached to an Arduino as a digital input
Breadboard view of a switch attached to an Arduino. The Arduino is connected to a breadboard as described in the image above. A switch is mounted in rows 4 through 6 in the left center section of the breadboard. A red wire connects from the left side voltage bus to row 7. A blue wire connects row 8 to digital pin 2 on the Arduino. a 10-kilohm resistor connects row 8 to the ground bus on the left side of the board.
Breadboard view of a switch attached to an Arduino as a digital input

Find a motor

Find yourself a DC motor that runs on low DC voltage within the range of 5 – 15V.  discarded toys and printers can be good sources of these. The ITP junk shelf is almost always a goldmine for discarded motors and fans. Asking classmates and second years is another good approach.

Solder leads to the motor’s terminals. The motor’s direction depends on the polarity, so it’s helpful to use different colors so you know which way the motor will turn when you hook it up.

Optional: Consider testing your motor with a bench power supply from the equipment room. Ask a teacher or resident if you need help setting one up. Begin by adjusting the voltage on the bench power supply and observe its effects. Take note of its speed at different voltages without dipping to low or too high.

Safety Warning: Running a motor at a voltage much lower or much higher than what it’s rated for could potentially damage or permanently destroy your motor. When the motor doesn’t spin, the voltage is too low. When the motor runs hot, or sounds like it’s straining, the voltage is too high.

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.

Connect the motor to the H-bridge

Connect the motor to the H-bridge as follows:

Schematic diagram of an Arduino connected to an H-bridge to control a DC motor. The Arduino and switch are connected as described in the drawing above. An L293D H-bridge has been added, and is connected to as follows: Pin 1 is connected to the Arduino's digital pin 9. Pin 2 is connected to digital pin 4. Pins 3 and 6 are connected to the DC motor's two connections. Pins 4, 5, 12, and 13 are connected to ground. Pin 7 is connected to digital pin 3 on the Arduino. Pin 8 is connected to the positive terminal of a 9-volt battery. The battery's negative terminal is connected to ground. Pin 16 of the H-bridge is connected to the Arduino's +5 volt output. The rest of the H-bridge pins are unconnected.
Schematic diagram of an Arduino connected to an H-bridge to control a DC motor.
Breadboard drawing of an Arduino connected to an H-bridge to control a DC motor. The Arduino and switch are connected as described in the breadboard drawing above. An L293D H-bridge has been added, straddling the center of the breadboard in rows 12 thorugh 19. Pin 1 of the H-bridge is connected to the Arduino's digital pin 9. Pin 2 is connected to digital pin 4. Pins 3 and 6 are connected to the DC motor's two connections. Pins 4, 5, 12, and 13 are connected to the ground buses of the breadboard on left and right, respectively. Pin 7 is connected to digital pin 3 on the Arduino. Pin 8 is connected to the positive terminal of a 9-volt battery. The battery's negative terminal is connected to the ground bus on the right side of the breadboard. Pin 16 of the H-bridge is connected to the +5 volt bus on the right side of the board. The rest of the H-bridge pins are unconnected.
Breadboard view of an Arduino connected to an H-bridge to control a DC motor.

If you need an external power supply, you can use any DC power supply or battery from 9 – 15V as long as your motor can run at that voltage, and as long as the supply can supply as much current as your motor needs. Related video: Connect 12V Power Supply (video)Wiring a power supply using a DC power jack (video). If you are using an external power supply,  you would connect the H-bridge’s supply pin (pin 7) to the Vin pin on the Arduino.

Schematic diagram of an Arduino connected to an H-bridge to control a DC motor. The switch, motor, and H-bridge are wired as they are in the previous diagram, but the battery is connected to the Arduino's Vin pin in this diagram. The H-bridge's supply pin (pin 7) is also connected to the Vin pin.
Schematic diagram of an Arduino connected to an H-bridge to control a DC motor. The battery is powering the Arduino this time.
Breadboard drawing of an Arduino connected to an H-bridge to control a DC motor. The switch, motor, and H-bridge are wired as they are in the previous diagram, but the battery is connected to the Arduino's DC power jack pin in this diagram. The H-bridge's supply pin (pin 7) is connected to the Arduino's Vin pin.
Breadboard view of an Arduino connected to an H-bridge to control a DC motor. The battery supplies the Arduino as well this time.

However you choose to power this circuit, make sure the power source is compatible with your motor. For example, don’t use a 9V battery for a 3V motor. The external voltage input from the DC power jack is connected to the Vin pin, so you can use it both to power the Arduino, and to power the motor.

Program the microcontroller

Program the microcontroller to run the motor through the H-bridge. First set up constants for the switch pin, the two H-bridge pins, and the enable pin of the H-bridge. Use one pin 9, one of the pins that can produce a PWM signal using analogWrite(),  for the enable pin.

const int switchPin = 2;    // switch input
const int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
const int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
const int enablePin = 9;    // H-bridge enable pin

In the setup(), set all the pins for the H-bridge as outputs, and the pin for the switch as an input. The set the enable pin high so the H-bridge can turn the motor on.

void setup() {
    // set the switch as an input:
    pinMode(switchPin, INPUT); 

    // set all the other pins you're using as outputs:
    pinMode(motor1Pin, OUTPUT);
    pinMode(motor2Pin, OUTPUT);
    pinMode(enablePin, OUTPUT);

    // set enablePin high so that motor can turn on:
    digitalWrite(enablePin, HIGH);
  }

In the main loop() read the switch. If it’s high, turn the motor one way by taking one H-bridge pin high and the other low. If the switch is low, reverse the direction by reversing the states of the two H-bridge pins.

void loop() {
    // if the switch is high, motor will turn on one direction:
    if (digitalRead(switchPin) == HIGH) {
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
    }
    // if the switch is low, motor will turn in the other direction:
    else {
      digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
    }
  }

Once you’ve seen this code working, try modifying the speed of the motor using the analogWrite() function, as explained in the Analog Lab. Use analogWrite() on the enable pin of the motor, and see what happens as you change the value of the analogWrite().

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 a TB6612 H-bridge controlling a DC motor
Schematic of a TB6612 H-bridge controlling a DC motor
Breadboard view of a TB6612 H-bridge controlling a DC motor
Breadboard view of a TB6612 H-bridge controlling a DC motor

Get creative

This is a suggestion for a possible project. It’s not a requirement for the class homework.

Use your motor to make something move, vibrate, rise, fall, roll, creep, or whatever you can think of, in response to user input from a digital input device (switch, floor sensor, tripwire, etc). Look inside moving toys, you’ll find a number of excellent motors and gears you can re-purpose. See the innards of a cymbal monkey below as an example. Perhaps you can re-design the user interface to a toy, using the microcontroller to mediate between new sensors on the toy and the motors of the toy. Whatever you build, make sure it reacts in some way to human action.

Photo of a toy monkey. The back has been removed to reveal the inner gear mechanism that plays the cymbals. At the center of a mechanism is a DC motor. Wires have been attached to it to run the motor from an H-bridge.
The guts of a Charley Chimp™ cymbal-playing monkey.

 

Originally written on July 1, 2014 by Matt Richardson
Last modified on October 30, 2018 by Tom Igoe