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 2 DC motors using Arduino code to contorl motor in both direction. Please watch the video for full details.
/*
* Lesson 51: Controlling 2 DC motor using L293D and Arduino | Arduino Step By Step Course
* This is the Arduino code L293 DC motor DRIVEER
Using this code you can control 2 motors to rotate in both directions
Please watch the video for full details of this code and project:
https://youtu.be/LkJRakZBChI
* *
// 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=lecture51
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 Pin1A 1A pin
#define Pin2A 4 // define Pin2A 2A pin
#define EN12 3 // define pin 3 for EN1, 2 (must be PWM pin)
#define Pin3A 8 // define Pin3A 3A pin
#define Pin4A 9 // define Pin4A 4A pin
#define EN34 10 // define pin 10 for EN3,4 (must be PWM pin)
const int CCW =1; // defining CCW variable
const int CW =2; // defining CW variable
void setup() {
// L293 Motor Control Code
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
pinMode(Pin3A, OUTPUT);// define pin as OUTPUT for Pin3A
pinMode(Pin4A, OUTPUT);// define pin as OUTPUT for Pin4A
pinMode(EN34, OUTPUT);// define pin as OUTPUT for EN34
// L293 Motor Control Code
}
void loop() {
// L293 Motor Control Code
motor(1,255,CW);// motor 1 CW at 255
delay(5000);// wait for 5 seconds
motor(2,170,CCW);// motor 2 CCW at 150
delay(5000);// wait for 5 seconds
motor(1,0, CCW);// stop 1 motor
delay(5000);// wait for 5 seconds
motor(1,170,CCW);// motor 1 CCW at 255
delay(5000);// wait for 5 seconds
motor(2,255,CW);// motor 2 CW at 255
delay(5000);// wait for 5 seconds
motor(2,0, CCW);// stop 2 motor
delay(5000);// wait for 5 seconds
Serial.println("====== Loop done");
// L293 Motor Control Code
}// loop end
/*
* drives L293D motor drivers
* @param motor is motor number (1 or 2)
* @param pwm is the PWM value 0 to 255
* @param dir is the direction (CW or CCW)
*/
void motor(int motor, int pwm, int dir)
{
int drive1Pin, drive2Pin, driveENPin;
String motorTitle;// define motor text
if(motor ==1){
drive1Pin = Pin1A;
drive2Pin = Pin2A;
driveENPin = EN12;
motorTitle ="Motor 1";
}else{
drive1Pin = Pin3A;
drive2Pin = Pin4A;
driveENPin = EN34;
motorTitle ="Motor 2";
}
if(dir ==1){
digitalWrite(drive1Pin,LOW);//LOW signal
digitalWrite(drive2Pin,HIGH);//HIGH signal
if(pwm ==0){
Serial.print(motorTitle);
Serial.println(" Stopped");
}else{
Serial.print(motorTitle);
Serial.print(" Rotating CCW: ");
Serial.println(pwm);//print actual speed value
}
}else{
digitalWrite(drive1Pin,HIGH);//HIGH signal
digitalWrite(drive2Pin,LOW);//LOW signal
if(pwm ==0){
Serial.print(motorTitle);
Serial.println(" Stopped");
}else{
Serial.print(motorTitle);
Serial.print(" Rotating CW: ");
Serial.println(pwm);//print actual speed value
}
}
analogWrite(driveENPin ,pwm);// send PWM to driveENPin
}//motor
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.