H-Bridge Motor Control

H-Bridge Motor Control


In this project, we will demonstrate how to use an H-Bridge to control a DC motor. The H-bridge IC will control the direction and speed of the motor based upon the signals from the chipKIT™ board. This project will also demonstrate how to safely drive inductive loads like the motor. The overall goal of this project will be to become familiar with the operation of the H-Bridge and DC motor, as well as understanding the possible applications for the components.

Before you begin, you should:
  • Understand the basic operation of diodes.
  • Understand inductive flyback and the purpose of flyback diodes.
After you're done, you should:
  • Understand the operation of an H-Bridge IC.
  • Be aware of the H-Bridge's specifications and its possible applications.
  • Understand how to use diode protection circuits/flyback diodes to safely drive an inductive load such as a motor.


Qty Description Typical Image Schematic Symbol Breadboard Image
4 Diode
1 L293DNE H-Bridge
1 GM12-N20VA DC Motor
1 Battery Pack
4 AA battery

(not included in parts kit)

Getting familiar with the L293DNE H-Bridge

In order to complete this project, you will need to become familiar with the H-Bridge provided in the Analog Parts Kit. If you have no prior experience with H-bridges and want to learn the basics, the red tab in the related materials section will provide additional information.

The type of H-Bridge included in the Analog Parts Kit is known as a quadruple half H-Bridge, or dual full H-bridge. This is because it can function as 4 half H-Bridges, equivalent to 2 full H-Bridges. With 4 half H-Bridges you can control the speed of 4 separate motors, each hard-wired to spin a direction of your choosing. You can also opt to combine the half H-Bridges to create two full H-Bridges that will drive two motors with variable speed and direction.

Since there are 4 half H-bridges inside of the L293DNE, there are a decent amount of pins on the IC. To be able to use this H-Bridge you will need to understand the function of its pins, which are illustrated in Fig. 1 below.

Figure 1. L293DNE H-Bridge pinout.

To start, let's look at pin numbers 4, 5, 12, and 13. These are the centermost pins labeled Heat Sink and Ground. These are the pins you want to connect to your circuit's ground. The pins can also be used to help cool the chip. Since you can use the H-Bridge to control relatively high power loads (max of 600 mA at 36V), the chip heats up easily. To help combat this, the 4 ground pins are designed to help transfer heat out of the chip. Unfortunately, they don't do a very good job by themselves. To help, you can attach a heat sink (a small metal block with many fins) to help radiate the heat away more effectively. If you don't have a heat sink and your chip gets too hot, it will automatically shut off until it returns to a safe temperature.

Next, let's examine how the pins 1A, 1Y, Vcc2 and EN relate. These pins are used to control one of the four half H-bridges inside of the IC. Pin 1Y is the output for the first half H-Bridge in the IC. Pin 1A is the input which configures what pin 1Y will output. If 1A is driven HIGH then 1Y will be configured to output Vcc2, if 1A is driven LOW then 1Y will be configured to output Ground. Once pin 1Y has been configured the EN pin is used to turn pin 1Y on or off. When pin 1Y is “off” it means no current can flow through it; this is very different from when 1Y is configured for Ground, since ground allows current to flow to complete a circuit. Ultimately, the EN pin will be used in this project to control the motor's speed. This is done by changing the rate at which the EN pin is driven HIGH and LOW. To help you visualize how half H-Bridge functions inside the IC, refer to figures below.

Figure 2. Half H-Bridge pinout.
Figure 3. Half H-Bridge circuit configurations.

Looking at Fig. 2, you could probably figure out the behavior of the circuit for any set of inputs. The results you would end up with are summarized below in Fig. 5.

The next few pins we will look at are pins 2A and 2Y. These pins function identically like pins 1A and 1Y and are used to control a second half H-bridge. Both sets of pins (1A, 1Y and 2A, 2Y) are all controlled by the same enable pin (labeled 1,2EN). This is because these pins can be used to form a full H-Bridge , and therefore must be triggered simultaneously. To configure the full H-Bridge, you need to apply the correct values to pins 1A and 2A. This will change voltage polarity across pins 1Y and 2Y. For instance, in Fig. 4 you can configure pin 1Y to Vcc2 and pin 2Y to ground. This will cause the voltage drop Vout to be +Vcc2. If you reverse the configurations for 1Y and 2Y, you can make Vout equal -Vcc2. When you want to stop the motor, you may configure pins 1Y and 2Y to both be the same. This will make the voltage drop Vout be zero and prevent any current from flowing through the motor. Finally, if you drive the H-bridges EN pin LOW it will completely disconnect the H-bridge from Ground and Vcc2. As long as EN is LOW the H-bridge will remain off. It doesn't matter what you set pins 1A and 2A to. This is why their inputs are represented with the don't care symbol X in that row of the table. The configuration table and simplified configuration circuit are shown in Fig. 4 below.

Figure 4. Full H-Bridge circuit configurations.

Just like the previous set of pins we discussed, the pins 3A, 3Y, 4A, 4Y, and 3,4EN (seen in Fig. 3.) all work the same with relation to each other.

An additional thing to note about the H-bridge is that the output pins (1Y,2Y,3Y,4Y) all make use of Vcc2 as their power source. They do not get their power from Vcc1. Vcc1 is used as a separate power source for the L293DNE's internal components, like the heat projection circuit. Having two separate power inputs allows you to save energy by operating the IC on a small power source with Vcc1. Meanwhile, you can still supply a large amount of power to the H-bridges using Vcc2.

Safely Driving an Inductive Load With a Diode Protection Circuit:

Now that we are familiar with the L93DNE H-Bridge, we can discuss the remainder of the circuit. We will be using the H-bridge to drive a DC motor. Inside the motor there are two electromagnets that repel each other when powered. This is what causes the motor to spin. These electromagnets are a type of inductor since they store energy in the form of a magnetic field. Because the motor contains two inductors, we need to be aware of the inductive flyback they generate. Inductive flyback is a large voltage spike that results from abruptly changing the current flow to an inductor. If you are unfamiliar with inductive flyback, follow the orange tab on the right.

Normally, to combat inductive flyback you only need a single flyback diode. Due to the variable polarity that is applied by an H-bridge, we will need several flyback diodes. We want to prevent the voltage from spiking above Vcc2 and spiking below ground. To accomplish this, we will use a diode protection circuit. This circuit consists of two diodes that allow an input voltage to go no higher than Vcc and no lower than ground. If you are unfamiliar with diode protection circuits, follow the yellow tab on the right. We will need two diode protection circuits in total; one for each electromagnet in the inductor. With these diode protection circuits, the H-bridge can safely drive the inductive load.

Step 1: Assembling the circuit

Figure 5. H-bridge motor control circuit.

Assembly Steps:

  1. Connect the positive end of the battery pack to the breadboard's red power rail.
  2. Connect the chipKIT's ground and the battery pack's ground to the breadboard's blue rail.
  3. Place the L293DNE H-bridge IC so that it straddles the breadboard's valley.
  4. Connect the H-bridge heat-sink pins to ground.
  5. Connect H-bridge Vcc1 pin to the 5V power rail.
  6. Run a wire from the H-bridge's 1,2EN pin to the chipKIT's PWM pin 10.
  7. Run a wire from the H-bridge's 1A pin to the chipKIT's pin 9.
  8. Run a wire from the H-bridge's 2A pin to the chipKIT's pin 5.
  9. Place two connected diodes above the H-bridge.
  10. Place two connected diodes below the H-bridge.
  11. Connect the right most side of both sets of diodes to ground.
  12. Connect the left most side of both sets of diodes to the 5V rail.
  13. Connect H-bridge pins 1Y and 2Y to the center of either set of diodes.
  14. Connect H-bridge Vcc2 pin to the 5V power rail.
  15. Connect one of the motor terminals to the center of the top set of protection diodes.
  16. Connect the other motor terminal to the center of the bottom set of protection diodes.
Fig. 6. Circuit Schematic.

The schematic for the circuit can be seen in Fig. 6 above.

Step 2: Writing the Sketch


Before we begin writing the sketch, we should discuss exactly what we want it to do. The goal of this project is to control a motor's speed and direction, so naturally we will need the functions to do so. To control the motor's speed, we will simply use a function which varies the PWM signal being sent to the H-bridge's EN pin. Next, we will want a function that can tell the H-bridge to change its polarity, which in turn changes the direction of the motor. This function simply has to drive the the correct H-bridge pins HIGH or LOW, as specified in the configuration table from Fig. 4. Whenever the H-bridge changes its polarity, it must be disabled. If it is not disabled you can damage the H-bridge (for more info click the red H-bridge tab above). To prevent damage while switching directions, we will need a function that disables the H-bridge. This can be accomplished by driving the H-bridge's EN pin LOW. Finally, once the direction is changed we will need a function to re-enable the H-bridge and restore the motor's previous speed (PWM Value). To keep track of the PWM value we will use a global variable called Motor_PWM_Value. For details about how each function works and how they relate, read their comments and examine the code provided below.

				// Define H-Bridge Control Pins
				int HB_1A = 9;
				int HB_2A = 5;
				int HB_EN = 10; // PWM pin 10
				int Motor_PWM_Value; // Global variable that keeps track of the motors current PWM value
				void setup()
				  //Enable H-Brdige Control Pins for output
				  pinMode(HB_1A, OUTPUT); // 1A
				  pinMode(HB_2A, OUTPUT); // 2A
				  pinMode(HB_EN, OUTPUT); // EN
				 // By Defualt always start with the H-bridge disabled
				 // By Default always start with the motor in the stop position 

				// Test Motor Controls
				void loop()
				  setDir(1); // Set motor to direction 1
				  setMotorSpeed(.5); // Set motor speed to 50%
				  delay(2000); // Wait 2 seconds
				  setDir(2); // Set motor to direction 2
				  delay(2000); // Wait 2 seconds
				  setDir(0); // Stop Motor
				  delay(2000); // Wait 2 seconds

				///Define H-Brdige/Motor Control Functions////

				//Set the motors speed
				void setMotorSpeed(double motor_speed )
				{ /*Here motor_speed is a percentage of the maximum speed, 
				   it can take decimal values of 0.0 (0%) to 1.0 (100%) */
				  //Convert the motor speed percentage to a corresponding PWM value
				   Motor_PWM_Value = int (255 * motor_speed);
				   Enable_H_Brdige();//Enable and Update H-Bridge to new PWM speed  

				//Disable H-Bridge 
				void Disable_H_Brdige()

				//Enable H-Bridge 
				void Enable_H_Brdige()
				  //re-enables H_Bridge with its previous PWM Value
				  analogWrite(HB_EN, Motor_PWM_Value);

				//Set Direction of Motor
				void setDir( int Dir)
				   Disable_H_Brdige();/*When chaning directions always disable the H-Brdige. 
										 This will prevent you from burning out the H-bridge */
				  if(Dir == 1)
				  {//Set motor to direction 1
					digitalWrite(HB_1A, LOW);//write to 1A
					digitalWrite(HB_2A, HIGH);//write to 2A     
				  else if(Dir == 2)
				  {//Set motor to direction 2
					digitalWrite(HB_1A, HIGH);//write to 1A
					digitalWrite(HB_2A, LOW);//write to 2A   
				  {//Set motor to stop posistion 
						digitalWrite(HB_1A, LOW);//write to 1A
						digitalWrite(HB_2A, LOW);//write to 2A   
				  Enable_H_Brdige();/* Now that the direction has been changed the H-bridge is re-enabled.
									  The motors previous speed is also restored  */

Test Your Knowledge!

Now that you've completed this project, try experimenting with it.

  • Play with the motor speed. What is the slowest speed you can use that will make the motor turn?
  • Try removing the battery pack and powering the circuit with the chipKIT's 5V source pin. Is the motor as responsive compared to when it used the battery pack? Why is there a difference?
  • Try modifying the circuit and code to use a potentiometer to control the motor's speed.

  • Other product and company names mentioned herein are trademarks or trade names of their respective companies. © 2014 Digilent Inc. All rights reserved.
  • Circuit and breadboard images were created using Fritzing.