Arduino L293D Control of DC Motor:


In case you want to build a robot with the help of DC motors, it is possible that sooner or later you will know that if you want your robot to move in a particular direction then you need to control both speed and direction of its motor. To do this, one can use an L293D motor driver IC as it is inexpensive, easy to use and by using PWM support can control both speed and direction. That’s why this tutorial will be based on a widely used L293D motor driver IC for simple speed and direction control of the motor. Our previous projects included RF Controlled Robot,  Automatic Door Opener using Arduino, Interfacing Stepper Motor with AVR Microcontroller Atmega16 etc.


DC Motor Control Through Microcontroller:


In order for us to have full control over the motor we need to regulate its speed as well as direction, whereby we are going to apply Pulse Width Modulation (PWM) Technique in order to vary the speeds and internal H-bridge will determine the way our motor should rotate.


Speed Control by PWM – DC Motor :


To change the speed of any Direct Current (DC) machine which could either be operating as a generator or otherwise just functioning as a motor there is an immediate requirement by any designer or user. One popular method used is known as pulse width modulation (PWM). These pulses are adjusted so that the output voltage changes as their width changes according to the pulse width generated by the microcontroller, also recognized as duty cycle.


Higher duty cycles cause higher average applied voltage across the dc motor resulting in high speed whereas lower duty cycles bring about lower average applied voltage across the dc motor leading to low speed.


Direction Control through H-Bridge motor


Unidirectional Movements: Changing Polarity of Supply – When we would like to change direction of rotation for any direct current machinery we must vary polarity at its supply. The best practice in such situations involves application of H-bridge motor drivers. This is an assembly of four switches (often MOSFETs) which are set in particular orientation with a motor connected in the middle of these to resemble structures like “H”.


The direction of rotation can be changed by closing or enabling two opposite switches.


L293D Motor Driver IC


L293D is a dual channel motor driver IC capable of running two DC motors with bidirectional control and a single stepper motor. L293D devices are quadruple high-current half-H drivers. These ICs are designed to drive loads such as relays, solenoids, DC and bipolar stepping motors also. The L293D is specified for operation from –40°C to 85°C.


L293D is typically employed in motor driving applications, but thanks to its totem pole output structure, it is also used for driving solenoids, four directional DC motors, two bidirectional DC motors or one stepper motor. To get more information about this IC, check the datasheet of L293D IC. For further study on Motor driver ICs and L293D look here.


Note: The L293 and L293D are not the same; the L293 IC does not have flyback diodes built-in so if you are using the L293 version, you will need to add those in your circuit. All the flyback diodes have been connected internally in the L293D version hence there is no need of connecting external diodes for the L293D IC.


L293D Motor Driver IC Pinout:


Look at the pin diagram below to see how pins from microcontrollers can be connected to this motor driver IC having 16 pins.


ENA, ENB These are ENA and ENB pins which are pin1 and pin9 of this device respectively. When pulled high enables the motor and it starts spinning. When pulled low it stops rotating. The speed of a DC motor can be controlled by applying PWM signals on these pins.


IN1 IN2 IN3 IN4 For each channel two direction control pins exist.The spinning direction of motor A is defined by IN1 &IN2 while that of B by IN3 &IN4.


OUT1 OUT2 OUT3 OUT4 This particular ic has got four output channels i.e.OUT1 &OUT2, which controls a single motor whereas OUT3&OUT4 controls another motor . In other words with this if you can drive two 5 – 36V dc motors respectively . Besides each channel on the ic has a capacity of carrying up to 600mA.


GND Pins 4 ,5 and 12,13 are the ground pins of the device. This pin doesn’t only provide a ground for the IC but it also acts as a heat sink to the IC, if your design includes this IC on board consider using design recommendations provided in its datasheet.


VCC1 The VCC1 pin is used to power the internal motor driver of the ic. You can connect VCC1pin from 4.5V to 36V supply.


VCC2 To interface with the internal logic circuitry of this IC use this pin and apply 5V.


L293D Motor Driver IC Frequently Asked Questions


Why do we use L293D?


They are designed for bidirectional drive currents of up to 600-mA at voltages from 4.5 V to 36 V that is why they both drives inductive loads like relays,solenoids,dc and bipolar stepping motors along with other high current / high voltage load applications in positive supply configurations.


How does an L293D work?


The microprocessor sends signals to the motor through L293D I.C; It has two voltage pins one being currently consumed by L293D and other applied on motor terminals.


Can I connect four motors in l293d?


It can drive either 4 solenoids, 4 unidirectional DC motors, 2 bi-directional DC motors or one stepper motor. It operates well with most of our motors because the L293D IC has a supply range of between 4.5V to 36V and can output up to about 1.2A per channel at peak.


How many wheels does the L293D have?


The L293D is a 16-pin Motor Driver IC that permits control of two DC motors simultaneously in any direction.


Does L293D control speed?


The IC does not have any provision for controlling the speed. Use PWM from a micro-controller or another source on the enable pin of the IC to regulate motor speed.


Arduino and L293D Circuit Diagram:


Having grasped how exactly this motor driver works we can now proceed to connect all necessary wires in our case between an Arduino and an L293d as well as write some code that rotates the motor and controls its speed. The following are circuit diagrams of DC Motor with Arduino:


In our circuit, we will use a small 3V DC gear motor hence we will feed it power from an external power supply rated at 5V through the VCC1 pin of the IC. The internal logic circuitry is powered via pin VCC2 which should be connected to a digital pin on an arduino board set HIGH (i.e., to +5 volts). Afterward, In1, In2 pins (pin9 and pin3) are connected upon ENA and ENB input pins respectively because these two pins support Pulse Width Modulation (PWM) i.e. PIN#9 AND PIN#3 CAN BE CONTROLLED THROUGH ARDUINO FOR VARIABLE PULSE WIDTHS TO CONTROL THE SPEED OF THE MOTOR being driven using arduino boards .Finally ,the OUT1, OUT2, OUT3,and OUT4 pins are connected from the shield into the motor. Once you are done, It should look like this picture below but please note that we have just connected a single motor to the IC for simplicity sake.


Arduino Code for Interfacing L293D Motor Driver IC with Arduino:


The following sketch will give you a complete understanding on how to control the speed and direction of a DC motor with L293D IC.


The code is quite straightforward for Arduino; first, we defined all the pins connected to Arduino.



// Electro Global- Innovate with us


// Define motor pins

#define MOTOR_A_EN 9

#define MOTOR_A_IN1 8

#define MOTOR_A_IN2 7

#define MOTOR_B_EN 10

#define MOTOR_B_IN1 11

#define MOTOR_B_IN2 12


void setup() {

  // Initialize motor 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_IN1, OUTPUT);

  pinMode(MOTOR_B_IN2, OUTPUT);



void loop() {

  // Move motor A forward

  digitalWrite(MOTOR_A_IN1, HIGH);

  digitalWrite(MOTOR_A_IN2, LOW);

  analogWrite(MOTOR_A_EN, 255); // Adjust speed by changing PWM value (0-255)


  // Move motor B forward

  digitalWrite(MOTOR_B_IN1, HIGH);

  digitalWrite(MOTOR_B_IN2, LOW);

  analogWrite(MOTOR_B_EN, 255); // Adjust speed by changing PWM value (0-255)


  delay(2000); // Move forward for 2 seconds


  // Stop motors

  digitalWrite(MOTOR_A_EN, LOW);

  digitalWrite(MOTOR_B_EN, LOW);


  delay(1000); // Pause for 1 second


  // Move motor A backward

  digitalWrite(MOTOR_A_IN1, LOW);

  digitalWrite(MOTOR_A_IN2, HIGH);

  analogWrite(MOTOR_A_EN, 255); // Adjust speed by changing PWM value (0-255)


  // Move motor B backward

  digitalWrite(MOTOR_B_IN1, LOW);

  digitalWrite(MOTOR_B_IN2, HIGH);

  analogWrite(MOTOR_B_EN, 255); // Adjust speed by changing PWM value (0-255)


  delay(2000); // Move backward for 2 seconds


  // Stop motors

  digitalWrite(MOTOR_A_EN, LOW);

  digitalWrite(MOTOR_B_EN, LOW);


  delay(1000); // Pause for 1 second



Arduino working with L293D Motor Driver IC:


This GIF is to show you how the hardware circuit works. We ran a code where the motor would change direction after some delay, then the speed of the motor would display PWM functionality.


Having problems with L293D interfacing Arduino? Here’s what you can do.


The most common problem that one may face is power issues. The operating voltage range for the L293D motor driver is 4.5V-36V. Any voltage below this will not work and any voltage above 36V will burn up the device.


Another common fault found in L293D is that when you power up your L293D IC, it starts rotating the motor or IC heats up to exponential value; it indicates that IC has been damaged because of which you need to replace it now immediately.


Logic pin of L293D is +5V tolerant so more than 5V voltage can harm it.


When using an L293D IC, always consider winding resistance of motors, if motor winding resistance is too low then it requires more current as rated for which can permanently damage this device.


Si prega di attivare i Javascript! / Please turn on Javascript!

Javaskripta ko calu karem! / Bitte schalten Sie Javascript!

S'il vous plaît activer Javascript! / Por favor, active Javascript!

Qing dakai JavaScript! / Qing dakai JavaScript!

Пожалуйста включите JavaScript! / Silakan aktifkan Javascript!