Download PDF Parts List View on YouTube Download Code

Today we will look at several H-Bridge motor drivers that you can use with several different motors. We’ll see how to select them, how to connect them to an Arduino, and how to code for them.

Introduction

Driving DC motors is a pretty common application for microcontroller-based designs, and the choice of a motor driver can be a pretty important one. 

 

If your only requirement is to turn a motor on and off without regard for controlling speed or direction, then a relay or MOSFET would do the trick.  But in most applications, you’ll want to be able to adjust your motor parameters, and for that, you’ll require a motor driver.

We’ll examine seven different motor drivers today for controlling motors of all different sizes.  Some of these drivers are dual, in other words, they drive a pair of motors, while others are just for single motors.

All of these drivers can be driven by any microcontroller and probably most microcomputers. One of the drivers we will look at also has a built-in controller, so you might not even need a microcontroller with that one.

I’ll be using an Arduino Uno for these demonstrations, but the code can be easily ported to any other microcontroller

Selecting DC Motor Drivers

The selection of a DC motor driver can sometimes feel overwhelming, as there are many models to choose from.

Many Arduino designs employ the old faithful L298N dual H-Bridge module. However, this isn’t necessarily the best solution, in fact, it seldom is, as a much more efficient driver can replace it. It also is inadequate for large or very small motors.

But it really shouldn’t be that hard to pick out a motor driver for your project, all you need to do is match the driver to your motor’s specifications.

DC Motor Specifications

If you’re lucky enough to have a data sheet for your DC motor, you’ll notice that these devices have a myriad of specifications.  We can break up these specs into two broad categories:

  • Mechanical
  • Electrical

The mechanical specifications include the motor size, mounting methods, maximum speed, and torque.  You’ll select a motor with specifications to suit your project, but this selection doesn’t directly impact your motor driver selections.

The electrical specifications are what you use to select a suitable DC motor driver. Just as with mechanical specs, there are several electrical specifications to go through, but a few basic ones are all you really need:

Working Voltage

This is the suggested voltage for running the motor, and it is the voltage at which all other specifications are measured. Note that I called it the “suggested” voltage, as DC motors actually consume current, not voltage.

You can usually run a DC motor at half the Working Voltage, or even less. You shouldn’t exceed it by more than 30%, as this could cause the motor to consume too much current for its coils to handle.

Current

This is arguably the most important electrical specification, at least regarding choosing a motor driver. 

The average current is the amount of current the motor will consume when running under a normal load.

Ideally, you’ll select a motor driver with a current rating above the motors’ average current, a 30 – 50% headroom is perfect.

Stall Current

The stall current is the amount of current the motor will draw if its shaft is seized. 

When selecting a motor driver, this will correspond to the peak current. Ideally, the driver will be able to handle this current for at least a minute.

H-Bridge Motor Drivers

Reversing a DC motor is simply a matter of reversing the polarity of the voltage applied to it. The most common method of doing this is to use a design called an “H-Bridge”.

The operation of an H-Bridge can be illustrated using four switches in the following configuration:

Note that the motor sits in the “bridge” section of the H-Bridge. If all the switches are open, as they are in the above illustration, then the motor will receive no voltage and will remain stationary.

If we close two of the switches, then DC current is allowed to flow through the motor. Note the polarity of the current, connecting the motor this way causes it to rotate clockwise.

If we now open those switches and close the other two, the motor will receive DC voltage in the opposite polarity, causing it to spin counterclockwise.

Naturally, you need to be careful not to close the wrong two switches and short out the power supply!

In the real world, we seldom use switches for an H-Bridge, although you could employ a DPDT switch or relay to do all the switching.  Instead, we use transistors.

Older H-Bridge designs employ bipolar transistors. While this makes for a simple and cost-effective design, it also has a drawback that makes it rather inefficient.

The problem is that bipolar transistors have a voltage drop between the emitter and collector when they are switched on. This drop is usually 0.7 volts for a bipolar power transistor.  Since we are using TWO switches simultaneously, the total drop is about 1.4 volts.

This means that if you apply 12 volts to the H-Bridge, the motor will actually receive only 10.6 volts. That “missing” voltage is converted to heat in the power transistors, which is why bipolar designs usually require heatsinks.

Newer designs use MOSFETs for improved performance. The voltage drop across a MOSFET drain and source is very tiny, generally 0.1 volts or less, so this is a much more efficient design. In addition, MOSFET designs can often be made smaller, and many don’t require a heatsink.

Speed Control with PWM

We have seen how to control the direction of a DC motor, but how do we adjust the motor speed?

When the rated voltage is applied to the motor, it will spin at its maximum speed.  It might seem that just reducing that voltage will reduce the speed, but this isn’t really a good motor control method. While the speed will indeed drop, the torque will also fall proportionally.

Instead, a better method is to use Pulse Width Modulation or PWM.

As the name would imply, a PWM system varies the width of a train of pulses driving the motor. The wider the pulse width, the faster the motor will spin.

By sending energy pulses to the motor, we retain its torque while controlling the speed. It works because we are changing the average amount of power to the motor, not its voltage.

Microcontrollers are great for precision timing and thus make excellent PWM controllers. Many microcontrollers have a limited number of PWM-capable pins, there are six, for example, on the Arduino Uno.

Controlling Speed and Direction with a Microcontroller

So we know that we can control a motor’s speed with PWM, and we can flip a few transistors on to control the motor direction. How do we interface all of this to our microcontroller?

The seven motor drivers we will be working with today can be divided into three types of interfaces:

PWM + 2 Control

The PWM + 2 Control method uses an input for PWM plus two inputs to control direction.

Looking at the truth table, we can see that the state of the two control pins determines what direction the motor shaft spins. I can also stop the motor or apply a “brake”.

The “brake” function applies current to hold the motor shaft in place. It can be useful for non-geared designs, but you should use caution as many motors can only withstand a few minutes of braking before they

PWM + 1 Control

This is probably the simplest scheme possible. 

In this arrangement, you have two inputs to the motor driver. One is the PWM input, which takes PWM pulses to control the motor speed. The other is the DIRECTION or DIR input, a logic signal that controls direction.

Changing direction is as simple as changing the state of the DIR input.

2 PWM

With this arrangement, you’ll need two I/O pins that are both PWM-capable.

Motor drivers that use the 2 PWM control method have two inputs, often called IN1 and IN2. Applying a PWM signal to IN1 while holding IN2 LOW will spin the motor in one direction. Holding IN1 LOW and applying PWM to IN2 will cause it to rotate in the opposite direction.

This is the most common method of speed and direction control that we will see today, as four out of the seven modules employ it.

Small & Mid-sized Motor Drivers

We will start our motor driver adventure by looking at some smaller modules intended for small to mid-sized motors.

These are common with experimenters, as these motors are inexpensive and easily obtainable. 

Motors in this range are used in toys and small appliances.  We use them here in the workshop in those small “robot car” projects.

L298N Dual H-Bridge

The L298N is arguably the most popular H-Bridge driver around, at least with marks. It’s available just about everywhere (Amazon, eBay, your local electronics store), and it’s pretty easy to use.

This is an older design based on bipolar transistors, and because it is produced in such a high volume, it’s very affordable. In fact, it is often sold in 5 or 10-packs.

This is a dual-channel driver module with the following specifications:

Looking at the module, you’ll note some screw terminals, DuPont connectors, and a few places for jumpers.

There are two sets of terminals for the motor outputs, plus another 3-terminal block for ground, motor voltage, and 5-volt logic voltage.

If you don’t want to supply the 5-volts, the L298N module can be strapped to use an internal linear regulator to supply its own logic power. If you do, then the motor voltage needs to be a minimum of 7.5-volts.

The ENA and ENB lines are Enable lines, which can be toggled by PWM to control the motor speed. If you just want to use the module to control the motor direction, you can strap ENA and ENB HIGH.

IN1 through IN4 control motor direction, as shown in the truth table.

L298N Arduino Hookup

You can hook up an L298N to an Arduino (or any other microcontroller) in several different ways, the only restriction is the ENA and ENB need to be attached to PWM-enabled pins.

Here is how I hooked mine up:

Click on the wiring diagram to open a larger image

Note that I am supplying 5 volts from the Arduino for the L298N logic circuitry. You can eliminate this connection by installing the strap on the module to use the internal voltage regulator. Just remember that to do this, your motor supply voltage will need to be at least 7.5 volts.

L298N Arduino Sketch

If there are a hundred ways to hook up an L298N, then there are probably about a thousand ways to code for it!

The following is a simple sketch that rotates the motors in different directions. For each direction, it increments and decrements the speed.

Note that the direction is changed by changing the state of the IN1, IN2, IN3 and IN4 pins using the digitalWrite function. The speed is controlled using the ENA and ENB lines using analogWrite.

Load the sketch and observe your motors. I drove a pair of those yellow “robot car” motors, and it worked well.

You will probably hear a “whining” sound from the motors at low speeds, this is common with PWM. It can be eliminated by changing the PWM frequency, which is beyond the scope of this article.

TB6612FNG Dual H-Bridge

The TB6612FNG is a MOSFET-based dual-channel H-Bridge that can often be used as a pin-for-pin replacement for the L298N.

This device has the following specifications:

The TB6612FNG is a tiny module, especially when compared to an L298N. It is breadboard-friendly and does not require a heatsink. Its MOSFET design makes it much more efficient than the L298N.

You’ll note that the truth table is the same as the L298N, and it also implements a Brake function. This should be used sparingly as it can cause some motors to overheat.

The module has an EN (Enable) input that must be brought up to the logic voltage; otherwise, the module will be disabled and will not work.  You can tie this to VCC or control it using an I/O pin.

TB6612FNG Arduino Hookup

I hooked up my TB6612FNG using the same pins I used for my L298, illustrating how this module can just be substituted for its older cousin.

Click on the wiring diagram to open a larger image

Note that I have connected 5 volts to the EN line on the motor driver to enable it. Don’t forget that connection!

TB6612FNG Arduino Sketch

Since this replaces the L298N, we can use the same sketch to drive the module. 

You can also use one of the many libraries available for the L298N, as any L298N code will work on this module.

The L298N Library by Andrea Lombardo is a well-documented library that you can install directly from your Arduino IDE.  It has several functions to make working with the L298N easier, and can also be used with the TB6612FNG.

 

DRV8871 Single H-Bridge

The DRV8871 is a single-channel H-Bridge. This MOSFET-based design has a very wide motor voltage range and pretty impressive current capabilities considering its size.

The module I used was from Adafruit. It has screw terminals on one side for power and motor hookup. On the other side are four pins, one for ground and one which is a repeat of the motor voltage input.

The other two pins, IN1 and IN2, are how the speed and direction are controlled. The design uses the “2 PWM” input scheme where one input is held LOW while the other receives PWM.

Note that there is no logic voltage connection, the module has an onboard voltage regulator, and it derives its logic voltage from the motor voltage. The minimum motor voltage is 6.5 volts.

DRV8871 Arduino Hookup

Hookup of the DRV8871 to an Arduino is quite simple, as only three wires are required to connect the Arduino to the driver.

Click on the wiring diagram to open a larger image

Note that both I/O pins must be PWM-capable.

DRV8871 Arduino Sketch

Here is a pretty basic sketch that can be used to exercise a motor connected to the DRV8871.

This sketch operates a lot like the L298N sketch, it sets the motor direction, then accelerates and decelerates the motor. It then reverses the direction and does it again.

Load the sketch and observe the motor. This is a very responsive motor driver that is quite easy to use, and its small size makes it ideal for hobbyist projects.

MX1508 Dual H-Bridge

If you want to drive very small motors, you certainly don’t want to use an L298N. Not only does it have a minimum motor voltage of 5 volts, but it also is physically huge!

The MX1508 dual H-Bridge is an ideal choice for tiny motors. This device will work with motors that require just 2 volts.

The module itself is a bit odd. It’s very tiny and has connections for the two motors, the inputs (2 per motor), and motor power. There is no input for logic voltage, as it is derived from the motor power.

The board is not solderless breadboard friendly, however, as its pins don’t align with the 0.1-inch grid.  The negative battery connection is also the only ground pin available, and the board has only one mounting screw.

Despite its idiosyncrasies, this is a great board for tiny motors, and it’s very inexpensive.

MX1508 Arduino Hookup

Here is how I hooked up an MX1508 to an Arduino.

Click on the wiring diagram to open a larger image

Note that all four of the input pins must use PWM-capable pins.

In my experiment, I used a 3-volt motor and powered it with two AA batteries. No logic level converter is required, despite the 3-volt supply and the 5-volt logic!

MX1508 Arduino Sketch

The MX1508 is controlled in the same fashion as the DRV8871, and you could simply use a sketch that duplicated the DRV8871 code and drove two channels.

Another way is to use a library, and there is an Arduino library made for the MX1508 that you can install from your Library Manager. Just filter by “MX1508” and choose the MX1508 Library by Cheng Saetern.

The library comes with two examples. I ran the MX1508Sample one, which just manipulates one of the motors.

I modified mine to use both motors, and it drove my 3-volt motors just fine – I’ll include a copy of the modified one in the code downloads in the Resources section below.

Large Motor Drivers

The motor drivers we have looked at so far are great choices for small or mid-sized motors, such as the ones we use in small “robot car” kits.

To drive larger motors, we will require drivers with a larger current capability.  We’ll look at a couple of them right now.

DBH-12 Dual H-Bridge

The DBH-12 is a dual H-Bridge driver that is pretty popular on Amazon and eBay, and is well priced. It’s a larger module with a large heatsink.

It has impressive current capabilities, although it only supports motors with a maximum of 14.5 volts. This driver is part of a series of drivers, some of which support higher voltages.

The module has a 6-screw terminal on one side for connecting the two motors and motor power supply.

On the other side of the circuit board is a dual-row Dupont connector, which is for the input to both motors. An output is also provided for use as a current monitor.

This module has a minimal amount of documentation, some of which is inaccurate. One of the most serious inaccuracies is that a few diagrams show the V+ pin as a logic voltage input. This is false, it is a motor voltage output. Confusing the two can cost you a microcontroller!

The module uses the same “2 PWM” technique for regulating motor speed and direction, and there are inputs for both motor channels. The Enable line is internally pulled up, if you ground it, you will disable the module. Note that it is pulled up to the motor voltage, so be careful if you want to control it with a microcontroller. In most applications, you can just ignore this pin.

One caution when using this module is to not exceed 98% speed for long periods. If you are using 8-bit PWM as we do with the Arduino Uno, this means keeping the maximum PWM value to 250.

DBH-12 Arduino Hookup

Here is how I hooked up the DBH-12 to an Arduino:

Click on the wiring diagram to open a larger image

Note that if you want to change I/O pins, you’ll need to restrict yourself to PWM-capable ones.  Aside from the I/O connections, the only other connection to the Arduino is the ground.

DBH-12 Arduino Sketch

The sketch we will use to control the DBH-12 is essentially the same sketch we used for the  DRV8871 single-channel driver. As this is a dual-channel device, we just double up on everything.

I’ve added a “fixed speed” value to the sketch, so we can also spin the motors in opposite directions at a fixed speed.  Don’t exceed 250 for this value.

Load the sketch and run it.

In my case, I used a couple of high-torque, high-speed motors, and the DBH-12 powered them quite nicely.

IBT-2 (BTS7960) Single H-Bridge

The IBT-2 is a single-channel H-Bridge with a large voltage and current capability.  As with the DBH-12, it sports a large heatsink.  This module is also known as the BTS7960.

This is another commonly available device that lacks somewhat in documentation, but it’s pretty easy to use, and there is a spec sheet available.

Once again, we are using the “2 PWM” speed control method. In addition to the control inputs, there is also an Enable line that must be pulled high. As with the DHT-12, this driver also has an output for current measurement.

The driver requires both motor and logic power. Motor power and motor outputs are brought out to a 4-screw terminal strip.

IBT-2 (BTS7960) Arduino Hookup

Hookup of the IBT-2 to a microcontroller is very simple, both I/O pins need t be capable of PWM:

Click on the wiring diagram to open a larger image

The Arduino’s 5-volt output is connected to both the VCC and Enable pins on the IBT-2.

IBT-2 (BTS7960) Arduino Sketch

We can use the same sketch we used for the DRV8871 with this module, as the driver requirements are identical. Here it is, with the variables renamed and a few timing constants changed, but otherwise the same:

Load the sketch to the module and power everything up.

I used mine with a 12-volt automotive motor, and it worked nicely.

Cytron MD25HV Single H-Bridge & Controller

The final module we will look at today is the most powerful one. It’s also a bit different than the rest.

The Cytron MD25HV is a single-channel H-Bridge that is the largest module we will be examining, and as its specs show, it can accommodate some pretty large motors. 

But what sets this driver apart from the others is that it is not just a driver, it also has a built-in motor controller. The device is shipped with a potentiometer and a 3-position switch that can be plugged in to control motor speed and direction.

So you might not even require a microcontroller if all you want to do is control a motor’s speed and direction manually.

This driver uses the simplest control method – one PWM input and one DIR input, which can be toggled LOW or HIGH to change direction.

The board also gas a couple of test switches, one that spins the motor forward and another in reverse. This is useful for testing your wiring.

Connections to the motor and power supply are on one side, and screw terminals are also provided for PWM, DIR, and Ground connections.

There is also a 5-volt output that can be used to power a microcontroller.

MD25HV Arduino Hookup

Hooking up an MD25HV motor driver to an Arduino is pretty simple, as only two control lines are required. Obviously, you’ll need to use a PWM-capable I/O pin for the PWM input, the DIR can use any I/O pin.

Click on the wiring diagram to open a larger image

MD25HV Arduino Sketch – Cytron Motor Driver Library

With the simple control method used by the MD25HV, writing a sketch for it is a very easy task.

Rather than doing that, I decided to look for a library, and my search was rewarded with the discovery of Cytron’s Motor Driver Library.

This library is for every Cytron motor driver, and as it turns out, it will also support every motor driver reviewed in this article! It really simplifies coding and allows you to write code that any motor driver can use.

I chose the example for PWM and DIR pin models. The library simplifies the coding by using one method, setSpeed. You supply a speed value as follows:

  • 1 to 255 moves forward
  • -1 to -255 moves backward
  • 0 stops the motor

The example sketch just moves the motor back and forth.

I loaded it to my Arduino and attached a large (14-amp) motor to the MD25HV. It worked great, both as a driver and as a controller.

Conclusion

Microcontrollers are a great way to automate motorized devices. With a wide range of motor drivers, you can use a microcontroller like an Arduino to drive virtually any size of DC motor.

The Cytron Motor Driver Library is a great find, as you can use it with any motor, adding flexibility to your code. 

Even if you choose to write the code without the library, you should have no problem, as using all these drivers is quite simple.

So get out there and make something move!

 

Parts List

Here are some components that you might need to complete the experiments in this article. Please note that some of these links may be affiliate links, and the DroneBot Workshop may receive a commission on your purchases. This does not increase the cost to you and is a method of supporting this ad-free website.

COMING SOON!

 

Resources

Code used here – All the code I used in this article, in one handy-dandy ZIP file!

MX1508 Library – MX1508 Library by Cheng Saetern

L298N Library – L298N Library by Andrea Lombardo

Cytron Motor Driver Library – Cytron motor driver library, which can be used with ANY motor driver.

 

Driving DC Motors with Microcontrollers
Summary
Driving DC Motors with Microcontrollers
Article Name
Driving DC Motors with Microcontrollers
Description
Learn to use several DC motor driver modules with an Arduino Uno. Covers H-Bridge theory and operation and has several code samples for drivers and DC motors of all sizes.
Author
Publisher Name
DroneBot Workshop
Publisher Logo
Tagged on:
Subscribe
Notify of

15 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Steven WKWong
1 year ago

Any of the module above have R/C function?

1 year ago

This was yet another well put together tutorial that imparts full knowledge in a few moments. Really loved it. Stay well and thank you Sir.

Moath
1 year ago

So thanks.
Can make vidios to explain simply the differences between AI, ML and DL.
We hope also to learn some about IOT simply tools, platforms and environments.
In other word what we need to work with AI and IOT.

Paul Block
1 year ago

I would really appreciate it if you could provide a pdf of your articles. Right now I have to copy edit and paste. I don’t like to read online so that would be great.

Ian Greaves
1 year ago

Thanks so much for this tutorial, I’ve burnt out a couple of nano’s 12v from the “logic” pins – now I understand!!! Will be able to use the DBH-12 now to run my adapted mobility scooter motors…..

Lookr1
1 year ago
Reply to  Ian Greaves

I have a electric wheel chair with a blown controller, I’m encouraged I could use 2 IBT-2 42 Amp motor drivers Or the Cytron and a joy stick. I am very grateful for these lessons. God Bless.

Bruce Gennette
1 year ago

Could you add an example of dual motor control from the 2 x half-bridges in 1 module? This is often very much cheaper than using 2 separate Fet modules and reduces inventory. There are many applications where the motors always turn the same direction (eg multiple cooling fans) so a single h-bridge module can be used to run 2 of them. This is usually setup with positive voltage through the motors going IN to the 2 bridge out connections which are controlled ‘in reverse’ with both half bridges setup as low side drivers by permanently connecting their Direction pins to… Read more »

nishu
1 year ago

sir 1 question we use 3 motor driver md20a
we want to control by rc receiver 
we know that used microcontoller but how sir plzz help

1 year ago

I’ve been watching the channel for years now. This will be the first time I have participated, but I need the assistance. I’m working on an automatic gate opener that is remote controlled by a fob. It was a Mighty Mule (mm371w) until the control board died. It started dying by not working with our key fobs, then progressed to full mortality. When the remotes quit, I bought a momentary remote relay with two fobs and wired it in, and it operated the gate just fine…. for a while, then gate’s main board quit with no visible damage. Everything on… Read more »

Ernesto Salvador
1 year ago

Are all topics available in e-book? And how is the price of this e-book?

Angga Amalul Ahli
1 year ago

To increase the torque of the dc motor range 12-48 but have a slow and stable rotation, besides using a gearbox, what do you use, sir

Robert
1 year ago

I’m working on the Arduino 1 program to code this/ I wrote down all this code and it says I have multiple errors. What did I do wrong, and what program did you put this code into?

Ziad Fawzi
1 year ago

Thank you so much.

Gerald Dinkins
10 months ago

I really appreciate your videos. I am in my early eighties, and I am in need of direction for a project. The project: Build a 1/12 scale tank with a blower mounted in place of the turret. I want to use it for blowing the roof. I have ordered two cheap tanks, and they are in transit. I have not a clue about the controls and drive electronics. I was an Industrial electronics tech in the 70’s, 80.’s and 90’s. That time has passed, (30+ years) and I need help. Electronics has changed and I have let it slide over… Read more »

Arben Bajro
9 months ago

Thanks for the complete and comprehensive tutorial.
I was wondering if it is possible to use less expensive components (for example: https://www.amazon.it/editore-aziona-Performance-regolatore-commutazione/dp/B0CF5TS3YL/ref=sr_1_51?crid=LO87P5PXDK43&keywords=motor+ driver+shield&qid = 1702278157&sprefix=MOTOR+DRIVER%2Caps%2C190&sr=8-51) and what contraindications there might be compared to more professional components.