# DC Motor Speed and Direction Control with L293D Driver IC and Arduino

In this tutorial, we will learn to control a DC motor’s speed and direction using the L293D motor driver IC. The L293D IC is a powerfully built motor driver IC. It is a dual-channel H bridge motor driver IC which can be easily used to drive two motors. We will discuss in detail about this motor driver IC and how to interface it with Arduino. Then, we will show you an Arduino sketch that will control the speed and direction of the DC motors every easily. So let us begin.

## DC Motors Introduction

DC motors are electro-mechanical machines which convert electrical energy into mechanical (rotational) energy. If you are looking to develop a robot such as a line follower robotobstacle avoidance robot, these DC motors will be the first choice for you. They are available in direct drive or geared types. But, both types require a dc voltage to operate. Therefore, we apply a DC voltage to drive DC motors.

## DC Motor Speed Control

The speed of rotation of motors is directly related to the input voltage. The higher the input voltage, the higher will be the rotational speed of the motor. But the voltage should be within the operating voltage range.

One important point to note here is that if we want to control the speed of a DC motor, we will need to provide a variable voltage to the DC motor. For example, If the operating voltage range of a motor is between 3 – 6 volts. If we apply 3 volts input, the motor will run at its lowest rated speed. Similarly, if we apply 6 volts, the DC motor will run at its highest rated speed. In short, we can control the speed of rotation by giving a variable input voltage to a DC motor. Usually, a pulse width modulation technique is used to generate a variable dc voltage from constant dc voltage source.

## DC Motor Direction Control

As you know that in case of DC power supply, there is a concept of polarity such as positive and negative terminals of a battery. The polarity of input voltage source determines the direction of rotation of a DC motor. For instance, if we connect the positive terminal of battery with one terminal and the negative terminal of battery with another terminal of DC motor, it will start rotating in clockwise direction. But if we interchange the connection of the battery by connecting opposite polarity, the DC motor will start rotating in another direction as shown in the simulation below.

Generally, an H-Bridge circuit is used to provide opposite polarity power to a DC motor without changing actual power supply connections. An H-Bridge consists of four switches, resistors, and two control signals as shown in the figure. Two logic probes control the direction of a DC motor. When left probe logic signal is on, the motor rotates in an anti-clockwise direction and when right probe logic signal is on, the motor rotates in a clockwise direction.

## L293D Motor Driver IC

L293D IC is known as a motor driver. It is a low voltage operating device like other ICs. L293D provides the continuous bidirectional Direct Current to the Motor. The Polarity of current can change at any time without affecting the whole IC or any other device in the circuit. L293D has an internal H-bridge installed for two motors.

H-Bridge is an electrical circuit that enables the load in a bidirectional way. L293D bridge is controlled by external low voltage signals. It may be small in size, but its power output capacity is higher than our expectation. It could control any DC motor speed and direction with a voltage range of 4.5 – 36 Volts. Its diodes also save the controlling device and IC from back EMF. To control the max 600mA amount of current an internal “Darlington transistor sink” installed in it, which could be used to control a large amount of current by providing a small amount of current. It has also internal “pseudo-Darlington source” which amplifies the input signal to control the high voltage DC motor without any interception.

### Pinout

The diagram below shows the pin out of L293D IC:

#### Enable Pins

• Pin1 (Enable): Pin 1 is known as the enable pin. It has a major effect on Input and output. If there is High logical signal on enable pin (EN) then there will be input and output between pin 2,3,6 & 7 (Input 1, Output 1, Input 2 & Output 2)
• Pin9 (Enable): Pin 9 is also the same as Pin 1. It controls the input and output signals. Pin 9 Controls the connection between Input 3, Input 4, Output 3 and Output 4. It also enables the connection when the logic signal will be High (1).

#### Input Pins

• Pin2 (Input 1): Mostly input means where we provide the input to give the output. But here Input 1 means which polarity we want to give at Output 1.
• Pin7 (Input 2): Input 2 will attach to the control button or device to control the Output 2 just like Input 1.
• Pin10 (Input 3): Input 3 will control the output polarity of the Pin 11 (Output 3) by logic signals.
• Pin15 (Input 4): Pin 15 will control the output polarity of the Pin 14 (Output 4) by logic signals.

#### Output Pins

• Pin3 (Output 1): Output 1 is the input of the first motor/Motor 1. It attaches to its one end.
• Pin6 (Output 2): Output 2 will attach to the input of the first motor/Motor 1. It will attach to its second end.
• Pin11 (Output 3): Output 3 will be connected to the one end of the second motor.
• Pin14 (Output 4): Pin 14 will attach to the second end of the second motor.

#### Power Pins

• Pin8 (Vcc): Pin8 is the voltage pin for Motor. It will decide how much power we are going to attach the Motor. This Power should not be more than 36 volts and should not be less than 4.5 volts.
• Pin16 (Vcc): Pin 16 will the Power we will provide to the L293D to activate it or to turn it on. The power level of Pin 16 should be 4.5 – 7 Volts. Voltage more than 7 will burn the IC
• Pin4 (Ground) and Pin5 (Ground) : The ground pins will attach to the ground of the circuit.
• Pin12 (Ground) and Pin13 (Ground): These will attach to the common ground with all other grounds.

### Control Pins

The L293D IC features both speed and direction control pins to control two motors simultaneously.

#### Direction Control Input Pins

The direction control pins are the four input pins (Pin2 IN1, Pin7 IN2, Pin10 IN3, Pin15 IN4) on the IC.

Through these input pins we can determine whether to move the dc motor forward or backwards. IN1 and IN2 control motor A’s spinning direction whereas IN3 and IN4 control motor B’s spinning direction. The table below shows the logic signals required for the appropriate spinning action for motor A.

As seen from the table, whenever one of the inputs is in a HIGH state (5V) then the motor will spin. Otherwise, when both the inputs are LOW (ground) state or both are in HIGH state then the motor stops. In order for motor A to spin forward, IN1 should be LOW and IN2 should be HIGH. For backwards motion, IN1 should be HIGH and IN2 should be LOW. Motor B is also controlled in a similar way.

#### Speed Control Pins

The speed control pins pin1 (EN1,2 or ENA) and pin 9 (EN3,4 or ENB) on the IC, control the speed of the dc motor and turn it ON and OFF.

EN1,2 controls the speed of one motor and EN3,4 controls the speed of the other motor. If both of the pins are in a logic HIGH (5V) state, then both the motors are ON and spinning at maximum speed. If both of the pins are in a logic LOW (ground) state, then both the motors are OFF. Through the PWM functionality, we can also control the speed of the motor.

You can know more about the L293D motor drivers here:

### Features

• L293D could be used to control the two motors at the same time.
• It has the ability to control the speed by using the enable pin.
• The direction is also easy to change.
• Voltage supply range is higher than other IC. Voltage range between 4.5-36 volts can easily handle by the IC to the motor.
• The motor has a maximum continuous range of current close to 600mA but the maximum peak current range is 1.2A
• It has an automatic shutdown system on thermal condition.
• Its working range is from 0 – 70 degree which is much higher for any small-sized IC.
• It has an internal back emp protection for IC and the controlling device.

## Interface L293D DC Motor Driver IC with Arduino

Now, as we have seen how to control the dc motor through the motor driver IC, let us do a demonstration by showing you how to control two DC motors using this IC.

### Required Equipment

1. Arduino
2. L283D Motor driver IC
3. External 3-12 V power supply
4. DC Motors
6. Connecting Wires

Assemble the circuit as shown in the connection diagram below.

We are using TT DC gear motors for this project. They require an operating voltage of 3-12V DC where the recommended operating voltage is 3-6V DC. Thus we will use 4xAA batteries (4×1.5V = 6V) to supply power for the DC motors. Connect positive terminal of power supply with pin8 and negative terminal with common ground.

Then, we have connected the input pins Pin2 (IN1), Pin7 (IN2), Pin10 (IN3) and Pin15 (IN4) with digital pins of the Arduino UNO. We have used GPIO9, GPIO8, GPIO7 and GPIO6 respectively to connect with each of the input pins of the motor driver IC.

The enable pins pin1 (ENA) and pin9 (ENB) are connected with the digital PWM pins of the Arduino UNO board. We have used GPIO10 to connect with EN1,2 and GPIO5 with EN3,4.

Each of the motor’s terminal is connected at Pin3 (OUT1), Pin6 (OUT2) for one motor and Pin11 (OUT3), Pin14 (OUT4) for the second motor respectively.

Connect pin16 of the driver IC with 5V from Arduino. Additionally, all grounds should be common.

## Controlling DC Motor Speed and Direction using L293D Motor Driver IC

Open your Arduino IDE and go to File > New. A new file will open. Copy the code given below in that file and save it.

### Arduino Sketch

This basic sketch will show us how to control a DC motor’s speed and direction of rotation using the L293D motor driver IC.

``````// First Motor connections
int ENA = 10;
int IN1 = 9;
int IN2 = 8;
// Second Motor connections
int ENB = 7;
int IN3 = 6;
int IN4 = 5;

void setup() {
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);

//Motors OFF
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}

void loop() {
setDirection();
delay(1000);
changeSpeed();
delay(1000);
}

void setDirection() {
analogWrite(ENA, 255);
analogWrite(ENB, 255);

// Motors ON
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(2000);

// Change Direction
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
delay(2000);

// Motors OFF
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}

void changeSpeed() {
// Motors ON
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);

// Speed Up
for (int i = 0; i < 256; i++) {
analogWrite(ENA, i);
analogWrite(ENB, i);
delay(20);
}

// Speed Down
for (int i = 255; i >= 0; --i) {
analogWrite(ENA, i);
analogWrite(ENB, i);
delay(20);
}

// Motors OFF
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}``````

### How the Code Works?

Firstly, we will define the L293D motor driver IC’s control pins connection with the Arduino UNO board. We have used the same pins as shown in the connection diagram above. Make sure the enable pins are connected with PWM enabled pins of the Arduino board. For the input pins you can use any digital pin of the Arduino UNO board.

``````int ENA = 10;
int IN1 = 9;
int IN1 = 8;
int IN3 = 7;
int IN4 = 6;
int ENB = 5;``````

#### setup()

Inside the setup() function, first we will configure all the control pins as output pins. This will be done by using the pinMode() function. The pin will be passed as the first parameter and OUTPUT will be passed as the second parameter.

``````  pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);``````

Then by using the digitalWrite() function we will set up all the input pins to a LOW state so that initially both the motors are off.

``````  digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);``````

#### loop()

Inside the loop() function, we will call the user defined functions setDirection() and changeSpeed() after a delay of 1 second. The first function will be used to control the direction of the motors and the second function will control the speed.

``````void loop() {
setDirection();
delay(1000);
changeSpeed();
delay(1000);
}``````

#### setDirection()

The setDirection() function is responsible to control the direction of the two motors. The motors can either turn on, off or change their directions. This is done by providing the input pins (IN1, IN2, IN3 and IN4) with different logic signals. As we have already discussed before, through these input pins we can determine whether to move the dc motor forward or backwards.

``````void setDirection() {
analogWrite(ENA, 255);
analogWrite(ENB, 255);

digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(5000);

digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
delay(5000);

digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}``````

First we will set up both the motors at their maximum speed. This will be done by using the analogWrite() function and passing the enable pin as the first parameter and ‘255’ as the second parameter. Here 255 denotes the highest PWM value.

``````  analogWrite(ENA, 255);
analogWrite(ENB, 255);``````

Then we will first turn both the motors ON, then change their direction and then turn them OFF. These three actions will be done after a delay of 5 seconds each. Use digitalWrite() to change the logic state of the pin by passing the pin as the first parameter and the logic state as the second parameter.

Follow the table given in the Direction Control Pins, section to set the input pins to required states appropriately.

``````  digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(5000);

digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
delay(5000);

digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);``````

#### changeSpeed()

The changeSpeed() function is responsible to control the speed of the two motors. As we have already discussed before, the speed control pins labeled ENA and ENB on the module, control the speed of the dc motor.

``````void changeSpeed() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);

for (int i = 0; i < 256; i++) {
analogWrite(ENA, i);
analogWrite(ENB, i);
delay(20);
}

for (int i = 255; i >= 0; --i) {
analogWrite(ENA, i);
analogWrite(ENB, i);
delay(20);
}

digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}``````

First, by using the digitalWrite() function turn on the motors by setting the relevant logic states to the input pins.

``````  digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);``````

Then we will increase the speed of the motors steadily. By using a for loop we will increment the PWM value from 0 to 255 (maximum speed). This is done through the analogWrite() function which takes in the enable pin as the first parameter and the PWM value as the second parameter.

``````  for (int i = 0; i < 256; i++) {
analogWrite(ENA, i);
analogWrite(ENB, i);
delay(20);
}``````

Likewise, we will decrease the speed of the motors from a maximum (255) to zero.

``````  for (int i = 255; i >= 0; --i) {
analogWrite(ENA, i);
analogWrite(ENB, i);
delay(20);
}

``````

Lastly, we will turn the motors off by providing a LOW signal to all the input pins.

``````  digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);``````

### Demonstration

To see the demonstration of the above code, upload the code to Arduino. Before uploading the code, make sure to select Arduino UNO from Tools > Board.

Also, select the correct COM port to which the Arduino board is connected from Tools > Port.

Once the code is uploaded to your board, the motors will start rotating.

First, the motors will start rotating backwards for 5 seconds then they will start rotating forwards for 5 seconds. Then the motors stop. After a delay of 1 second, the motors start speeding up and reach maximum speed then they start slowing down and finally stop. Then the loop starts again.

For more Motor Driver articles:

Web server related projects: