搜索代码

Lesson 51: Controlling Two DC Motors Using an Arduino and L293D

Lesson 51: Controlling Two DC Motors Using an Arduino and L293D

In this lesson, we learn how to use an L293D motor driver to drive two DC motors using Arduino code to control the motors in both directions. Please watch the video for full details.

Video chapters:

00:00 Introduction

01:46 Wiring Explained

04:13 Code Explained

11:30 Demonstration

494-Lesson 51: Controlling Two DC Motors Using an Arduino and L293D
语言: C++
/*
 * Lesson 51: Controlling 2 DC motors using L293D and Arduino | Arduino Step By Step Course

 * This is the Arduino code for the L293D DC motor driver.
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

 *  * 
 // Written by Ahmad Shamshiri for Robojax.com on 
// January 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

|||您可能需要的东西

资源与参考

文件📁

没有可用的文件。