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.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
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.
- 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.
Participated in the
Microcontroller Contest 2017