Complete Guide to DFRobot Motor Shield L298N for Arduino UNO code 1/2

Project Video
Project Details

This video shows you how to use DFRobot Motor Shield for Arduino UNO to control 2 DC motor.

See Advanced Code for DFRobot Motor Shield to control 2 DC motors

Project Code

/*
 * Code to control 2 DC motors using DFRobot Arduino Motor Shield (Basic Code)
 * Written by Ahmad Shamshiri for Robojax.com on Sep 02, 2018 at 09:57 in Ajax, Ontario, Canada
 * Watch video instruction for this code:https://youtu.be/N71djcUZKk0
 * 
 * This code has been download from Robojax.com
    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 .
 * 
 */
const int MotorPinA = 4; // direction motor 1 (Channel A)
const int MotorSpeedPinA = 5; // for motor 1 (channel A)



const int MotorPinB = 7; // direction motor 2 (Channel B)
const int MotorSpeedPinB = 6;// for motor 2 (channel B)


const int CW  = HIGH;
const int CCW = LOW;

void setup() {
  // motor A pin assignment
  pinMode(MotorPinA, OUTPUT);
  pinMode(MotorSpeedPinA, OUTPUT);


  // motor B pin assignment
  pinMode(MotorPinB, OUTPUT);
  pinMode(MotorSpeedPinB, OUTPUT);

  Serial.begin(9600);//  seial monitor initialized 

}

void loop() {


  digitalWrite(MotorPinB, CW);// set direction
      Serial.print("Direction CW - B"); 
  analogWrite(MotorSpeedPinB, 87);// set speed at maximum
      Serial.println(" 87");

 digitalWrite(MotorPinA, CCW);// set direction
      Serial.print("Direction CCW - A"); 
  analogWrite(MotorSpeedPinA, 110);// set speed at maximum
      Serial.println(" 110");
            
  delay(5000);// run for 5 seconds
  analogWrite(MotorSpeedPinB, 0);// stop motor B
        Serial.println("Stop motor B");
  delay(2000);
  
 digitalWrite(MotorPinB, CCW);// set direction 
      Serial.print("Direction CCW - B ");
  analogWrite(MotorSpeedPinB, 160);// set speed at maximum
      Serial.println("160");         
  delay(5000);// run for 5 seconds

  analogWrite(MotorSpeedPinA, 0);// stop motor A
        Serial.println("Stop motor A");

  delay(2000);


  digitalWrite(MotorPinA, CW);// set direction
      Serial.print("Direction CW - A "); 
  analogWrite(MotorSpeedPinA, 189);// set speed at maximum
      Serial.println("189");  
 delay(3000);
  

Serial.println("===== loop "); 

}// loop end



Required File(s)