Other Arduino Codes and Videos by Robojax

Complete Guide to DDFRobot Motor Shield for Arduino UNO Robojax.com

DFRobot Motor Shield for Arduino UNO

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

Basic Code to control 2 DC Motors using DFRobot Motor Shield


 
/*
 * 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 <https://www.gnu.org/licenses/>.
 * 
 */
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





   

Advanced Code for DFRobot Motor Shield to control 2 DC motors

This code has two extra functions moveMotor() and brake() function which makes controlling motor very easy and with minimum amount of code lines.


 /*
 * Code to control 2 DC motors using DFRobot Arduino Motor Shield (Advanced Code)
 * This code has two extra functions moveMotor() and brake() function which makes controlling motor very easy and with minimum amount of code lines.
 * 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 <https://www.gnu.org/licenses/>.
 * 
 */
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() {


  moveMotor('B', CCW, 120); 
      Serial.print("Motor B CCW "); 
      Serial.println(" 120");
 delay(3000);
 brake('B');// stops motor B
    Serial.println("Motor B Stopped ");
 delay(2000);
  moveMotor('B', CW, 255); 
      Serial.print("Motor B CW "); 
      Serial.println(" 255"); 
 delay(4000);
  

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

}// loop end


/*
 * 
 * Written by Ahmad Shamshiri August 29 2018 at 20:59 in Ajax, Ontario, Canada 
 * moveMotor controls the motor
  @param motor is char A or B refering to motor A or B.
  @param dir is motor direction, CW or CCW
  @speed is PWM value between 0 to 255

  Example 1: to start moving motor A in CW direction with 135 PWM value
  moveMotor('A', CW, 135);

  Example 2: to start moving motor B in CCW direction with 200 PWM value
  moveMotor('B', CCW, 200);  
 */

void moveMotor(char motor, int dir, int speed)
{
  int motorPin;
  int motorSpeedPin;
  
  if(motor =='A')
  {
    motorPin      = MotorPinA;
    motorSpeedPin = MotorSpeedPinA;  
  }else{
    motorPin      = MotorPinB;
    motorSpeedPin = MotorSpeedPinB;     
  }
   digitalWrite(motorPin, dir);// set direction for motor
   analogWrite(motorSpeedPin, speed);// set speed of motor   
}//moveMotor end

/*
 * for function is to be used with DFRobot Motor Shield
 * brake, stops the motor
 * @param motor is character A or B
 * example of usage:
 * brake('A');// stops motor A
 * brake('B');// stops motor B
 */
void brake(char motor)
{
  if(motor =='A')
  {
    digitalWrite(MotorSpeedPinA, 0);// stop motor A
    delay(1000);
  }else{
    digitalWrite(MotorSpeedPinB, 0);// stop motor B
    delay(1000);
   
  }
}// brake end