Control DC motor using L293D motor driver with Aarduino PWM (loop) -Example 1

Project Video
Project Details

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

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

See Source for simple L293D motor controller using Arduino (CW, CCW and STOP) -Example 2

Project Code

*

 * 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