# More on Direct Current motors: H-Bridge and L293D

#### Objectives

• We keep on playing with the Direct Current motors.
• Introduce the H-bridge and why they are  necessary.
• The L293D integrated circuit.
• Assemble a variable speed drive and also a direction change mechanism.

#### Bill of materials

 Arduino UNO or equivalent. A solderless breadboard . Some jumper wires. A 5V DC motor and a wheel. An H-bridge L293D Integrated circuit. A 10K potentiometer.

#### Coming back to  Direct Current motors

We have seen very quickly the basic characteristics of brushed DC motors  (because there are several types besides this), but we will go into a little more detail on what things to consider when choosing one of these motors.

When you buy a DC motor you consider three things basically:

• Operation Voltage: it specifies the voltage range where the motor operates correctly. For example the one we are using operates between 3 and 6V, but it is not uncommon to find 9, 12 and 24V DC motors.
• Revolutions per minute:  It indicates the speed that can reach at the different voltages and it is usually measured in Rpm. Typical hobby motors will move between 200 and 2.000 rpm.
• Torque: or power that the motor can provide and indicates the payload it can move.

In the previous chapter we saw how to connect one of these motors to our Arduino and how to vary the speed of rotation by modifying the voltage that we deliver in the terminals. The speed of rotation of the motor depends directly on this voltage.

• Be very careful not to exceed the maximum voltage that the manufacturer indicates as safe or you will find that the smell of a burned engine is very characteristic and particularly unpleasant.

But one thing we did not do in the previous chapter was to reverse the direction of rotation of the motor, because this is a thorny issue, since for this we need to reverse the polarity of the voltage in terminals, and this, dear friends, can not be done using only our Arduinos (because Arduino can provide + 5V but not -5V).

To solve this problem we have to rack our brains and design a circuit called H-bridge. To understand it, imagine the following switch-based assembly:

When we use the switches as shown in the picture on the left the motor turns in forward direction. But if we use them as shown in the picture on the right it will turn in the opposite direction, because we have inverted the polarity of the voltage at the motor terminals, and therefore the direction of rotation, without having to reverse the polarity of the voltage. A complicated thing, isn’t it?.

These circuits are called H-bridge, because they vaguely resemble an H around the motor.

Of course, turning the motor by means of switches is impractical, and since we are electronic experts  (without a laugh, please), let’s see how we can do the same function using electronics, so not to have to switch them manually but by means of electrical signals. A typical H-Bridge would be similar to this diagram:

Forget about the diodes for now, because they are only used to protect the transistors from the inductive discharge of the motor. Applying different voltages in the pins A, B, C and  D we can move the transistors to cut-off  or saturation regions, so providing  an electronic means to open or close a circuit, which is equivalent to using switches .

• Note that two transistors are a PNP and another NPN to play with the voltage polarity.
• The magnetic field of the rotor stores energy, which can be important, and when we power off the circuit it must be released as an electric current properly directed by the diodes to prevent damage to the transistors or to the power supply.

Don’t panic!  I’m not going to ask you to assemble that circuit (although it would not be too complicated). Remember that when there is a need in the market, there is always someone willing to manufacture an integrated circuit that does that and more.

And since this was not going to be the exception, we found that we have several versions of H-bridge circuits depending on the voltage and current to be switched.

Today we are going to use an inexpensive, proven and easy to find integrated circuit that includes two H-bridges and is useful to control small DC motors. It’s called L293D.

#### The L293D H-Bridge

The L293D, is a small integrated circuit that includes two H-Bridge bridges that can be used to control simultaneously two DC motors.

If you look for the data sheet of the L293D, you will see that although it operates at 5V internally, it can switch voltages up to 36V for your motors.

• Make sure not to exceed the rated maximum voltage of your motors.  The chip will withstand it, but your motor…

Let’s see the L293D pinout:

• Pin 16, Vss, is the 5V used to power the chip.
• Pin 8, Vs, is the voltage used to power the motor.
• Pins from 1 to 7 control the first motor.
• Pins from 9 to 15 control the second motor.
• Pin 1, Enable1, enables motor 1 to be used. If we set it HIGH, the motor can turn depending on the values of I1 and I2. If we set it LOW, the motor stops, however the values of the rest of the pins.
• Pins 2 and 7 are the control pins for the first motor, motor 1, and they will be connected to our Arduinos to control the direction of rotation.
• Pins 3 and 6 are the output pins to which the motor 1 is connected. The polarity of the motor is inverted according to the values applied to the pins 2 and 7.
• On the above diagram you can see that there are equivalent pins for a second motor and which they are.
• The pins 4, 5, 12 and 13 are connected to GND.

We can use a table to show the logic behind the rotation of the motor according to the values of these three pins:

ENABLE CONTROL PIN 2 CONTROL PIN 7 MOTOR STATUS
LOW Motor stopped
HIGH HIGH LOW Turns forward
HIGH LOW HIGH Turns reverse
HIGH HIGH HIGH Motor stopped
HIGH LOW LOW Motor stopped

Therefore we have to set the enable pin for the motor to rotate and then use the pins Input1(pin 2) and Input2(pin 7) with opposite values to rotate the motor in one direction or the other. Easy, isn’t it? Let’s see how to assemble the prototype with our Arduinos.

Let’s make a summary of connections:

L293D Pin Arduino Pin Description
1 10 Enable
2 9 Input 1
3 Motor1 +
4,5, 12,13 GND GND
6 Motor1 –
7 8 Input 2
8 Vin Motor power
16 5V L293D power

Let’s go with the wiring diagram.

#### Circuit wiring diagram

Once the connections are clear, the wiring diagram to connect the chip L293D to our Duino is the following:

Let’s see the sketch  we are going to use to control this motor. Let’s use the 3 pins in the table above. Arduino Pin 10 is the enable pin of Motor 1 and we will use pins 8 and 9 to control the direction of rotation. So:

```#define E1 10    // Enable  pin   for motor 1
#define I1 8     // Control pin 1 for motor 1
#define I2 9     // Control pin 2 for motor 1

void setup()
{
for (int i = 8 ; i<11 ; i++) // Initialize pins
pinMode( i, OUTPUT);
}
void loop()
{    digitalWrite(E1, HIGH);      // Enable Motor1
digitalWrite(I1, HIGH);      // Start
digitalWrite(I2, LOW);
delay(3000);

digitalWrite(E1, LOW);       // Stop Motor 1
delay(1000);
digitalWrite(E1, HIGH);      // Enable Motor1

digitalWrite(I1, LOW);       // Start reversing the direction of rotation
digitalWrite(I2, HIGH);
delay(3000);

digitalWrite(E1, LOW);       // Stop Motor 1
delay(1000);
}```

The program can not be simpler. We set Enable1 to start Motor 1, and then use I1 and I2 with inverted values. The engine starts and we stop it after 3 seconds. 1 second later we set Enable1 again and exchange the values of I1 and I2, so the rotation of the motor starts in the opposite direction.

The most astute readers, will have realized that we have not changed the speed of rotation and since in the previous sketch all the values are digital, we will have problems to vary the speed in an analog way.

But the trick is that we have connected the Enable1 pin to the Arduino pin 10, which is a PWM pin (so, unintentionally) and the L293D are designed to vary the speed of rotation of the corresponding motors according to the voltage applied to this pin, so it is trivial to vary the speed of the motor without using analog values.

If, for instance, we add a potentiometer connected again to A1, we can use this value directly (divided by 4, of course) to pin 10 as an analog value, replacing the line:

`digitalWrite(E1, HIGH);  // Set Motor1`

by this one, that uses an analog value:

```analogWrite(E1, analogRead(A1) /4 );

```

We will set the speed of rotation of the motor according to the value of the potentiometer. The corrected sketch would look something like this:

```#define E1 10    // Enable  pin   for motor 1
#define I1 8     // Control pin 1 for motor 1
#define I2 9     // Control pin 2 for motor 1

void setup()
{
for (int i = 8 ; i<11 ; i++)                    // Initialize pins
pinMode( i, OUTPUT);
}

void loop()
{   analogWrite(E1, analogRead(A1) / 4);            // Enable Motor1
digitalWrite(I1, HIGH);                         // Start
digitalWrite(I2, LOW);
delay(3000);

digitalWrite(E1, LOW);                          // Stop Motor 1
delay(1000);
analogWrite(E1, analogRead(A1) / 4);            // Enable Motor1

digitalWrite(I1, LOW);                          // Start reversing the direction of rotation
digitalWrite(I2, HIGH);
delay(3000);

digitalWrite(E1, LOW);                          // Stop Motor 1
delay(1000);
}```

And that’s all, folks!

• If you assemble the prototype, keep in mind that the reading of A1 is only done when leaving the delay, so that variation is made at the beginning of each cycle of rotation and not in the middle of this.
• It would be a good programming example to make a sketch in which the speed varies immediately, although for this you will have to remove the delays.

#### Summary

• We have introduced the  H-bridges and told why are they necessary.
• We have seen an integrated circuit, the L293D, that includes 2 H-bridges able to control their speed and direction of rotation.
• We have assembled a prototype to use the H-bridge with our Arduinos.