Controlling motors with Arduino and H-Bridges

The future of humanity is filled with robots and even now they are an essential part of making our world tick, but what makes robots themselves work? This blog is about how to use a motor with an Arduino using an L239D H Bridge IC.

What and How of DC motors

A DC motor, short for direct current motor is a motor whose axle rotates when a DC voltage is applied at its terminals. The speed and direction are dependent on the magnitude and polarity of the voltage respectively.

In simple words, when you connect a DC voltage (Remember guys, batteries provide DC while mains provide AC. Don’t confuse them!) to the motor, it spins. The more the voltage (or bigger the battery) the faster the motor will rotate, but in the same direction. If you reverse the battery and connect it, the motor will spin in the opposite direction, but with the same speed.

Now you must be asking where and why H bridges enter the picture. Let me explain.

DC motors, depending on their size, specifications and their current load can draw extremely large currents which are enough to at best, cause your Arduino to temporarily switch off and stop and at worst, cause overheating and fry your microcontroller.

H Bridges are ICs used to overcome this issue by acting as an intermediary between the Arduino and motor while also providing a slightly more convenient way of speed control.

H Bridges

An H bridge consists of 4 MOSFETs connected in an H shaped configuration, sometimes along with a few extra transistors (cosmetic only, not shown) like so (if you are not aware of circuit schematics, click here for a wonderful tutorial)-

A MOSFET (Q1 – Q4) is like a switch which can open or close the circuit, but unlike a regular switch which is manipulated by flicking it, a MOSFET is controlled using an electric current. Two of its pins are connected in series with the circuit like the terminals of a switch. These are called the collector and the emitter and are capable of carrying large currents. The third pin is called the base and it can be grounded or provided with current to connect or disconnect the collector and emitter. This allows a small current from the Arduino to drive a large load like the motor; like an amplifier.

While you can construct your own H Bridge using transistors which you purchased, it wont be long before you realize that this is inconvenient, bulky and prone to failure, as a result of which it isn’t really recommended. Instead, in this blog we will be using the L239D H Bridge IC which is very popular, cheap, has been used in a lot of projects, and has a lot of existing documentation.

Here is how it looks-

Image result for L239D
IC form
Image result for L239D
Diagramatic form

L239D Basics

I have already mentioned before that a lot of H Bridge ICs have some extra transistors on them. and the L239D is no exception. These are used for providing more convenient control over the speed of rotation of the motor, which I have also talked about.

The IC has pins for supplying power (multiple, in fact), the control pins which are connected to the bases of the MOSFETS, control pins which are connected to your load and finally another pin called the enable pin. This pin is responsible for “enabling” the motors to run or not to run. If it is grounded, the IC is switched off and if it is held high, then the IC is on. This can be connected to a third PWM capable pin on the Arduino which will be used for speed control.

Now that we have got all the theory out of the way, it is finally time to get started with the circuit and the code!

Circuit and Components

The first component you are going to require is an Arduino or equivalent microcontroller. I would recommend that you use the UNO due to its simplicity, but most boards will work. If you are not familiar with Arduino much, I recommend that you go read my guide/tutorial on getting started with Arduino by clicking here.

Image result for Arduino UNO
Arduino UNO

Next, you are going to require a DC motor, preferably a geared one such as a yellow BO motor, which I will be using, but anything will do.

Image result for BO motor
I shaped BO Motors
Image result for BO motor
L shaped BO motors

Finally, as I mentioned before, you will need an L239D H Bridge which will be used to amplify the Arduino’s current up to the motor’s requirements.

Image result for L239D
The IC itself
Image result for L239D
IC with labels

Optionally, you can also use a breadboard as it will make connecting the circuit a whole lot easier and convenient. Click here to read more about breadboards and how to use them.

Image result for breadboard
Full size Breadboard

Start by connecting the VCC and GND pins to the positive and negative terminals respectively of your chosen power source (or battery). Next, connect the motor’s terminals to the two output pins of the IC as shown below-

Finally, connect two digital pins of your Arduino to the two input pins of the IC and a PWM capable pin to the enable pin on the IC like so, making it finally look like this (I will be explaining this with more detail in the next step so don’t worry if you didn’t fully understand) –

The red wires indicate +ve voltage, black indicate ground, yellow indicates +ve output and input, blue indicates -ve output and input and orange indicates the PWM control pin. In this case 3 and 4 have been used for providing the input while 5 is being used for speed control.

Coding and Uploading

The code for this project will be simple. It will accelerate the motor from stationary to maximum speed, de-accelerate it and repeat this in the opposite direction, with this whole cycle repeating indefinetely.

As always, start by defining what pins you have used for speed and direction control. While the direction control pins can be regular digital pins (I have used 3 and 4, use whatever you want), the speed control must be done through PWM and requires a PWM capable pin (pins with the squilda ~ before them, I have used 5, use whatever you feel like). If you are not familiar with PWM, I have a tutorial ready for you to read so click here. I have named them SP, M1 and M2.

Next, in the setup, set the pin modes of each of the pins. They will not be used for measuring any voltage and as a result are going to be output pins.

Now, to make the program shorter and easier to read, it will be broken into functions to handle speed and direction control. First, the speed control as it is simpler. It will simply take speed as a parameter (from 0 to 255) and set pin SP to the it.

The next function will set the direction of the motor and is slightly more complicated. It will take either HIGH (forward) or LOW (backward) as a parameter, which is a boolean value. Then it will set M1 to the given value and M2 to its inverted value.

Finally, in the main loop, create four for loops, each going from 0 to 255 in increments of 1 with the appropriate delay (I have used 1 second or 1000 milliseconds), changing the speed of the motor each time. Before each loop, remember to set the right direction of the motor.

Now, your entire program should be looking a little like this-

After you have made sure there are no typos and have selected the right board and port, compile and upload your program and watch the motor get into action! That’s it, you’re done!

Final Remarks

Now that you are done with the basics, try to modify the code so that there is only one function which handles both the speed and direction control as coding practice.

You would have also realized that the L239D has the capability to control not just one but two motors. Try to control two motors with the Arduino, with both of them spinning at different speeds in different directions.

Finally, go brag a bit about it to your friends, you’ve earned it after all!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s