Driving DC Motors with L293D and Arduino

Motor_driver_blog

Summary

Want to control a DC motor using an Arduino? The L293D motor driver IC makes it easy. This guide explains everything you need to know about the L293D, including its pin configuration, how to connect it to an Arduino, and sample code to help you get started. Whether you’re just starting out or already experienced with electronics, this tutorial will help you achieve accurate and reliable motor control in your projects.

Controlling a DC Motor

DC motors are widely used across many applications, ranging from basic hobby builds to advanced industrial systems. Their affordability and ease of control make them a preferred option in many electronic designs.

L293D Motor Driver IC

The L293D motor driver IC is a commonly used component for driving DC motors. It is a dual H-bridge driver, allowing simultaneous control of both speed and direction for two DC motors.

Each motor channel can handle currents of up to 600 mA, making the L293D suitable for most small to medium DC motors.

Technical Specifications :

Operating Voltage: 4.5V to 36V

Output Current: 600 mA per channel

Built-in Protection Diodes: Yes

Control Pins: 4

Output Pins: 2 per channel

L293D Motor Driver IC Pinout

Power Pins:
The L293D includes two power supply pins: VCC1 and VCC2. VCC1 powers the internal logic circuitry, while VCC2 supplies power directly to the motors.

Output Pins:
There are four output pins in total, with two assigned to each motor channel. These pins connect directly to the motor terminals.

Direction Control Pins:
Each motor channel has two direction control pins. These pins determine the rotation direction of the motor by controlling the current flow.

Speed Control Pins:
The IC features two enable (speed control) pins—one per channel. By applying a PWM signal to these pins, motor speed can be precisely controlled.

Motor Control Using Arduino

Using an Arduino with the L293D motor driver IC allows efficient control of DC motors. The IC manages motor operation by adjusting current flow and reversing polarity to achieve forward and reverse motion.

It provides four input pins (IN1, IN2, IN3, IN4) and four corresponding output pins (OUT1, OUT2, OUT3, OUT4) to manage motor speed and direction.

Additionally, the enable pins (EN1 and EN2) activate or deactivate the motor outputs. Setting these pins HIGH turns the motors on, while setting them LOW stops them.

The L293D operates within a voltage range of 4.5V to 36V and supports DC motors drawing up to 600 mA per channel.

Wiring an L293D Motor Driver IC to an Arduino

To wire an L293D motor driver IC to an Arduino, you will need to connect the power pins of the IC to the power supply and connect the control pins of the IC to the digital output pins of the Arduino. The following diagram shows an example of how to wire an L293D motor driver IC to an Arduino:

Arduino Example Code

The following example code can be used to control the speed and direction of a DC motor using the L293D motor driver IC and an Arduino:

 
// Motor A connections

int enA = 9;

int in1 = 8;

int in2 = 7;

// Motor B connections

int enB = 3;

int in3 = 5;

int in4 = 4;

void setup() {

// Set all the motor control pins to outputs

pinMode(enA, OUTPUT);

pinMode(enB, OUTPUT);

pinMode(in1, OUTPUT);

pinMode(in2, OUTPUT);

pinMode(in3, OUTPUT);

pinMode(in4, OUTPUT);

// Turn off motors - Initial state

digitalWrite(in1, LOW);

digitalWrite(in2, LOW);

digitalWrite(in3, LOW);

digitalWrite(in4, LOW);

}

void loop() {

directionControl();

delay(1000);

speedControl();

delay(1000);

}

// This function lets you control the spinning direction of motors

void directionControl() {

// Set motors to maximum speed

// For PWM maximum possible values are 0 to 255

analogWrite(enA, 255);

analogWrite(enB, 255);

// Turn on motor A & B

digitalWrite(in1, HIGH);

digitalWrite(in2, LOW);

digitalWrite(in3, HIGH);

digitalWrite(in4, LOW);

delay(2000);

// Now change motor directions

digitalWrite(in1, LOW);

digitalWrite(in2, HIGH);

digitalWrite(in3, LOW);

digitalWrite(in4, HIGH);

delay(2000);

// Turn off motors

digitalWrite(in1, LOW);

digitalWrite(in2, LOW);

digitalWrite(in3, LOW);

digitalWrite(in4, LOW);

}

// This function lets you control speed of the motors

void speedControl() {

// Turn on motors

digitalWrite(in1, LOW);

digitalWrite(in2, HIGH);

digitalWrite(in3, LOW);

digitalWrite(in4, HIGH);

// Accelerate from zero to maximum speed

for (int i = 0; i < 256; i++) {

analogWrite(enA, i);

analogWrite(enB, i);

delay(20);

}

// Decelerate from maximum speed to zero

for (int i = 255; i >= 0; --i) {

analogWrite(enA, i);

analogWrite(enB, i);

delay(20);

}

// Now turn off motors

digitalWrite(in1, LOW);

digitalWrite(in2, LOW);

digitalWrite(in3, LOW);

digitalWrite(in4, LOW);

}
Back to blog