Controlling DC Motor with L293D Driver IC with Arduino

1,255 views

Introduction

DC motors are widely used in various applications such as robotics, automation, and control systems. However, controlling the speed and direction of a DC motor can be challenging without the right equipment. This is where the L293D driver IC comes into play.

In this article, we will explore how to use an Arduino and an “L293D driver IC” to control a DC motor. With this setup, you can easily control the speed and direction of your motor with just a few lines of code.

What is L293D Driver IC?

The L293D is a popular dual H-bridge motor driver IC, commonly used to control the speed and direction of DC motors. It is designed to provide bidirectional drive currents up to 600mA per channel, with peak currents up to 1.2A. The IC contains two H-bridge circuits, which means it can control two motors independently or one stepper motor.

Hardware Components

To interface a DC Motor with L293D Driver IC with Arduino, you’ll need the following hardware components to get started:

ComponentsValueQty
Arduino UNO1
USB Cable Type A to B1
DC Power for Arduino1
Motor Driver ICL293D1
Battery5V1
DC Motor2
Jumper Wires1

L293D Driver IC Pinout

Pin NumberPin NameDescription
1Enable 1,2This pin enables the input pin Input 1(2) and Input 2(7)
2Input 1Directly controls the Output 1 pin. Controlled by digital circuits
3Output 1Connected to one end of  Motor 1
4GroundGround pins are connected to the ground of circuit (0V)
5GroundGround pins are connected to the ground of the circuit (0V)
6Output 2Connected to another end of  Motor 1
7Input 2Directly controls the Output 2 pin. Controlled by digital circuits
8Vcc2 (Vs)Connected to Voltage pin for running motors (4.5V to 36V)
9Enable 3,4This pin enables the input pin Input 3(10) and Input 4(15)
10Input 3Directly controls the Output 3 pin. Controlled by digital circuits
11Output 3Connected to one end of Motor 2
12GroundGround pins are connected to the ground of circuit (0V)
13GroundGround pins are connected to the ground of the circuit (0V)
14Output 4Connected to another end of Motor 2
15Input 4Directly controls the Output 4 pin. Controlled by digital circuits
16Vcc2 (Vss)Connected to +5V to enable IC function

DC Motor with L293D Driver IC Circuit

Make connections according to the circuit diagram given below.

Installing Arduino IDE

First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.

Code

Now copy the following code and upload it to Arduino IDE Software.

//Motor_1 Connection
#define ENABLE_1    11
#define MOTOR_1_A   10
#define MOTOR_1_B   9
 
//Motor_2 Connection
#define ENABLE_2    5
#define MOTOR_2_A   4
#define MOTOR_2_B   3
 
//other variables
char serial_data;
int speed_value_m1;
int speed_value_m2;
 
void setup() {
  Serial.begin(9600);
  Serial.println("Motor Init..");
  motor_1_init();
  motor_2_init();
  speed_value_m1 = 0;
  speed_value_m2 = 0;
}
 
void loop() {
  motor_speed_dir_control();
}
 
void motor_1_init()
{
  pinMode(MOTOR_1_A, OUTPUT);
  pinMode(MOTOR_1_B, OUTPUT);
  pinMode(ENABLE_1, OUTPUT);
 
  digitalWrite(MOTOR_1_A, LOW);
  digitalWrite(MOTOR_1_B, LOW);
  analogWrite(ENABLE_1, LOW);
}
 
void motor_2_init()
{
  pinMode(MOTOR_2_A, OUTPUT);
  pinMode(MOTOR_2_B, OUTPUT);
  pinMode(ENABLE_2, OUTPUT);
 
  digitalWrite(MOTOR_2_A, LOW);
  digitalWrite(MOTOR_2_B, LOW);
  analogWrite(ENABLE_2, LOW);
}
void motor_speed_dir_control()
{
while (Serial.available())
{
  serial_data = Serial.read();
     switch (serial_data)
	{
  	case 's':
        	analogWrite(ENABLE_1, 255);
        	Serial.println("Enable Motor_1");
    		break;
  	case 'S':
 	analogWrite(ENABLE_2, 255);
       	Serial.println("Enable Motor_2");
    		break;
 
  	case 'h':
        	analogWrite(ENABLE_1, 0);
        	digitalWrite(MOTOR_1_A, LOW);
        	digitalWrite(MOTOR_1_B, LOW);
        	speed_value_m1 = 0;
    	Serial.println("Stop Motor_1");
    		break;
  	case 'H':
        analogWrite(ENABLE_2, 0);
        digitalWrite(MOTOR_2_A, LOW);
        digitalWrite(MOTOR_2_B, LOW);
        speed_value_m2 = 0;
        Serial.println("Stop Motor_2");
    		break;
 
  	case 'f':
digitalWrite(MOTOR_1_A, HIGH);
digitalWrite(MOTOR_1_B, LOW);
Serial.println("Motor_1 Forward Direction");
    		break;
  	case 'F':
       	 	digitalWrite(MOTOR_2_A, HIGH);
        		digitalWrite(MOTOR_2_B, LOW);
    		Serial.println("Motor_2 Forward Direction");
    		break;
 
  	case 'b':
        	digitalWrite(MOTOR_1_A, LOW);
        	digitalWrite(MOTOR_1_B, HIGH);
        	Serial.println("Motor_1 Backward Direction");
    		break;
  	case 'B':
    	digitalWrite(MOTOR_2_A, LOW);
        	digitalWrite(MOTOR_2_B, HIGH);
        	Serial.println("Motor_2 Backward Direction");
    		break;
 
  	case 'a':
    	for (int i = 0; i < 256; i++)
    	{
          		analogWrite(ENABLE_1, i);
    	}
        		Serial.println("Motor_1 acceleration");
    		break;
  	case 'A':
    	for (int i = 0; i < 256; i++)
    	{
          		analogWrite(ENABLE_2, i);
    	}
        		Serial.println("Motor_2 acceleration");
    		break;
  	case 'd':
        for (int i = 255; i > 1; i--)
    	{
          		analogWrite(ENABLE_1, i);
      		delay(5);
    	}
        		Serial.println("Motor_1 deceleration");
    		break;
  	case 'D':
    	for (int i = 255; i > 1; i--)
    	{
      		analogWrite(ENABLE_2, i);
      		delay(5);
    	}
       		Serial.println("Motor_2 deceleration");
    		break;
  	case 'i':
    	if (speed_value_m1 <= 245)
    	{
          speed_value_m1 += 10;
          analogWrite(ENABLE_1, speed_value_m1);
          Serial.println("Motor_1 increased Speed value: ");
          Serial.print(speed_value_m1);
    	}
    	else
    	{
          Serial.println("Maximum Speed Limit Reached for Motor_1: ");
          Serial.print(speed_value_m1);
    	}
    		break;
  	case 'I':
    	if (speed_value_m2 <= 245)
    	{
          speed_value_m2 += 10;
          analogWrite(ENABLE_2, speed_value_m2);
          Serial.println("Motor_2 increased Speed value: ");
          Serial.print(speed_value_m2);
    	}
    	else
    	{
          Serial.println("Maximum Speed Limit Reached for Motor_2: ");
          Serial.print(speed_value_m2);
    	}
    		break;
 
  	case 'r':
    	if (speed_value_m1 >= 10)
    	{
          speed_value_m1 -= 10;
          analogWrite(ENABLE_1, speed_value_m1);
          Serial.println("Motor_1 reduced Speed value: ");
          Serial.print(speed_value_m1);
    	}
    	else
    	{
          		Serial.println("Minimum Speed Limit Reached for Motor_1: ");
          		Serial.print(speed_value_m1);
    	}
    		break;
  	case 'R':
    	if (speed_value_m2 >= 10)
    	{
          		speed_value_m2 -= 10;
          		analogWrite(ENABLE_2, speed_value_m2);
      		Serial.println("Motor_2 reduced Speed value: ");
          		Serial.print(speed_value_m2);
    	}
    	else
    	{
          		Serial.println("Minimum Speed Limit Reached for Motor_2: ");
          		Serial.print(speed_value_m2);
    	}
    	break;
 
  	case 'X':
        	digitalWrite(MOTOR_1_A, HIGH);
       	 digitalWrite(MOTOR_1_B, LOW);
       	 digitalWrite(MOTOR_2_A, HIGH);
        	digitalWrite(MOTOR_2_B, LOW);
        	analogWrite(ENABLE_1, 255);
        	analogWrite(ENABLE_2, 255);
    	Serial.println("Motor_1 and Motor_2 Forward Direction");
    		break;
    	
  	case 'Y':
        	digitalWrite(MOTOR_1_A, LOW);
        	digitalWrite(MOTOR_1_B, HIGH);
        	digitalWrite(MOTOR_2_A, LOW);
        	digitalWrite(MOTOR_2_B, HIGH);
        	analogWrite(ENABLE_1, 255);
        	analogWrite(ENABLE_2, 255);
        	Serial.println("Motor_1 and Motor_2 Backward Direction");
        	break;
    	
  	case 'Z':
        	digitalWrite(MOTOR_1_A, LOW);
        	digitalWrite(MOTOR_1_B, LOW);
        	digitalWrite(MOTOR_2_A, LOW);
        	digitalWrite(MOTOR_2_B, LOW);
        	analogWrite(ENABLE_1, 0);
        	      	analogWrite(ENABLE_2, 0);
       	      	Serial.println("Motor_1 and Motor_2 Stop");
    	      	break;
  	default:
    		break;
	}
     }
}

Code Explanation

This Arduino code is a program to control the speed and direction of two DC motors using an L293D motor driver IC. The L293D driver IC provides an H-bridge configuration to control the motor’s direction and speed. The code uses the serial monitor to receive commands from the user and then controls the motors accordingly.

The program begins with the initialization of the motor driver and variables. In the main loop, the motor_speed_dir_control() function is called. The motor_speed_dir_control() function checks for input from the serial monitor and then performs the appropriate action.

The serial monitor commands are as follows:

  • s: Enable Motor_1 in the forward direction with maximum speed.
  • S: Enable Motor_2 in the forward direction with maximum speed.
  • h: Stop Motor_1.
  • H: Stop Motor_2.
  • f: Set Motor_1 to move in the forward direction.
  • F: Set Motor_2 to move in the forward direction.
  • b: Set Motor_1 to move in the backward direction.
  • B: Set Motor_2 to move in the backward direction.
  • a: Accelerate Motor_1 until maximum speed is reached.
  • A: Accelerate Motor_2 until maximum speed is reached.
  • d: Decelerate Motor_1.
  • D: Decelerate Motor_2.
  • i: Increase the speed of Motor_1 by 10 units.
  • I: Increase the speed of Motor_2 by 10 units.
  • r: Reduce the speed of Motor_1 by 10 units.
  • R: Reduce the speed of Motor_2 by 10 units.

The program uses PWM to control the speed of the motors by changing the duty cycle of the PWM signal. The program uses the analogWrite() function to write to the PWM pins on the Arduino board.

In summary, this program provides a simple way to control two DC motors using an L293D motor driver IC and an Arduino board.

Application

Here are some applications of the L293D driver IC:

  • Controlling DC motors: L293D is a popular choice for driving DC motors in robotics and automation projects. It can drive two DC motors simultaneously in either direction and provides a high current capacity of up to 600 mA per channel.
  • Controlling stepper motors: L293D can also be used to drive stepper motors, which are often used in applications such as CNC machines, 3D printers, and robotics.
  • Controlling servo motors: L293D can be used to control servo motors, which are commonly used in RC models and robotic arms.
  • H-Bridge applications: L293D is an H-Bridge driver IC, which means it can be used in applications where bidirectional control of a motor or load is required. Examples of such applications include motorized gates, blinds, and curtains.
  • Control of solenoids: L293D can be used to drive solenoids, which are used in a variety of applications such as door locks, pneumatic and hydraulic valves, and industrial automation.
  • LED driving: L293D can also be used to drive high-power LEDs, providing a constant current source to drive the LEDs with low-power dissipation.
  • Other applications: L293D can be used in other applications, such as driving fans, buzzers, and relays.

Conclusion

Using an L293D driver IC with an Arduino is a cost-effective and straightforward way to control the speed and direction of your DC motor. With this setup, you can easily integrate your motor into your projects, whether you’re building a robot, an automation system, or a control system.