Two DC Motors Example

Two DC Motors Example

This example shows you how to use two DC Motors with your Motor Shield.

Hardware

  • Arduino board
  • Arduino Motor Shield
  • 2 DC Motors
  • Wires
  • Battery

Circuit

Arduino Motor Shield example circuit 
Mount your Motor Shield on an Arduino board, connect one of the DC Motors on Channel A and the other on Channel B, also connect the positive pole of a battery to pin "Vin" and the negative pole to the "GND" of the Shield as shown in the figure. 
Finally connect the board to PC with a USB cable and upload the sketch.

Code

In this example are connected two DC Motors. 
One in the Channel A, commanded from pins: 12, 9, 3 and the other in the Channel B, commanded from pins: 13, 8, 11. 
The Pin 12 is used to choose the direction of the motor connected on channel A, instead the pin 13 is used for the channel B. 
If the pin is set as "HIGH" the motor turns forward else if it is set as"LOW" the motor turn backward. 
The Pin 9 is used to stop the motor connected on channel A, instead the pin 8 is used for the channel B. 
If the pin is set as "LOW" disengages the brake and the motor can move else if it is set as "HIGH" engages the brake and the motor is blocked. 
The Pins 3 and 11 are used to select the speed respectively of the motors connected on Channel A and B. 
It is possible set these pins with a value between 0-255 to indicate the speed.

The complete code and its detailed description are shown down.

 
 
/*Motor Shield: Two DC Motor Example*/
//Define the variables used in the sketch
/*DC_Motor1 connected in Channel A */
int pinDirA = 12;//Variable used to direct the Motor1
int pinBrkA = 9;//Varialble used to brake Motor1
int pinPwmA = 3;//Variable used to select the speed Motor1
 
/* DC_Motor2 connected in Channel B */
int pinDirB = 13;//Variable used to direct the Motor2
int pinBrkB = 8;//Variable used to brake the Motor2
int pinPwmB = 11;//Variable used to select the speed Motor2
 
void setup()
{
//initialize the Motor1
pinMode(pinDirA, OUTPUT);
//pinMode(pinPwmA, OUTPUT);
pinMode(pinBrkA, OUTPUT);
//initialize the Motor2
pinMode(pinDirB, OUTPUT);
// pinMode(pinPwmB, OUTPUT);
pinMode(pinBrkB, OUTPUT);
}
 
void loop()
{
//Test 1: Direction: Motor1 and Motor2 forward; Speed: Motor1 and Motor2 Max Speed
/*Motor1*/
digitalWrite(pinDirA, HIGH); //set forward the direction of Motor1
digitalWrite(pinBrkA,LOW);   //disengage the brake for Motor1
analogWrite(pinPwmA, 255);   //set max speed for Motor1
/*Motor2*/
digitalWrite(pinDirB, HIGH); //set forward the direction of Motor2
digitalWrite(pinBrkB,LOW);   //disengage the brake for Motor2
analogWrite(pinPwmB, 255);   //set max speed for Motor2
delay(2500);
 
//Stop Motors
digitalWrite(pinBrkA, HIGH); //Engage the Brake for Channel A
digitalWrite(pinBrkB, HIGH); //Engage the Brake for Channel B
delay(1000);
 
//Test 2: Direction: Motor1 and Motor2 backward; Speed: Motor1 and Motor2 Max Speed
  /*Motor1*/
digitalWrite(pinDirA,LOW); //set backrward the direction of Motor1
digitalWrite(pinBrkA,LOW); //disengage the brake for Motor1
analogWrite(pinPwmA, 255); //set max speed for Motor1
/*Motor2*/
digitalWrite(pinDirB, LOW); //set backward the direction of Motor2
digitalWrite(pinBrkB,LOW); //disengage the brake for Motor2
analogWrite(pinPwmB, 255); //set max speed for Motor2
delay(2500);
 
//Stop Motors
digitalWrite(pinBrkA, HIGH); //Engage the Brake for Channel A
digitalWrite(pinBrkB, HIGH); //Engage the Brake for Channel B
delay(1000);
 
//Test 3: Direction: Motor1 forward; Motor2 backfoward; Speed: Motor1 and Motor2 Max Speed
  /*Motor1*/
digitalWrite(pinDirA,HIGH); //set forward the direction of Motor1
digitalWrite(pinBrkA,LOW);   //disengage the brake for Motor1
analogWrite(pinPwmA, 255);   //set max speed for Motor1
/*Motor2*/
digitalWrite(pinDirB,LOW);   //set backrward the direction of Motor2
digitalWrite(pinBrkB,LOW);   //disengage the brake for Motor2
analogWrite(pinPwmB, 255);   //set max speed for Motor2
delay(2500);
 
//Stop Motors
digitalWrite(pinBrkA, HIGH); //Engage the Brake for Channel A
digitalWrite(pinBrkB, HIGH); //Engage the Brake for Channel B
delay(1000);
 
// Test 4: Direction: Motor1 backward; Motor2 forward; Speed: Motor1 and Motor2 Max Speed
/*Motor1*/
digitalWrite(pinDirA,LOW); //set backrward the direction of Motor1
digitalWrite(pinBrkA,LOW); //disengage the brake for Motor1
analogWrite(pinPwmA, 255); //set max speed for Motor1
/*Motor2*/
digitalWrite(pinDirB, HIGH); //set forward the direction of Motor2
digitalWrite(pinBrkB,LOW); //disengage the brake for Motor2
analogWrite(pinPwmB, 255); //set max speed for Motor2
delay(2500);
 
//Stop Motors
digitalWrite(pinBrkA, HIGH); //Engage the Brake for Channel A
digitalWrite(pinBrkB, HIGH); //Engage the Brake for Channel B
delay(1000);
 
}