Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 38

H-Bridge Motor Driver Fundamentals

Recommended resources

Hello! Welcome to the first lesson in our new module, "Advanced Motor Control."

In the previous modules, we've built a strong foundation in C++ and mastered the fundamentals of multitasking with FreeRTOS on the ESP32. We learned how to manage tasks, protect shared resources, and signal events efficiently using tools like task notifications.

Now, it's time to apply that knowledge to the physical world. This module is all about making things move. We'll start with the most fundamental component for controlling a brushed DC motor: the H-bridge motor driver. Your background in mechanical engineering will be a great asset here, as we'll be discussing the electrical-to-mechanical interface that brings robotic and automated systems to life.

Today's lesson will focus on understanding the theory behind the H-bridge. We'll cover what it is, why it's necessary, and how it enables both direction and speed control of a DC motor.

1. The Core Problem: Bidirectional Motor Control

Controlling a simple DC motor seems trivial at first. If you connect it to a power source like a battery, it spins. To make it spin in the opposite direction, you simply reverse the polarity by swapping the wires.

But how can a microcontroller, which can only output high or low voltage signals on its GPIO pins, achieve this polarity reversal electronically without any manual intervention?

Let's watch a short video that frames this problem.

What is an H-Bridge?

This clip from CircuitBread clearly explains the basic challenge of controlling a DC motor's direction and introduces the H-bridge as the solution.

Watch the first 43 seconds of the video. It sets the stage for why a dedicated circuit like an H-bridge is necessary.

As the video explains, we need a circuit that can electronically "swap the wires" for us. This is precisely what an H-bridge does.


2. The H-Bridge: A Four-Switch Solution

The H-bridge is an electronic circuit named for its characteristic 'H' shape. It consists of four switches with the motor placed in the center, on the horizontal bar of the 'H'.

H-Bridge Motor Direction Control
This diagram illustrates the two primary modes of an H-bridge. On the left, closing switches SW1 and SW4 allows current to flow from left to right through the motor (M). On the right, closing switches SW2 and SW3 reverses the current flow, causing the motor to spin in the opposite direction.

The principle is simple: by controlling which switches are open and closed, we can control the direction of the current flowing through the motor.

  • Forward Motion: By closing the top-left (SW1) and bottom-right (SW4) switches, we create a path for current to flow from the power supply, through the motor from left to right, and to ground.
  • Reverse Motion: By opening SW1 and SW4 and closing the top-right (SW2) and bottom-left (SW3) switches, we reverse the path. Current now flows through the motor from right to left.

The following video provides an excellent visualization of this fundamental concept using simple mechanical switches before moving on to electronic ones.

How H-Bridge Motor Drivers Work! (Theory, Circuits, PCBs, Buying Guide)

This segment from Robonyx demonstrates the H-bridge concept with a live breadboard circuit, making the current paths very clear.

Watch from 00:49 to 01:41. Pay attention to how closing switches in diagonal pairs changes the direction of the motor's spin.

The DANGER: Shoot-Through

There is one critical rule when operating an H-bridge: never close both switches on the same side (or "leg") of the bridge simultaneously.

For example, closing SW1 and SW3 at the same time would create a direct path from the power supply to ground, bypassing the motor entirely. This is a short circuit, often called shoot-through, which can draw massive amounts of current and instantly destroy the switching components or your power supply.

Test your understanding!

Looking at the "H-Bridge Current Flow" diagram above:

  1. To which terminal is the motor's left lead connected if SW1 is closed and SW3 is open?
  2. What would happen if SW2 and SW4 were closed at the same time?
Show answer
  1. If SW1 is closed, the motor's left lead is connected to the positive power supply (VDD).
  2. Closing SW2 and SW4 simultaneously would create a shoot-through condition. It would short-circuit the power supply, likely damaging the switches and/or the power supply.

3. From Mechanical Switches to MOSFETs

To control an H-bridge with a microcontroller like the ESP32, we replace the mechanical switches with solid-state electronic switches, most commonly MOSFETs (Metal-Oxide-Semiconductor Field-Effect Transistors).

H-Bridge Circuit with MOSFETs
A typical H-bridge schematic using four N-channel MOSFETs (Q1-Q4) as the switching elements. A microcontroller would send signals to the gates of these MOSFETs to turn them on and off.

MOSFETs act as voltage-controlled switches, making them perfect for digital control. However, driving a motor, which is an inductive load, introduces a new challenge: inductive voltage spikes (also known as back-EMF). When you suddenly cut the power to a spinning motor, the collapsing magnetic field in its coils induces a large voltage spike in the opposite direction. This spike can be high enough to destroy the MOSFETs.

To protect against this, H-bridge circuits include flyback diodes (or catch diodes) in parallel with each MOSFET. These diodes provide a safe path for the inductive current to circulate and dissipate when the switches are turned off.

Let's watch a video that covers the transition to MOSFETs and the crucial role of these protection diodes.

How H-Bridge Motor Drivers Work! (Theory, Circuits, PCBs, Buying Guide)

This part of the Robonyx video explains the move to transistors and, importantly, introduces the concept of flyback diodes for protection against inductive spikes.

Watch from 01:41 to 05:08 (you can skip the sponsor segment from 02:40 to 03:40). Focus on: Why transistors (MOSFETs) are used for microcontroller control. The explanation of inductive spikes. How the four diodes provide a path for the current to dissipate safely.

For a more in-depth explanation of why these diodes are so essential due to the non-instantaneous nature of real-world switching, the following article is an excellent resource.

H-Bridges – the Basics: Catch Diodes

This article from Modular Circuits provides a fantastic technical explanation of the role of catch diodes, connecting it to the practical problem of 'shoot-through' avoidance.

Read the section titled 'Catch diodes'. The key insight is that to prevent shoot-through, you must ensure a small delay where both switches on a leg are off. The diodes provide a path for the motor current during this tiny but critical time gap.


4. Modes of Operation

By combining the states of the four switches, we can achieve more than just forward and reverse motion. There are four fundamental modes of operation for an H-bridge:

Input A Input B S1 State S2 State S3 State S4 State Function
1 0 ON OFF ON OFF Reverse
0 1 OFF ON OFF ON Forward
0 0 OFF OFF OFF OFF Coast (Freewheel)
1 1 OFF ON ON OFF Brake

This table assumes a common H-bridge driver logic where two inputs, A and B, control the four switches. Note how the 'Brake' state is achieved by turning on both low-side switches.

  1. Forward/Reverse: As we've discussed, closing diagonal pairs drives the motor.
  2. Coast (Freewheel): If all four switches are open, the motor is disconnected from the power supply. If it was already spinning, it will continue to spin until friction slows it to a stop.
  3. Brake: If we close both of the low-side switches (S3 and S4), we effectively short-circuit the motor's terminals. If the motor is spinning, it acts as a generator, and the resulting current flow creates a braking torque that quickly stops the motor. This is often called "dynamic" or "regenerative" braking.

5. Controlling Speed with PWM

So far, we have only discussed turning the motor on and off and changing its direction. To control its speed, we use Pulse Width Modulation (PWM).

Instead of applying a constant voltage, we apply a series of rapid ON-OFF pulses to the H-bridge switches. The duty cycle of these pulses—the ratio of ON time to the total period of the pulse—determines the average voltage applied to the motor.

  • A high duty cycle (e.g., 90% ON) results in a high average voltage and high speed.
  • A low duty cycle (e.g., 20% ON) results in a low average voltage and low speed.
  • A 0% duty cycle means the motor is off.

This technique allows for very precise and efficient speed control.

What is an H-Bridge?

This final clip from CircuitBread provides a perfect introduction to using PWM with an H-bridge to control motor speed.

Watch from 04:37 to 05:28. This segment connects the H-bridge concept directly to speed control via PWM, which sets the stage for our next lesson.


Conclusion

In this lesson, we've unpacked the theory behind one of the most fundamental circuits in motion control. You now have a solid conceptual understanding of how an H-bridge driver functions.

Key Takeaways:

  • An H-bridge is an electronic circuit that allows a DC motor to be driven in both forward and reverse directions.
  • It uses four switches (typically MOSFETs) arranged in an 'H' configuration.
  • Closing diagonal pairs of switches controls the direction of current flow through the motor.
  • Shoot-through is a dangerous short-circuit condition that occurs when both switches on the same side of the bridge are closed simultaneously.
  • Flyback diodes are essential for protecting the switching elements from voltage spikes generated by the motor's inductance.
  • An H-bridge supports four main modes: Forward, Reverse, Coast, and Brake.
  • Motor speed is controlled by applying a PWM signal to the H-bridge inputs to vary the average voltage across the motor.

Now that we've covered the "what" and "why," our next lesson will focus on the "how." We will take what we've learned today and write C++ code to drive an H-bridge with an ESP32, using its GPIO and LEDC (LED Control) peripheral to generate the necessary control and PWM signals.