Knowing how to drive motors using a microcontroller is an important part of learning robotics and electronics. In this blog, I will be explaining what an H Bridge is and how it can be used along with an Arduino to drive motors with the example of famous L293D motor driver IC (commonly misspelt as L239D). Let’s get started!
Recap of DC Motors
A DC motor (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 (direction) of the voltage respectively.
In simple words, when you connect a DC voltage (from let’s say, a battery) to the motor, it spins. The greater the voltage, the faster the motor will rotate. If the battery is connected in reverse, the motor will begin rotating in the opposite direction, but with the same speed.
But where do H-Bridges enter the picture?
DC motors, depending on their size, specifications and applied load, can draw extremely large currents which will switch of your Arduino at best and fry the microcontroller permanently worst.
H Bridges are ICs used to overcome this issue by acting as an intermediary between the Arduino and motor.
An H bridge circuit consists of 4 MOSFETs connected in an H shaped configuration as shown (along with a few extra transistors sometimes, not shown). In case you are not aware of circuit schematics, click here for a wonderful tutorial–
A MOSFET (Q1 – Q4) is a device which like a switch, can open or close the circuit. But unlike a regular switch which is controlled by physically moving it, a MOSFET is controlled using an external 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 what actually “drive” the motor as they are capable of carrying large currents. The third pin is called the base and is where the magic happens.
A small current supplied to the base can allow a larger current to flow from the collector to the emitter. When the base is pulled low, the collector and emitter are disconnected and when it is pulled high, they are connected. This allows a small current from the Arduino (or any microcontroller) to be “amplified” to drive large loads such as motors.
While it is possible to construct your own H Bridge using individual transistors, it wont be long before you realize that this is expensive, bulky and prone to failure. Instead, as mentioned before, we will be using the L293D H Bridge Motor Driver IC which is very popular, cheap, and has a lot of existing documentation.
Here is how the IC looks-
And here is the pin diagram-
The L293D (often misspelt as L239D) is a very popular H Bridge IC. It contains not one, but two H Bridges on it, allowing you to drive 2 motors individually upto 36V at 0.6A (1A in short spurts).
It also contains two extra transistors (one for each side) which are used for providing convenient speed control over the speed of rotation of the motor using PWM.
The IC has pins for supplying power, the control/input pins which are connected to the base pins of the MOSFETs, output 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 stop. If it is held high, the motor runs based on the control signal and if it is held low, then the motors are switched off irrispective of the control signal. You must have realized by now that this can be given a PWM signal from your Arduino to control the speed of the motor
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 beginner-friendliness, but most boards will work. If you are not very familiar with Arduino, I recommend that you go read my guide/tutorial on getting started with Arduino by clicking here.
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.
Finally, as I mentioned before, you will need an L293D H Bridge which will be used to amplify the Arduino’s current up to the motor’s requirements.
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.
Start by connecting the VCC and GND pins to the positive and negative terminals respectively of your DC power source (eg: 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, making it 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 pins 3 and 4 have been used for providing the input while pin 5 is being used for speed control.
Coding and Uploading
In this section I will be showing the code for this project. 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 indefinitely.
As always, start by defining what pins you have used for speed and direction control. While the input pins can be regular digital pins (I have used 3 and 4, you use any), the speed control must be done through PWM and requires a PWM capable pin (pins with the tilda ~ before them, I have used 5, you can use any other). 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 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. This makes sure that the two pins are always set to opposite states.
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), 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 look as follows-
Now, you can compile and upload your program and if everything was done right, the motor should start spinning! That’s it, you’re done!
You may try to modify the code so that there is only one function which handles both the speed and direction control as coding practice.
As I have said, L293D has the capability to control not just one but two motors. You may try to control two motors with the Arduino simultaneously, with both of them spinning at different speeds, in different directions.
Motors and H Bridges are extremely fun and useful too. If you found this blog helpful, leave a comment down below. I would love to know your thoughts!