Driving DC Motors with L293D and Arduino

Driving DC Motors with L293D and Arduino

Summary

Are you looking to control a DC motor with your Arduino? The L293D motor driver IC can help! This blog covers everything you need to know about the L293D, including its pinout, how to wire it to your Arduino, and even provides example code to get you started. Whether you're a beginner or an experienced maker, this guide will help you achieve precise motor control in your projects.

Controlling a DC Motor

DC motors are widely used in many applications, from small hobby projects to industrial automation systems. They are simple to control and relatively inexpensive, making them a popular choice for many DC motor applications.

In this blog post, we will discuss how to control a DC motor using the L293D Motor Driver IC and an Arduino microcontroller.

The L293D motor IC can be used to control a DC motor by providing a variable amount of current to the motor's terminals and by reversing the direction of current flow through the motor's terminals.

The L293D has four input pins (IN1, IN2, IN3, IN4) and four output pins (OUT1, OUT2, OUT3, OUT4) that are used to control the direction and speed of the motor.

The L293D also contains two enable pins (EN1, EN2) which enable or disable the output driver. By providing a logically high or low signal to the enable pins, the L293D can turn the motor on and off.

It is important to note that the L293D requires a power supply with a voltage between 4.5V to 36V and it can drive a DC motor with a maximum current of up to 600mA.

L293D shield for Arduino UNO  

PWM – to control speed

Pulse Width Modulation (PWM) is a technique used to control the speed of a DC motor. It works by rapidly switching the power to the motor on and off, in a specific pattern.

The more often the power is switched on, the faster the motor will rotate. The Arduino can generate PWM signals on its digital output pins, which can be used to control the speed of the motor.

To control the speed of the motor, the L293D uses a PWM (pulse width modulation) signal on one of the input pins (IN3 or IN4) and one of the output pins (OUT3 or OUT4).

This PWM signal controls the amount of time that the output pin is at a logically high level, which controls the amount of current that is sent to the motor.

H-Bridge – to control the rotation direction

An H-Bridge is a type of circuit that is commonly used to control the direction of rotation of a DC motor. The L293D is a popular H-Bridge IC (integrated circuit) that is commonly used in robotics and other projects that involve controlling DC motors.

The L293D can control the direction of rotation of a DC motor by sending current in either direction through the motor's terminals. The L293D can also control the speed of the motor by adjusting the amount of current that is sent through the motor's terminals.

Also, read our blog on DC Motor Applications explaining DC motor applications in different industries, products, and types of DC motors.

L293D Motor Driver IC

The L293D Motor Driver IC is a popular choice for controlling DC motors. It is a dual H-bridge motor driver IC, which means it can control the speed and direction of two DC motors at the same time.

The L293D motor driver IC is designed to handle up to 600mA of current per motor, which is enough for most small to medium-sized motors.

Technical Specifications

  • Supply Voltage: 4.5V to 36V
  • Output Current: 600mA per channel
  • Protection Diodes: Included
  • Control Pins: 4
  • Output Pins: 2 per channel

L293D Motor Driver IC Pinout 

 

Power Pins: The L293D motor driver IC has two power pins, VCC1 and VCC2. VCC1 is used to supply power to the control logic of the IC, while VCC2 is used to supply power to the motors.

Output Pins: The L293D motor driver IC has four output pins, two per channel. These pins are used to connect the motors to the IC.

Direction Control Pins: The L293D motor driver IC has two direction control pins, one per channel. These pins are used to control the direction of rotation of the motors.

Speed Control Pins: The L293D motor driver IC has two-speed control pins, one per channel. These pins are used to control the speed of the motors by applying a PWM signal.

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);

}

Conclusion

Using an L293D motor driver IC with an Arduino is an excellent way to achieve precise DC motor control in your projects. By following the steps outlined in this blog, you can easily wire the L293D to your Arduino and get started with controlling your motor. Whether you're an experienced maker or a beginner, this guide has everything you need to succeed. So why wait? Start controlling your DC motor today! 

 

 

If you appreciate our work don't forget to share this post and leave your opinion in the comment box.

 

Please do check out other blog posts about  DC Motor Applications and Speed Control of DC Motor 

 

Make sure you check out our wide range of products and collections (we offer some exciting deals!)

Components and Supplies

You may also like to read

Frequently Asked Questions

1. What is DC motor and how it works?

A DC motor is an electrical machine that transforms electrical energy from direct current into mechanical energy. Its operation is based on the principle of electromagnetic induction, whereby current flowing through a wire induces a magnetic field that attracts or repels a magnetic object. The DC motor consists of four basic components: a stator, rotor, commutator, and brushes. The stator generates a fixed magnetic field, while the rotor rotates within this field. By means of the commutator and brushes, the current flow direction in the rotor can be switched, resulting in its rotation. DC motors find widespread application in a variety of fields, including industrial machinery and electric vehicle propulsion.

2. What is a L293D motor driver?

The L293D is an integrated circuit used for driving motors, capable of controlling the speed and direction of DC motors or a single stepper motor. It is a type of dual-channel H-bridge driver, which means it can drive two motors simultaneously in either direction. The L293D can deliver bidirectional drive currents up to 600 mA, operating within a voltage range of 4.5 V to 36 V. This motor driver is commonly utilized in robotics and other motor control applications.

3. How to control a DC motor with a L293D?

In order to control a DC motor using an L293D, one can make use of an Arduino board by connecting the IC's 5V and ground pins to the Arduino's 5V and ground pins respectively. Pins 2 and 3 of the L293D can be connected to the motor, while IN1 of the IC can be connected to pin 8 of the Arduino. The L293D is a dual channel H-Bridge IC, which has the ability to control two DC motors with voltage ratings between 4.5V to 36V. It can also manipulate the speed and direction of the motor. Several online tutorials provide step-by-step instructions on how to control a DC motor with an L293D using an Arduino.

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.

Components and Supplies

You may also like to read