If you want to build a basic DIY Electronics project such as a Mini Fan, simple robot, or robotic arm, you need to learn to control the speed and direction of DC motors. Using the L293D motor driver IC is the easiest way to control DC motors due to its simplicity, flexibility, and ease of integration with Arduino. 

In this tutorial, you will learn how DC motors work, and how to control DC motor using the L293D motor driver IC and Arduino Uno.

How does a DC Motor Work?    

A DC motor operates on the principle of electromagnetic induction that converts electrical energy into mechanical motion. It consists of two main components: the stator (stationary part) and the rotor or armature (moving part). The rotor is generally located on the inside of the motor, while the stator is located on the outside. The rotor consists of coil windings and the stator has either permanent magnets or electromagnetic windings.

Dc motor construction and parts

When the DC is applied to the motor, a magnetic field forms within the stator, attracting and repelling the magnets on the rotor. This initiates rotor rotation. To keep the rotor rotating, the motor has a commutator which keeps the polarity of the field flipping. 

Controlling a DC Motor

Controlling a DC motor involves regulating its speed and direction of rotation. Here we will use two methods to achieve full control over the motor: 

  • Pulse Width Modulation (PWM) for speed control
  • H-bridge for control over the direction of rotation

PWM – for Motor Speed Control

One method often used to control the speed of a DC motor is Pulse Width Modulation (PWM). In this technique, the average power provided to the motor is modified by delivering voltage given to the motor in a sequence of pulses. The voltage provided to the motor is proportionate to the pulse width generated by a microcontroller.

The percentage of time the signal is in the ON state within each pulse period is represented as the Duty Cycle. By regulating the duty cycle, we can effectively control the average power supplied to the DC motor. If the duty cycle is higher, the average power supplied to the DC motor is also higher so the motor rotates faster. If the duty cycle is shorter, the average power supplied to the DC motor is also lower thus the motor rotates at low speed.

This dynamic method of adjusting voltage allows for precise and efficient control of motor speed. The following image shows the PWM method with different duty cycles:

DC Motor Speed Control using PWM

   💡Must Read

How to Use Pulse Width Modulation on the Arduino

How to Use Pulse Width Modulation on the Arduino

This article will provide more in-depth information on what is Pulse Width Modulation and how it works?

H-Bridge – for Motor Direction Control

To control the direction of rotation of a motor, we need to reverse the polarity of the voltage applied to the motor. This can be done by using an H-bridge circuit configuration. It consists of four switching elements, typically transistors, arranged in a configuration that resembles the letter “H.” These switches allow the current to flow through the motor in either direction, enabling control over its rotation. By turning ON and OFF two opposite switches, the H-bridge can reverse the polarity of the voltage applied to the motor, effectively changing its direction. 

In the below figure we have used four BC547 transistors to make an H-bridge and a motor has been connected between the two pairs.

H-Bridge Circuit – for Motor Direction Control

The ‘1’ in Red represents the transistor is ON and a ‘0’ in blue represents the transistor is OFF.

In the first figure, the Motor is running in a clockwise direction, and in the second figure, the motor is running in a counter-clockwise direction.

L293D Motor Driver IC

The L293D is a popular integrated circuit (IC) used as a motor driver in electronic circuits. Specifically designed for driving small DC motors, it provides two H-bridge configurations within a single chip. The H-bridge arrangement allows the L293D to control the direction of motor rotation. This IC can drive two DC motors simultaneously and the direction of these two motors can be controlled independently. 

The L293D also features built-in protection diodes, which help prevent damage to the circuitry from the back electromotive force generated by the motors. 

Technical Specifications

  1. Supply Voltage for Motors (VCC2): 4.5V to 36V
  2. Supply Voltage for IC (VCC1): 4.5 to 7V
  3. Logic Input HIGH: 2.3V to VCC1
  4. Logic Input LOW: –0.3V to 1.5V
  5. Output Current: 600mA per channel
  6. Peak Output Current: 1.2A per channel

L293D Motor Driver IC Pinout

The L293D motor driver IC typically has a 16-pins, and the pinout is as follows:

L293D Motor Driver IC Pinout

Power Pins: The L293D IC has two input power pins – VCC1 and VCC2 

VCC1 This pin provides power to the IC. So, this pin should be connected to 5V.

VCC2 This pin is connected to the motor supply voltage. It is the voltage that gives power to the internal H-Bridge of the IC to drive the motors. The voltage is usually in the range of 4.5V to 36V, depending on the specifications of the IC and the motors.

Ground Pins 4, 5, and 12, 13 are the ground pins of the IC. All 4 Ground pins are internally connected and also act as a heat sink for the IC.

Output Pins: The L293D has four output pins: Output 1 , Output 2Output 3, and Output 4 . These pins are used to connect to the terminals of the DC motors. Output 1 and Output 2 can be used to control one motor. The Output 3 and Output 4 can be used to control another motor. So, with this IC, you can control two 4.5 – 36V DC motors respectively.

Direction Control Pins: The L293D has two sets of input pins for controlling the direction of the motors: Input 1/ Input 2 for motor 1 and Input 3/ Input 4  for motor 2. The combination of logic levels applied to these pins determines the direction of rotation (clockwise or counterclockwise) for the connected motors.

IN1IN2Spinning Direction
HIGH(1)HIGH (1)Motor OFF
LOW (0)LOW (0)Motor OFF
HIGH (1)LOW(0)Forward
LOW (0)HIGH (1)Backward

Speed Control Pins: The speed of the motors can be controlled by varying the input voltage on the Enable pins: Enable 1 for Motor 1 and Enable 2 for Motor 2. Applying a PWM (Pulse Width Modulation) signal to these pins allows you to control the average voltage supplied to the motors, thereby adjusting their speed.

To know more download the datasheet of L293D IC:

Working modes of L293D IC

L293D works in two modes:

  • Four Motor control without speed and direction control.
  • Two Motor control with Direction and Speed control.

Controlling Four Motors with L293D IC

In the above circuit, we can see four DC motors are connected at each output of the L293D IC. The other end of the motors connected to Output 1 and Output 3 is connected to Ground and the motors that are connected to Output 2 and Output 4 are connected to the 24 Volts Supply.

According to the internal working of the L293D IC, it is required that Input 2 and Input 4 should be inverted and hence they are inverted before being given to the IC pins. Pin 16 is connected to the 5V supply and the supply for the motor (24V) is connected to Pin 8 of the IC.

As soon as the logic at the Input pin is set to ‘1’ i.e. HIGH, the motor starts rotating. The rpm of the motor can be seen increasing and after some time rpm becomes stable.

The motors connected to the Output 1 and Output 3 are rotating in the clockwise direction and the motors connected to the Output 2 and Output 4 are rotating in the counterclockwise direction

Controlling Two Motors with L293D IC

In the above circuit, L293D IC is controlling the direction and speed of the two DC motors connected to its outputs

The first motor is connected between Output 1 and Output 2 whose direction is controlled by Input 1 and Input 2 and the speed is controlled by Enable 1 pin. The second motor is connected between Output 3 and Output 4 whose direction is controlled by Input 3 and Input 4 and the speed is controlled by Enable 2 pin.

When the Duty cycle of the Pulse connected to the enable pin is less, the average power delivered to the motor is less and hence the speed of the motor is less. The power can be seen at the wattmeter connected to the motors in series.

Initially pulse at 50 Hz and duty cycle 50% is given to the Enable pin. Later when the duty cycle is increased to 100% the speed of the motor is increased and the power delivered to the motor is doubled thereby the power consumption of the motor is almost doubled.

When the polarity of all four Inputs is reversed, then first the speed of both the motors is decreased then the direction is reversed, and their speed increases in opposite direction.

Interfacing L293D Motor Driver IC with an Arduino

In this section, we will be interfacing an Arduino UNO with an L293D IC. Two DC motors will be simultaneously controlled using code. The speed of both motors will be controlled using the POT connected to the Analog Input A0. The higher the voltage at A0, higher will be the speed of the motors.

Interfacing L293D Motor Driver IC with an Arduino Uno

Hardware and Software Requirements

Hardware Requirement

Component NameQuantity
RemarksWhere to Buy
Arduino UNO R31Revision R3Amazon
L293D IC116 Pin DIP PackageAmazon
9V Battery1For Powering MotorAmazon
DC Motor2For connection with L293D ICAmazon
POT110k ohm and it will be used to change the speed of DC Motor.Amazon
Breadboard1Big sizeAmazon
Connection wires25For breadboard and Arduino connectionsAmazon

Software Requirement

Arduino IDE, Version 2.1.1 or above installed on your PC.

Connections

IC Side PinConnected To
Enable In1, In2 (Pin 1)Pin 11 of Arduino
Input 1 (pin 2)Pin 10 of Arduino
Output 1 (Pin 3)The positive terminal of the First Motor
GND (Pin 4)GND Pin of Arduino
GND (pin 5)GND Pin of Arduino
Output 2 (Pin6)The negative terminal of the First Motor
Input 2 (pin 7)Pin 8 of Arduino
Motor Supply (pin 8)The positive terminal of the 9V Battery
Enable Input3, Input4 (Pin 9)Pin 6 of Arduino
Input 3 (pin 10)Pin 12 of Arduino
Output 3 (pin 11)The negative terminal of the Second Motor
GND (pin 12)GND Pin of Arduino
GND (pin 13)GND Pin of Arduino
Output 4 (pin 14)The positive terminal of the Second Motor
Input 4 (Pin 15)Pin 4 of Arduino
IC Voltage Supply (Pin 16)VCC Pin (5V) of Arduino

Note

The Gnd pin of Arduino and Gnd of the 9V supply should be connected.

Arduino Code

/*
Controlling DC Motors with L293D Motor Driver IC and Arduino
by www.PlaywithCircuit.com 
*/
// Define the pins connected to the L293D IC
#define MOTOR_A_EN 11   // Enable pin for Motor A
#define MOTOR_B_EN 6    // Enable pin for Motor B
#define MOTOR_A_IN1 10  // Input 1 for Motor A
#define MOTOR_A_IN2 8   // Input 2 for Motor A
#define MOTOR_B_IN3 12  // Input 3 for Motor B
#define MOTOR_B_IN4 4   // Input 4 for Motor B

// Define the analog pin connected to the potentiometer
#define POT_PIN A0

void setup() {
  // Initialize motor control pins as outputs
  pinMode(MOTOR_A_EN, OUTPUT);
  pinMode(MOTOR_A_IN1, OUTPUT);
  pinMode(MOTOR_A_IN2, OUTPUT);
  pinMode(MOTOR_B_EN, OUTPUT);
  pinMode(MOTOR_B_IN3, OUTPUT);
  pinMode(MOTOR_B_IN4, OUTPUT);

  // Set initial motor speed to zero
  analogWrite(MOTOR_A_EN, 0);
  analogWrite(MOTOR_B_EN, 0);
}

void loop() {
  // Read the analog value from the potentiometer
  int potValue = analogRead(POT_PIN);

  // Map the potentiometer value (0-1023) to motor speed (0-255)
  int motorSpeed = map(potValue, 0, 1023, 0, 255);

  // Control Motor A
  digitalWrite(MOTOR_A_IN1, HIGH);
  digitalWrite(MOTOR_A_IN2, LOW);
  analogWrite(MOTOR_A_EN, motorSpeed);

  // Control Motor B
  digitalWrite(MOTOR_B_IN3, HIGH);
  digitalWrite(MOTOR_B_IN4, LOW);
  analogWrite(MOTOR_B_EN, motorSpeed);
}

Output:

Explanation of Code

Initially, all the MACROS of the pins are defined above the setup() function. In the setup() function, all the pins that are connected to IC are made Output pins. The voltage at the Enable pins which are working as PWM is set to 0 in below lines.

void setup() {
  // Initialize motor control pins as outputs
  pinMode(MOTOR_A_EN, OUTPUT);
  pinMode(MOTOR_A_IN1, OUTPUT);
  pinMode(MOTOR_A_IN2, OUTPUT);
  pinMode(MOTOR_B_EN, OUTPUT);
  pinMode(MOTOR_B_IN3, OUTPUT);
  pinMode(MOTOR_B_IN4, OUTPUT);

  // Set initial motor speed to zero
  analogWrite(MOTOR_A_EN, 0);
  analogWrite(MOTOR_B_EN, 0);
}

In the loop() function the Analog voltage at pin A0 is read and then mapped to the output of PWM pins using the below lines.

// Read the analog value from the potentiometer
int potValue = analogRead(POT_PIN);

// Map the potentiometer value (0-1023) to motor speed (0-255)
int motorSpeed = map(potValue, 0, 1023, 0, 255);

Next the direction control pins of both sides of the IC are used to control the direction of both the motors and the speed is controlled by the variable “motorSpeed” calculated above using the below code.

// Control Motor A
digitalWrite(MOTOR_A_IN1, HIGH);
digitalWrite(MOTOR_A_IN2, LOW);
analogWrite(MOTOR_A_EN, motorSpeed);

// Control Motor B
digitalWrite(MOTOR_B_IN3, HIGH);
digitalWrite(MOTOR_B_IN4, LOW);
analogWrite(MOTOR_B_EN, motorSpeed);

Project Demonstration