Tags: , , ,
The Motor Shield V2 is an expansion board for microcontrollers Arduino UNO and Mega to control DC and stepper motors. For motor control, it is possible to use integrated circuits directly but their use requires significant wiring can quickly become unmanageable.
To make clean prohect, you can either create your own PCB or use Shields.
We will use the Motor Shield V2 and see the improvements over the previous version.

Prerequisite: Programming with Arduino



Hardware

  • Computer
  • ArduinoUNO
  • USB A Male/B Male
  • MotorShieldV2
  • DC motor or stepper x1

Operating principle

The Motor Shield V2 uses the MOSFET TB6612 with 1.2A capacity per channel and I2C communication. It uses fewer pins than the previous version:
  • Digital pin SCL/A5: Communication I2C Serial Clock Line
  • Digital pin SDA/A4: Communication I2C Serial Data Line
The Motor Shield V2 has jumper to select the I2C address and is stackable. Which mean, you can operate up to 32 shieds (64 steppers or DC motors 128) with a single Arduino.
In the case of a shield, the connections are predefined. Make sure the component’s technical documentation to use it.




Schematics

Compatible with UNO and Mega card, the shield is placed directly on the Arduino board. Power connects to Power terminal block. The motors are connected to the terminals M1 to M4. Three types of motors can be connected to the shield (excluding actuators):
  • DC motor
  • Unipolar stepper motor
  • Bipolar stepper
The connections of these engines are described in the following schemes.





Code

To interact with the Motor Shield V2, we use the library Adafruit_MotorShield.h .
To drive a DC motor, we will use the Adafruit_DCMotor class whose functions are:
  • motor.getMotor (whole) to select the motor used
  • motor.run () to activate the motor in one direction or the other (RELEASE, BACKWARD, FORWARD).
  • motor.setSpeed ​​() to adjust the speed
To control the step-by-step motors we will use the Adafruit_StepperMotor class whose functions are:
  • motor.getStepper (number of steps, terminal used) to select the motor used
  • motor.setSpeed ​​(speed) to adjust the speed
  • motor.step (number of steps, direction, algortihme) to rotate a number of steps in one direction or the other

//Libraries
#include //https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library/blob/master/Adafruit_MotorShield.h

//Objects
Adafruit_MotorShield AFMS=Adafruit_MotorShield();
Adafruit_DCMotor *motor = AFMS.getMotor(1);;
Adafruit_StepperMotor *stepper = AFMS.getStepper(48, 2);;

void setup(){
//Init Serial USB
Serial.begin(9600);
Serial.println(F("Initialize System"));
//Init MotorShieldV2
 AFMS.begin();
 stepper->setSpeed(10);//rpm
motor->run(RELEASE);
}

void loop(){
testStepperMS2();
testMotorMS2();
}

void testStepperMS2(){/* function testStepperMS2 */ 
//// Test stepper 
  int nbSteps=4080;
  Serial.println("Single coil steps forward");
  stepper->step(nbSteps, FORWARD, SINGLE); 
  delay(1000);
  Serial.println("Single coil steps backward");
  stepper->step(nbSteps, BACKWARD, SINGLE); 
  delay(1000);
  Serial.println("Double coil steps forward");
  stepper->step(nbSteps, FORWARD, DOUBLE); 
  Serial.println("Double coil steps backward");
  stepper->step(nbSteps, BACKWARD, DOUBLE);
  delay(1000);
  Serial.println("Interleave coil steps forward");
  stepper->step(nbSteps, FORWARD, INTERLEAVE); 
  Serial.println("Interleave coil steps backward");
  stepper->step(nbSteps, BACKWARD, INTERLEAVE); 
  delay(1000);
  Serial.println("Microstep steps forward");
  stepper->step(50, FORWARD, MICROSTEP); 
  Serial.println("Microstep steps backward");
  stepper->step(50, BACKWARD, MICROSTEP);
  stepper->release();
}

void testMotorMS2(){/* function testMotorMS2 */ 
//// Test DC motor 
Serial.println(F("DC forward"));
 motor->setSpeed(150);
 motor->run(FORWARD);
 delay(500);
 motor->run(BACKWARD);
 delay(500);
 motor->run(RELEASE);
}


Applications

  • Manage motors of a robot

Sources

To convert your code from Motor Shield V1 to V2, the following modification should be made:
  • Replace library
    #include <Adafruit_MotorShield.h> 
  • Create MotorShield object:
    Adafruit_MotorShield AFMS = Adafruit_MotorShield();  
  • Create DCmotor using pointer:
    Adafruit_DCMotor *motorG = AFMS.getMotor(1); 
    Adafruit_StepperMotor
    *myMotor = AFMS.getStepper(48, 2);
  • Initilize shield:
      AFMS.begin();   
  • Replace “.” with “->” when function call:
    motorG->setSpeed(motorSpeed);  
    motorG->run(RELEASE);

Test and mix those examples to obtain the desired functionalities. If you encounter any issue, do not hesitate to write a comment or send us a message.

Find more tutorials and examples in our code generator
Code Architect