Other Arduino Codes and Videos by Robojax

Control DC motor using L293D motor driver with Aarduino PWM

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

Control DC motor using L293D motor driver with Aarduino PWM

This video shows you how to control DC motor speed with L293D motor driver and PWM using Arduino.
Code for L293D Motor shield to control 4 DC motor Is here

Resources for this sketch

Arduin oSource for L293D motor driver (loop)

This code will run loop to speed up the motor from value of 0 to 255 which is from 0% to 100% PWM.


 /*

 * This is the Arduino code to control speed of motor using L293d DC motor Driver

Watch instruction for this video: https://youtu.be/akQoGNUzhHI

 // Written for Robojax.com video 

 * Code is available at http://robojax.com/learn/arduino

 * 
 // Written by Ahmad S. for Robojax.com on 
// on Aug 11, 2018 at 13:23 at city of Ajax, Ontario, Canada
// This code is AS is without warranty. You can share if you keep this note with the code
*/
// DC motor  control
#define P1A 10 // define pin 10as for P1A
#define P2A 11 // define pin 11 as for P2A
#define EN12 9 // define pin 9 as for 1,2EN enable

const int speedStep =15;
const int speedDelay = 1000;// delay between speed increment

void setup() {
  // L293 Motor Control Code by Robojax.com 20180811
  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

  Serial.println("Robojax");
  Serial.println("L293D Motor Speed Control");
  
  // L293 Motor Control Code by Robojax.com 20180811   
}

void loop() {
  // L293d Motor Control Code by Robojax.com 20180811

 for(int speed=0; speed<=255; speed +=speedStep)
 {
  L293D('L',speed, 0);// 
  delay(speedDelay);// delay between each step
 }

  delay(50);// 50 millisecond delay
  // L293d Motor Control  by Robojax.com 20180810 
}//loop end




/*
 * L293D(char dir,int spd, int en)
 * dir is character either L for CW direction
 *  or R for CCW direction
 *  en is integer 1 to totate, 0 for stop
 *  spd is the speed value from 0 to 255
 */
void L293D(char dir,int spd, int en)
{
  if(dir =='L')
  {
    if(en ==0){
       Serial.println(" CW Motor Stopped");
    }else{
       Serial.print(" Rotating CW: "); 
       Serial.println(spd);//print actual speed value        
    }
    digitalWrite(EN12 ,en);// Enable 1A and 2A 
    analogWrite(P1A,spd);// send PWM with spd value to P1A
    digitalWrite(P2A,LOW);// LOW singal to P2A       
   
  }else{
    if(en ==0){
       Serial.println(" CCW Motor Stopped");
    }else{
       Serial.print(" Rotating CCW: "); 
       Serial.println(spd);//print actual speed value     
    }    
    digitalWrite(EN12 ,en);// Disable 1A and 2A    
    digitalWrite(P1A,LOW);// Keep thos LOW P1A
    analogWrite(P2A,spd);// send PWM with spd value to  P2A  
  }
}//L293D end



   

Source for simple L293D motor controller using Arduino (CW, CCW and STOP)

This code will control the motor using L293D with different value to rotate right, left of stop it.


  /*

 * This is the Arduino code to control speed of motor using L293d DC motor Driver
 * This code will control the motor with different direction and speed type

Watch instruction for this video: https://youtu.be/akQoGNUzhHI

 // Written for Robojax.com video 

 * Code is available at http://robojax.com/learn/arduino

 * 
 // Written by Ahmad Shamshiri for Robojax.com on 
// on Aug 11, 2018 at 13:23 at city of Ajax, Ontario, Canada
// This code is AS is without warranty. You can share if you keep this note with the code
*/
// DC motor  control
#define P1A 10 // define pin 10as for P1A
#define P2A 11 // define pin 11 as for P2A
#define EN12 9 // define pin 9 as for 1,2EN enable

const int speedStep =15;
const int speedDelay = 1000;// delay between speed increment

void setup() {
  // L293 Motor Contro Code by Robojax.com 20180811
  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

  Serial.println("Robojax");
  Serial.println("L293D Motor Speed Control");
  
  // L293 Motor Control Code by Robojax.com 20180811   
}

void loop() {
  // L293d Motor Control Code by Robojax.com 20180811


  L293D('L',255, 1);// maximum speed to LEFT
  delay(3000);// wait for 3 seconds

  L293D('L',255, 0);// stop motor
  delay(2000);// wait for 2 seconds

  L293D('L',127, 1);// half speed to LEFT
  delay(3000);// wait for 3 seconds  

  L293D('L',255, 0);// stop motor
  delay(2000);// wait for 2 seconds

  L293D('R',90, 1);// slow to RIGH
  delay(3000);// wait for 3 seconds  

  L293D('L',255, 0);// stop motor
  delay(2000);// wait for 2 seconds

  // L293d Motor Control  by Robojax.com 20180810 
}//loop end




/*
 * L293D(char dir,int spd, int en)
 * dir is character either L for CW direction
 *  or R for CCW direction
 *  en is integer 1 to totate, 0 for stop
 *  spd is the speed value from 0 to 255
 */
void L293D(char dir,int spd, int en)
{
  if(dir =='L')
  {
    if(en ==0){
       Serial.println(" CW Motor Stopped");
    }else{
       Serial.print(" Rotating CW: "); 
       Serial.println(spd);//print actual speed value        
    }
    digitalWrite(EN12 ,en);// Enable 1A and 2A 
    analogWrite(P1A,spd);// send PWM with spd value to P1A
    digitalWrite(P2A,LOW);// LOW signal to P2A       
   
  }else{
    if(en ==0){
       Serial.println(" CCW Motor Stopped");
    }else{
       Serial.print(" Rotating CCW: "); 
       Serial.println(spd);//print actual speed value     
    }    
    digitalWrite(EN12 ,en);//enable 1A and 2A    
    digitalWrite(P1A,LOW);// Keep it LOW P1A
    analogWrite(P2A,spd);// send PWM with spd value to  P2A  
  }
}//L293D end


   

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