loading

An H-Bridge module/shield allows a micro controller like Arduino to operate a motor in two directions (clockwise and anticlockwise). An H-Bridge fits many applications like a mini car. Usually a H-Bridge contains an IC like L293D. In this instructable I'm going to show you how to make a H-bridge shield for Arduino which can control up to 2 motors using only NPN transistors. It is not only fun to make but, is also cheaper than buying commercial H-bridge shields.

Step 1: Materials Required

Components needed to make the shield-

  • 8* NPN transistors (I used some BC 547 and some 2N2222a because I didn't have 8 of either one)
  • 8* 1k ohm resistors (a value close to that should work)
  • Stack Headers (optional)
  • Pin Headers
  • 9V Battery and clip
  • Perf Board

Hey, if you want a fun project for determining if your transistor is NPN or PNP please refer to my 'The Transistor Testenator' instructable.

Tools

  • Soldering iron
  • Wire cutter and pliers

You, of course, need to have an Arduino and motors. A breadboard would also help as it allows you to prototype the circuit before soldering it permanently on to the perf board.

Step 2: The Circuit

The above diagram shows the circuit for controlling 1 motor. It consists of 4 transistors which are connected to two Arduino PWM pins. T1 and T3 bases are connected to the same pin on the Arduino and the same for T2 and T4. When the pin connected to T1 and T3 goes HIGH, current flows through T1, through the motor and then to T3 and goes to GND. This rotates the motor. When the pin connected to T2 and T4 goes HIGH, the current flows through the motor in the opposite direction.

To make a H-Bridge for two motors the circuit is simply duplicated as two independent circuits. In this project PWM pins 10, 11 control one motor and pins 6, 9 control the other. You have to use PWM pins.

Step 3: Bread Board It

You can skip this step if you want, but it is always a good idea to prototype the circuit on a breadboard before putting it on a perf board. Follow the above Eagle/Fritzing schematic to recreate the circuit. Please note that the Eagle schematic is shown only for a single motor H-bridge. To make it a dual motor H-bridge you simply need to replicate the same circuit and connect it to its respective PWM pins. One of the motors is connected to pins 10, 11 and the other is connected to pins 6, 9. When I made the circuit I made only a single motor H-bridge for saving time.

Important points to note:

  • Make sure you connect the collector and emitter of your transistor properly. Many a times, the pin configurations of even two NPN transistors differ. So, do refer to the datasheet of your transistor. In the Fritzing schematic I've used 2N2222 transistors. Attach your transistors according to their pinout. In the Eagle schematic the transistor pin with an arrow represents emitter.
  • Also, connect one resistor per transistor (at the base). If you connect only one common resistor to the bases of two transistors that are connected to the same Arduino pin, the circuit wouldn't work. I am not sure why, but that's what I have seen.

Once you are done with bread boarding the circuit upload the below code-

Once you upload the sketch your motors connected to Arduino would first rotate in one direction slowly for 2 seconds then fast for 2 seconds then stop for 1 second. After that it would rotate in the other direction in a similar fashion. The sketch spins motors connected to pins 10, 11 and 6, 9 (if you've connected both).

Step 4: Making the Shield - Prepare the Board and Solder the Headers

The Shield requires the pins 6, 9, 10, 11 and GND for its motors. So I made my shield cover pins 6 - GND on the Arduino (the GND pin on the i/o pin side). That is why it can be seen that my shield has 7 pins. But, you can make your shield any way you like. Given below is a tutorial on how to make shields by DIY Hacks and How Tos -

If you have got stack headers with long leads as shown in the video, you can directly solder them as a through-hole component and carry on to the next step. But if you have stack headers with tiny leads like mine, you have to use pin headers. To solder the pin headers place the perf board on a rigid surface and then put the pin headers and then solder (refer to the first three pics). After that solder the stack headers to each of the pin headers (refer to the last two pics). The stack headers allow you to access all the pins.

Step 5: Complete the Shield

Solder the the rest of the components as shown in the above schematic. Note that the Eagle schematic shows only the schematic for a single motor (just replicate the circuit to make it for two motors). The 'To PWM Pin' goes to 6, 9 (for one motor) and 10, 11 (for the other motor). Also connect Pin Headers to connect to the motor (the two dots which say 'Motor' in the Eagle schematic). After you are done, trim the board, insert the shield on to your Arduino board and test it out.

To make use of this and control the motors, you can either use the library that I have attached or write your own program. If you write your own program, to spin the motors, you can either give a digitalWrite(HIGH) or analogWrite(<0 to 255>) to one of the pins of the motor; while giving LOW to the other pin. If you want to spin the motor in the opposite direction, in your program, you can simply swap the pins to which you send LOW / HIGH values.

You are then done!

Step 6: Library

I like making libraries for Arduino and therefore decided to make my own for the H-Bridge. Please try and use this library and give me advice on how I could improve it. The library makes writing the code for the H-bridge easier and reduces the length of the code. The library is called 'Motor'.

Here is a brief documentation for the library:

class Motor
Helps you manage a motor on the HBridge

Constructor:
Motor (int Pin1, int Pin2);
Initializes an object of Motor class. Pin1 and Pin2 are the arduino pin
numbers to which the motor is connected. Ensure to connect the motor to
PWM pins only.

Methods:
SpinForward (int Speed);
Spins the motor in one direction. Pin2 is set to LOW and Pin1 is set to
appropriate PWM signal. Pass Speed between 0 and 255. If the passed speed
is negative, it is defaulted to zero. If the passed speed is greater than
255, it is defaulted to 255.

SpinBackward (int Speed);
Same as SpinForward, except it rotates the motor in the opposite direction.

Stop ();
Stops the rotation of the motor.
      

To add the library simply download the zip file given below. Open the Arduino IDE go to Sketch/Include Libraries/Add zip file. Select the library zip file. Given below is an example of the usage of the library.

I hope you enjoyed this instructable. Please feel free to give me suggestions on how I can improve my Instructables/Projects. If you liked this Instructable take a look at my others as well.

My Instructables

<p>what code</p>
<p>Your circuit wastes at least 4.6V (x current) as heat in the upper side transistors. Their emitter will have a voltage equal with the difference between the voltage of their base and the voltage drop on the diode made by the base-emitter junction - that is 5v (arduino out) - 0.6V drop = 4.4V. As your source voltage is 9V, the difference is a voltage drop (waste) on the transistor. Measure it if you don't believe me.</p><p>Suggestion: google for &quot;level switch BJT&quot; and use a &quot;push-pull emitter-follower switch&quot; as the half-bridge.</p><p>Alternatively, use a 6V source rather than a 9V one - it will put less stress on the upper BJT-s and the output will be the same. </p>
<p>Thanks for telling me, but why does this happen? I am quite new to electronics and so don't know too much about the working of transistors. Also you said that the voltage the emitter would be 5v - voltage drop made by the diodes. So, does that mean that the source voltage doesn't as the voltage over 4.4V would be wasted as heat? Would this cease to happen if I used a mosfet?</p><p>Thanks.</p>
<p>&gt; but why does this happen?</p><p>Because the BJT amplifies currents. The current through the transistor and the current through its base is in a relation of Ice=?*Ibe. Assume the base-emitter diode is perfect and has a 0 voltage drop; the moment your emitter has a higher voltage than the base, the base current becomes zero, thus the collector-emitter current will cease as well. Therefore, the maximum emitter voltage which will still let the current through the BJT is the voltage that keeps a non-zero current through base-emitter diode. That diode is not a perfect one, it will need somewhere between 0.6-0.7V to let any current through.</p><p>&gt; I am quite new to electronics and so don't know too much about the working of transistors.</p><p>A good time to learn, then. Google is your friend, &quot;BJT basics&quot; or &quot;transistors explained&quot; or whatever your google-fu tells you.</p><p>allaboutcircuits.com seems a good starting point. See this one: </p><p><a href="https://www.allaboutcircuits.com/textbook/semiconductors/chpt-4/bipolar-junction-transistors-bjt/" rel="nofollow">https://www.allaboutcircuits.com/textbook/semicond...</a></p><p>&gt; So, does that mean that the source voltage doesn't as the voltage over 4.4V would be wasted as heat?</p><p>Essentially, <em>with 5V applied on your base</em>, yes. With 9V applied to your base as input and 9V as your source, you will waste only the power caused by the drop on the base-emitter diode.</p><p>This is where my suggestion of &quot;use a level switch&quot; comes in - it will <br>help &quot;translate&quot; your 5V input in a voltage (almost) equal with your <br>source (9V battery in your case).</p><p>&gt; Would this cease to happen if I used a mosfet?</p><p>Ummm... no and yes.</p><p>No - you can't simply substitute the BJT in your circuit with mosfets and have it working.</p><p>Yes - properly designed H-bridge circuits using mosfets are more efficient than their BJT counterpart. </p><p>As I said, time for you to do some learning - it well worth it, electronics are fun.</p>
<p>Thanks for telling and giving a source for reading about the transistors. Please feel free to give more advice/ideas. Also please tell me if you know a place to access good tutorials about electronics.</p><p>Thanks.</p>
This is really helpful: thanks for sharing!
<p>Good work. But these days probably you should use power mosfets.</p>
<p>Thanks. Yes I could use power mosfets but I didn't have them at home. Transistors for small DC motors like the ones I have work fine.</p>
<p>Good work</p>
<p>Thanks!</p>

About This Instructable

1,694views

27favorites

More by Tanmay Deuskar:DIY H-Bridge Shield for Arduino With Transistors The Tic Tac Adjustable Power Supply The Transistor Testenator 
Add instructable to: