Back to Arduino Step By Step Course
If you don't like to see ads while video is being played you can purchase this course for $200 from Udemy or purchase YouTube premium Here
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.
/*
* 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
The least I expect from you is to thumb up the video and subscribe to my channel. I appriciate that. .I have spent months making these lectures and writing code. You don't lose anything by subscribging to my channel. Your subscription is stamp of approval to my videos and more people can find them and in it turn it helps me. Thank you
If you found this tutorial helpful, please support me so I can continue creating content like this. support me via PayPal
**** AFFILIATE PROGRAM **** We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to Amazon.com and affiliated sites.