Other Arduino Codes and Videos by Robojax

Arduino Code and video L293D and DC Motor Controller (witout speed control)

دروس آردوینو به فارسی

Arduino code L293 DC motor Driver

Using L293 and L293D DC motor driver you can control two motors with controlling direction of rotation. This code is NOT for Stepper Motor control. it is just for DC motor control.
Code to contorl speed of DC motor using L293D Is here
Code for L293D Motor shield to control 4 DC motors Is here

Resources for this sketch


 /*


 * This is the Arduino code L293 DC motor Driver (Advanced)
Using this code you can control a motor to rotate in both direction Clockwise(CW) 
and Counter-clockwise(CCW)

 *  * 
 // Writeen by Ahmad S. for Robojax.com on 
// on Feb 25, 2018 at 18:30 at city of Ajax, Ontario, Canada
 * Permission granted to share this code given that this
 * note is kept with the code.
 * Disclaimer: this code is "AS IS" and for educational purpose only.
 * 

 * watch L293 video for details https://youtu.be/jAmDliHcTJ0
 * Code is available at http://robojax.com/learn/arduino

 * 

*/
// DC motor 1 control
#define P1A 2 // define pin 2 as for P1A
#define P2A 7 // define pin 7 as for P2A
#define EN12 8 // define pin 8 as for 1,2EN enable



// DC motor 2 control 
#define P3A 10 // define pin 10 as for P3A
#define P4A 13 // define pin 13 as for P4A
#define EN34 9 // define pin 9 as for EN3 and EN4 enable

/*
 * Permission granted to share this code given that this
 * note is kept with the code.
 * Disclaimer: this code is "AS IS" and for educational purpose only.
 * 
 */

void setup() {
  // L293 Motor Control Code by Robojax.com 2018025
  Serial.begin(9600);// setup Serial Monitor to display information
  pinMode(P1A, OUTPUT);// define pin as OUTPUT for P1A
  pinMode(P2A, OUTPUT);// define pin as OUTPUT for P2A
  pinMode(EN12, OUTPUT);// define pin as OUTPUT for 1,2EN
   
  pinMode(P3A, OUTPUT);// define pin as OUTPUT for P3A
  pinMode(P4A, OUTPUT);// define pin as OUTPUT for P4A
  pinMode(EN34, OUTPUT);// define pin as OUTPUT for 3,4EN   
  // L293 Motor Control Code by Robojax.com 2018025   
}

void loop() {
  // L293 Motor Control Code by Robojax.com 2018025


    Serial.println(" Rotating CW");
    digitalWrite(EN12 ,HIGH);// Enable 1A and 2A 
    digitalWrite(P1A,HIGH);// send + or HIGH singal to P1A
    digitalWrite(P2A,LOW);// send - or LOW singal to P2A       
    delay(3000);// motor runs for 3 seconds
    digitalWrite(EN12 ,LOW);// Disable 1A and 2A
    delay(2000);// motor stop for 3 seconds 
    Serial.println(" Motor Stopped"); 

  // L293 Motor Control Code by Robojax.com 2018025
    // now changing the direction of rotation of motor
    Serial.println(" Rotating CCW");
    digitalWrite(EN12 ,HIGH);// Enable 1A and 2A     
    digitalWrite(P1A,LOW);// send + or HIGH singal to P1A
    digitalWrite(P2A,HIGH);// send - or LOW singal to P2A  
    delay(3000);// motor runs for 3 seconds
    digitalWrite(EN12 ,LOW);// Disable 1A and 2A
    delay(2000);// motor stop for 3 seconds    
    Serial.println(" Motor Stopped");   
     

  Serial.println("=========== Loop done"); 
  delay(500);
  // L293 Motor Control Code by Robojax.com 2018025 
}

   

Arduino code L293 DC motor Driver (Advanced)

Using L293 and L293D DC motor driver you can control two motors with controlling direction of rotation. In this Advanced code you a separate function is used to offer controlling the motor easily and make it ready for your application with simple code. This code is NOT for Stepper Motor control. it is just for DC motor control.

 /*


 * This is the Arduino code L293 DC motor Driver (Advanced)
Using this code you can control a motor to rotate in both direction Clockwise(CW) 
and Counter-clockwise(CCW)

 *  * 
 // Writeen by Ahmad S. for Robojax.com on 
// on Freb 25, 2018 at 18:30 at city of Ajax, Ontario, Canada
 * Permission granted to share this code given that this
 * note is kept with the code.
 * Disclaimer: this code is "AS IS" and for educational purpose only.
 * 

* watch L293 video for details https://youtu.be/jAmDliHcTJ0
 * Code is available at http://robojax.com/learn/arduino

 * 

*/
// DC motor 1 control
#define P1A 2 // define pin 2 as for P1A
#define P2A 7 // define pin 7 as for P2A
#define EN12 8 // define pin 8 as for 1,2EN enable


// DC motor 2 control 
#define P3A 10 // define pin 10 as for P3A
#define P4A 13 // define pin 13 as for P4A
#define EN34 9 // define pin 9 as for EN3 and EN4 enable

/*
 * Permission granted to share this code given that this
 * note is kept with the code.
 * Disclaimer: this code is "AS IS" and for educational purpose only.
 * 
 */

void setup() {
  // L293 Motor Contro Code by Robojax.com 2018025
  Serial.begin(9600);// setup Serial Monitor to display information
  pinMode(P1A, OUTPUT);// define pin as OUTPUT for P1A
  pinMode(P2A, OUTPUT);// define pin as OUTPUT for P2A
  pinMode(EN12, OUTPUT);// define pin as OUTPUT for 1,2EN
   
  pinMode(P3A, OUTPUT);// define pin as OUTPUT for P3A
  pinMode(P4A, OUTPUT);// define pin as OUTPUT for P4A
  pinMode(EN34, OUTPUT);// define pin as OUTPUT for 3,4EN 

  // L293 Motor Contro Code by Robojax.com 2018025   
}

void loop() {

 if(value == 45){
  rotateMA("CW",HIGH); // start motor in CW
 }
  delay(3000); 
  rotateMA("CW",LOW); // stop motor
  delay(2000);

  rotateMA("CCW",HIGH); // start motor in CCW
  delay(3000); 
  rotateMA("CW",LOW); /// stop motor
  delay(2000);  
  
  // L293 Motor Contro Code by Robojax.com 2018025 
}// loop ends


/**
 * function: rotateMA(String di,int action)
 * Sends power to motor A
 * di is tring and can be "CW" or "CCW"
 * to use: 
 * rotateMA("CW",HIGH);// to rotate motor  CW
 * rotateMA("CCW",HIGH);// to rotate motor CCW
 *  to stop the motor
 * rotateMA("CW",LOW);// to stop motor 
 */
void rotateMA(String di,int endis)
{
    // L293 Motor Contro Code by Robojax.com 2018025
 if(di =="CW"){
    Serial.println(" Rotating CW");
    digitalWrite(EN12 ,endis);// Enable 1A and 2A 
    digitalWrite(P1A,HIGH);// send + or HIGH singal to P1A
    digitalWrite(P2A,LOW);// send - or LOW singal to P2A       
  

 }else{
  // L293 Motor Contro Code by Robojax.com 2018025
    // now changing the direction of rotation of motor
    Serial.println(" Rotating CCW");
    digitalWrite(EN12 ,endis);// Enable 1A and 2A  
    digitalWrite(P1A,LOW);// send + or HIGH singal to P1A
    digitalWrite(P2A,HIGH);// send - or LOW singal to P2A  

 }   
   // L293 Motor Contro Code by Robojax.com 2018025
}//rotateMA ends here

   

If you found this tutorial helpful, please support me so I can continue creating content like this. support me via PayPal