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


  • 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.


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.


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

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

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

void setup(){
//Init Serial USB
Serial.println(F("Initialize System"));
//Init MotorShieldV2

void loop(){

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

void testMotorMS2(){/* function testMotorMS2 */ 
//// Test DC motor 
Serial.println(F("DC forward"));


  • Manage motors of a robot


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); 
    *myMotor = AFMS.getStepper(48, 2);
  • Initilize shield:
  • Replace “.” with “->” when function call:

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 other examples and tutorials in our Automatic code generator
Code Architect

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?