Lesson 50: Control Direction of rotation of motor using Arduino and L293D
Part 5: DC Motors
In this lesson we learn how to use L293D Motor driver to drive a DC motor using Arduino code to control direction of rotation of a DC motor. Please watch the video for full details.
- Introduction to L293D chip
- Wiring of L293D Explained
- Code Explained
- Running code demonstration

/*
* Lesson 50: Controlling Direction of Rotation of Motor | Arduino Step By Step Course
* This is the Arduino code L293 DC motor DRIVEER
Using this code you can control a motor to rotate in both directions
Please watch the video for full details of this code and project:
https://youtu.be/-Ss67lqrtL8
* *
// Writeen by Ahmad Shamshiri for Robojax.com on
// on Jan 26, 2019 at 08:01 in Ajax, Ontario, Canada
This code is available at http://robojax.com/course1/?vid=lecture50
with over 100 lectures Free On YouTube Watch it here http://robojax.com/L/?id=338
Get the code for the course: http://robojax.com/L/?id=339
* on Saturday November 24, 2018
* in Ajax, Ontario, Canada
*
* This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#define Pin1A 2 // define DRIVE 1A pin
#define Pin2A 4 // define DRIVE 1Y pin
#define EN12 3 // define pin 3 for EN1, 2 (must be PWM pin)
void setup() {
// L293 Motor Control Code by Robojax.com 2018025
Serial.begin(9600);// setup Serial Monitor to display information
pinMode(Pin1A, OUTPUT);// define pin as OUTPUT for Pin1A
pinMode(Pin2A, OUTPUT);// define pin as OUTPUT for Pin2A
pinMode(EN12, OUTPUT);// define pin as OUTPUT for EN12
// L293 Motor Control Code by Robojax.com 2018025
}
void loop() {
// L293 Motor Control Code by Robojax.com 2018025
// DRIVE motor at maximum speed in CCW direction
digitalWrite(Pin1A,HIGH);// for CCW
digitalWrite(Pin2A,LOW);// for CCW
analogWrite(EN12, 255);// run motor at (255 PWM value) maximum speed
Serial.println(" CCW at 255");
delay(4000);//for 4 seconds
// stop motor
analogWrite(EN12, 0);// stop motor
Serial.println(" Stop motor 1");
delay(3000);//for 3 seconds
// DRIVE motor at half speed in CW direction
digitalWrite(Pin1A,LOW);// for CW
digitalWrite(Pin2A,HIGH);// for CW
analogWrite(EN12, 125);// run motor at (125 PWM value) speed
Serial.println(" CW at 125");
delay(4000);//for 4 seconds
// stop motor
analogWrite(EN12, 0);// stop motor
Serial.println(" Stop motor 1");
delay(3000);//for 3 seconds
// DRIVE motor at half speed in CW direction
digitalWrite(Pin1A,HIGH);// for CW
digitalWrite(Pin2A,LOW);// for CW
analogWrite(EN12, 125);// run motor at (125 PWM value) speed
Serial.println(" CCW at 125");
delay(4000);//for 4 seconds
// stop motor
analogWrite(EN12, 0);// stop motor
Serial.println(" Stop motor 1");
delay(4000);//for 4 seconds
for(int i=125; i <=255; i +=20)
{
digitalWrite(Pin1A,HIGH);// for CCW
digitalWrite(Pin2A,LOW);// for CCW
analogWrite(EN12, i);// send i value of PWM to motor
Serial.print(" CCW at ");
Serial.println(i);
delay(500);//for 4 seconds
}
delay(4000);// wait for 4 seconds
// stop motor
analogWrite(EN12, 0);// stop motor
Serial.println(" Stop motor 1");
delay(4000);//for 4 seconds
for(int i=255; i >100; i -=20)
{
digitalWrite(Pin1A,LOW);//
digitalWrite(Pin2A,HIGH);//
analogWrite(EN12, i);// send i value of PWM to motor
Serial.print(" CW at ");
Serial.println(i);
delay(500);//for 4 seconds
}
delay(4000);// wait for 4 seconds
Serial.println("====== Loop done");
// L293 Motor Control Code by Robojax.com 2018025
}// loop end