Projects

Bridging The Gaps

H-Bridges: For Motors, and More!

Sophie Parker

Issue 12, June 2018

This article includes additional downloadable resources.
Please log in to access.

Log in

They're simple to use, and provide powerful control!

As many people who have experimented with DC motors will know, the H-bridge is a simple circuit pattern that allows changing the polarity of its outputs. It achieves this by switching the outputs to either V+ and Ground, depending on the state of the inputs. This allows it to make current flow in either direction across the outputs, bridge them, or disconnect them.

OPERATING PRINCIPLE

An H-bridge requires a minimum of four switching elements – one to V+ and one to Ground for each motor terminal, in a characteristic “H” pattern, hence its name.

There are two main operating states to an H-bridge: these are the two possible orientations of V+ and Ground on the outputs. On a solenoid, these correspond to push and pull forces.

However when used with a DC motor, these switched elements control clockwise and counterclockwise rotation – although exactly which is which will depend on the motor.

The other states of the H-bridge are as follows:

  • Disconnected, where one or both sides are connected to neither V+ nor Ground, leaving at least one terminal “floating” (this equates to coasting in a DC motor).
  • Common polarity, where both sides are connected to the same terminal, which results in a DC motor braking. In simple terms, having both terminals of a motor shorted, results in it absorbing the energy of the magnetic field, and dissipating it as heat.
  • Short circuit. Fairly logically, if you close both switches on one side, that’s a short straight to ground. Kaboom!

H-bridges need not be constructed with physical switches. In the past, they were constructed with large relays and auto-transformers, to control industrial motors. The rise of semiconductors has allowed solid-state H-bridges to be produced, taking up significantly less space and having the higher reliability afforded by a lack of moving parts.

GOIN’ SLOW

One element that is missing from the basic H-bridge circuit, is a way to dim the power applied to the load, which in real applications, is usually a necessity! For this reason, real-life H-bridge circuits (available as either drivers requiring external power transistors, or all-in-one chips) have pins that enable and disable the H-bridge. This may sound like a bit of a gimmick – but what if we were to, say, switch it off and on very quickly?

Voilà, pulse width modulation (or PWM)!

You’ve probably seen PWM used before, as it's a fairly common feature in our modern electronics. It’s a simple way that microcontrollers emulate analogue signals, by proportionally varying a square wave’s “pulses”. Usually, it’s used in a situation where a near-approximation to analogue is desired, but in this application, analogue simply wouldn’t work because a digital signal is necessary for the enable pin.

By making use of PWMs secretly-digital nature, this approach is simpler and results in greater efficiency, compared to using a regulator circuit to vary the supply voltage. And it doesn’t have any significant effect on the motor’s output, due to the inertia of the physical rotor and magnetic fields.

The L293D chip from Texas Instruments, and its somewhat cheaper sibling the SN754410, are simple to use, widely available quad-half-H-bridge chips, which are rated for 1A per channel with no additional cooling. This means they could drive one motor at 2A, two motors at 1A each, or 4 separate 1A outputs (we’ll get to that a bit later).

fig 4

Note: You’ll notice the term “half-H-bridge” in datasheets and product titles. This means one channel, which can be connected to either V+ or Ground (two switches, from the diagrams above). The distinction may seem unnecessary, but as you’ll see later on, there are applications where they are not part of a whole H-bridge. For now, just rest assured two halves count as one whole.

This may look a bit confusing, so let’s look at an example. Bring in the Arduino!

Let’s start with our inputs. We want to run a motor, so we’ll need two half-H-bridges, or channels. Correspondingly there are three input pins: 1&2ENABLE, 1IN, and 2IN.

1&2ENABLE is a combined enable pin for channels 1 and 2. In the application of our motor, we can use it for the PWM speed control. 1IN and 2IN don’t seem so obvious in their function, so think of them as selecting which supply pin the channel is connected to. Logical high (5V/VLOGIC) means the output will be connected to V+ (note: the motor supply voltage is separate from the logic supply voltage). For clarity, we’ll refer to them as V+ and VLOGIC, but they could be labelled as various different things. Read that datasheet!. Logical low (GND) means the output will be connected to Ground.

Here is a table showing what will happen for different values of these pins, if a motor is attached to their output pins

H-BRIDGE PIN TRUTH TABLE:

1&2ENABLE 1IN 2IN MOTOR ACTION
1 1 0 Rotate Clockwise*
1 0 1 Rotate Counterclockwise*
1 0 0 Brake
1 1 1 Brake
0 ? ? Coast - No Connection

*These directions depend on the design and orientation of the motor, relative to the output pins.

One thing to note here, is the difference between bringing the IN pins low, versus the ENABLE pin. When an IN is low, the corresponding output is connected to Ground. When ENABLE is low, the output is “high impedance” – basically, not connected to anything.

Okay, let’s start prototyping!

Build 1: Prototyping with a H-Bridge

Here is a simple circuit to connect one DC motor to the SN754410, and control it using an Arduino Uno. You can also use a L293D which uses the same pinout, and has very similar specifications. But check the datasheet of what you're using to be sure!

For this demonstration we're only using one side of the IC. Both "sides" work in precisely the same manner, so it allows us to test things with minimal hardware.

Parts Required:JaycarAltronics
1 × Arduino UNOXC4410Z6280
1 × SN754410 or L293DZK8880Z2900
1 × 12VDC MotorYM2716-

Note: You'll also need a breadboard and jumper wires.

We have provided both a schematic and a fritzing diagram for wiring. The H-Bridge chips are relatively robust, but all ICs should be treated with care. Be sure to orient the chip correctly.

The UNO and the H-Bridge are both powered from 12V. However you'll notice we wire 5V from the UNO back to the H-Bridge to Vlogic and the enable pins. This ensures that the H-Bridge responds to our logic level, which is 5V on the UNO.

If you've checked your wiring, you can move on to the code.

The CODE

We have provided a sample sketch in the Digital Resources for this build. Load up "h-bridge_motor_demo.ino" into your Arduino IDE, and compile it onto your UNO.

We have deliberately made this demo sketch with separated functions to make it clear what does what. It's very easy to follow, so we don't go through it in major detail here. But essentially we're cycling through different PWM values, and logging those actions to the serial monitor so you can see what's happening.

With the magic of an H-bridge and PWM, we can choose the direction a DC motor turns, as well as its speed.

You may notice you can hear a hum from the motor, especially when it isn’t quite moving, but there is some power being applied. This is because the Arduino PWM pins on most boards, operate at either 490 or 980Hz, which is well within human hearing.

Small DC motors have three coils of wire between two permanent magnets, similar to what you would find within an audio speaker – do you see where this is going? The motor itself acts in a similar way to a rudimentary speaker, at the frequency of the PWM driving it, and that is the sound you can hear.

To remove this hum, you can change the PWM frequency, but this is a little complicated, and has some caveats. Check out https://playground.arduino.cc/Code/PwmFrequency for more info.

Well done! You can now control motors with Arduino This is all a bit academic though, isn’t it? Let’s make something a bit more fun. Let's turn this demo circuit into two motors controlled by a joystick!

fig 7

Build 2: Dual Motor Joystick Control

There are two parts to this build. We're adding another motor, but more interestingly, we're adding speed control using a joystick module too!

ADDITIONAL Parts Required: Jaycar Altronics
1 × Analogue Joystick Module XC4422 Z6363
1 × Additional 12VDC Motor YM2716 -

Note: You'll also still need a breadboard and jumper wires. Parts required are additional to the list in Build 1.

Firstly, add another motor wired to 3OUT and 4OUT on the SN754410, and connect 3&4ENABLE to the Arduino’s pin 9, 3IN to 10, and 4IN to pin 11. This is literally just using the "other side" of the driver IC.

Next, grab your joystick module, and connect the GND and VCC wires. Connect the X output to A0, and Y to A1 on the UNO. When that’s done, upload the joystick control sketch; H-bridge_motor_joystick_control.ino.

With no additional configuration, you should now have control over the motors when you actuate the joystick in one direction or another.

In this example, each axis of the joystick module controls one motor forward or backwards, but not together. The code block below shows the approximate logic between the read of the joystick axis, and the output to the motor (using our internal setSpeed function).

bool directionX = joystickX > 0;
setDirection(Motor1, directionX);
int cappedPosX = min(abs(joystickX), joystickDelta);  // Cap it to 0-450.
if (cappedPosX <= joystickJitter) cappedPosX = 0;  // Make (v <= 30) = 0 to remove jitter.
setSpeed(Motor1, map(cappedPosX, 0, joystickDelta, 0, 255));   // Map to 8 bit speed and set.

There is a detailed explanation of the process within the sketch itself which you can read in further detail.

fig 7 schematic

The analogue joystick is also a versatile module. If you wanted to modify it so “up” makes both motors go forwards, it’s possible to either orient the joystick 45 degrees off vertical (such that “up” is positive on X and Y), or modify the code so the Y axis value is added to one motor, and subtracted from the other.

This demo build is a starting point from which you could make a remote controlled rover, a robot arm, or bring life to a motorised children’s toy – to name just a few ideas! Check out our bonus build for a fun practical use of this setup. But first we want to show you something else really awesome about the h-bridge. We have also created a 3D printable hand-control caddy to make the joystick more comfortable to use, should you wish to.

Build 3: RGB LED Controller

demo 2

It’s important to note that H-bridges aren’t solely restricted to DC motors for their use. Each half-H-bridge “channel” can operate independently. The thing to note though, is that when using channels independently, you should tie the ENABLE pins to 5V, and apply PWM signal to the IN pins instead.

Here’s a simple demo using H-bridge as a cheap and simple driver for a 12V RGB LED strip There’s no need to use a full H-bridge for an LED load, as power only ever needs to flow in one direction. This means we can use the channels individually, bringing the count per chip from two to four. In this case, three is all we’ll use, for the three channels of red, green and blue.

It's worth noting that it doesn't actually matter which three of the four H-bridge channels you use. We are using 1, 2, and 4. However it's really your choice.

ADDITIONAL Parts Required:JaycarAltronics
1 × 12V RGB LED Strip (up to 0.5m)SL3942X3213A
1 × SN754410 or L293DZK8880Z2900
1 × 12VDC MotorYM2716-

Note: You'll also still need a breadboard and jumper wires. Parts required are additional to the list in Build 1.

fig 8 schematic

The sketch named H-bridge_LED_strip_demo.ino is provided in the digital resources. Load it onto your UNO via the Arduino IDE, and you should see things spring to life.

The code will cycle through a series of colours, showing you the colour range of the LED strip. There is no user-input for this code, however there is useful output provided to the Serial Monitor for you to watch.

While such a circuit would work just fine with three discrete MOSFETs or power transistors, sometimes H-bridge chips are handy, as they require less extra wiring.

An additional benefit is that H-bridges are inherently symmetrical switches. Transistors, including MOSFETs, come in N-type and P-type varieties. The correct type must be chosen per use, depending on whether the “low” or “high” voltage side of the circuit is being switched – not so for H-bridges. This means the same chip can be used for both common-anode and common-cathode LED strips; for example, just by inverting the PWM value in software.

The main consideration between a H-bridge and discrete power transistors etc, is really current handling. MOSFET and power transistors are easily obtained to handle 5, 10, even 30 amps. The current handling of a H-bridge driver may only be 0.5A, 1A, or somewhere in the middle (check your ICs datasheet to be sure) when presented in a DIP16 IC package like we're using here.

This reveals one small caveat to the fantastic H-bridge. Of course, there are ways to overcome this, such as using transistors on the outputs, but then the question should be asked - is a H-bridge the right solution. The answer may lie somewhere between.

However there are many modules available for higher current H-bridge, which we have used in other projects too. Back-EMF protection and other protection are often then included.

fig 8

THE BIG WHY?

You may be wondering, why we need to use the H-bridge at all, when we're simply providing PWM signals to the RGB LED strip.

In this case, we're basically making the H-bridge a logic level converter. Since this is 12V LED strip, the PWM inputs are expected to be 12V also. Depending on the make of LED strip, it may not work at all from 5V logic. You can use this same setup on 5V strip, simply supply 5V instead of 12.

The overall advantage of 12V LED strip is lower current requirements, which is a huge benefit in longer runs.

YOUR MISSION, SHOULD YOU CHOOSE TO ACCEPT IT

It's very possible to integrate the two sketches we've just provided (joystick motor control, and RGB LED control), to provide Joystick-enabled colour selection of RGB LED strip. That would be awesome!

Here's something you can challenge yourself with. Develop some code to use the Joystick positioning to set RGB values.

What we didn't cover with the Joystick controller was the click-button which is integrated into the joystick itself. We haven't connected it, but you could experiment with mode-selection to, transition speed, and all sorts of other creative adjustments.

Bonus Build: Remote Control Two Wheel Car

All that theory can be good, but building something fun is GREAT!

Here, we'll put our two-motor and joystick idea into a practical use, using a two-wheel chassis. These two-wheel chassis' are a simple and effective way to get some movement! The end result is a simple remote controlled vehicle that could be the basis for all sorts of ideas.

Sure, it's tethered by a cable, and that might be considered a bit old-school, but the point here is to get things moving with a h-bridge, not to focus on Bluetooth or RF wireless. We'll circle back to that another time.

Scaled-up, this is essentially the same fundamental system that can make a large robotic carrier, driven by a human. An electric wheelbarrow or palette lifter, or a similar idea.

Just because it's small and simple however, doesn't mean it's not alot of fun! Driving it around the DIYODE offices, we had this little unit doing wheelstands in no time.

ADDITIONAL Parts Required:JaycarAltronics
1 × Two Wheel ChassisKR3160K1090
1 × 8 x AA Battery HolderPH9209S5034
1 × 8 x AA Alkaline BatteriesSB2425S4916A
1 x 2m Length Cat5 CableWB2022W2759

Note: You'll also still need a breadboard and jumper wires. Parts required are additional to the list in Build 1.

There is no change to the sketch from the demo sketch provided (H-bridge_motor_joystick_control.ino).

The two-wheeled chassis is a great platform to build on. While the kits are readily available from various places, they all tend to look and feel fairly similar. One thing is consistent however, the holes in the acrylic panels never really line up to anything.

Double sided tape or a Blu-tac type product is really the simplest method for mounting all your hardware to the chassis to stop it moving about. This is designed as a proof of concept more than a finished build, so we haven't built a specific mount.

We're also using a length of Cat5 cable between the car and the joystick, to allow you to walk behind it. You can use any copper cable with 4 or more cores really, but Cat5 is very cheap and available everywhere, so it's an easy choice. The joystick has only signal level current, so we don't need anything huge.

3D PRINTED CONTROLLER MOUNT

You probably recall we noted about rotating the joystick 45° in order to control both motors at once. We have therefore created a 3D printed mount to make hand-holding the joystick easier. It positions it in the hand well for comfortable use.

WHERE TO FROM HERE?

You no-doubt now have a thorough understanding of what a H-bridge is, and what it does.

With the practical examples provided, you probably have some great ideas for applications using H-bridge now too!

fig 9