Tags: , , ,
The Motor Shield V1 is an expansion board for Arduino UNO and Mega microcontrollers for DC and stepper motor control. We have seen how to control a DC motor using H bridge which may require a lot of connections when using the simple integrated circuit. In an embedded application, such as a Rovy robot, you will have to control several motors in parallel. Shields exist for this purpose, to simplify programming and installation.
The Shield used in this tutorial is the Motor Shield V1 from Adafruit.

Prerequisite : Programming with Arduino, Control a DC Motor with Arduino, Control a stepper with Arduino



Hardware

  • Computer
  • ArduinoUNO
  • USB A Male/B Male
  • MotorShieldV1
  • x1 DC motor or stepper motor-not x1

Operating principle

The Motor Shield uses the H-bridge L293D and 74HC595N integrated circuit. It uses the following pins:
– Digital Pin 11: Motor # 1 DC/Stepper # 1 (on/speed)

– Digital pin 3: # 2 DC Motor/Stepper # 1 (on/speed)

– Digital pin 5: DC Motor # 3/# 2 Stepper (activation/speed)

– Digital pin 6: # 4 DC Motor/Stepper # 2 (activation/speed)

– Digital pin 4, 7, 8 and 12 for communication with the 74HC595

– Digital pin 9 and 10 for the actuators 1 and 2
In the case of a shield, the connections are predefined. Make sure to check in the component’s technical documentation how to use it (Motor Shield V1 datasheet).




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 this shield (excluding servomotors):
– DC motor

– Unipolar stepper

– Bipolar stepper
The connections of these engines are described in the following schemes.
Warning: the shield depicted in the diagram is the Motor Shield V2 but the connection at the terminal remains the same
.





Code

In order to interact with the Motor Shield V1, we use AFMotor.h library.
To drive a DC motor, we will use the AF_DCMotor class whose functions are to know:
-motor.run() to activate the motor in one direction or the other (RELEASE, BACKWARD, FORWARD).

-motor.setSpeed() to set the speed of the motor
For motors step by step we use AF_Stepper class whose functions are to know:
-stepper.step (nombre_de_pas, MANAGEMENT, MODE) to perform a number of steps in one direction and a given mode.

-stepper.setSpeed() to set the speed of the motor

//Libraries
#include <AF_Motor.h>//https://github.com/adafruit/Adafruit-Motor-Shield-library

//Objects
AF_DCMotor motor(1);;
AF_Stepper stepper(48, 2);;

void setup(){
//Init Serial USB
Serial.begin(9600);
Serial.println(F("Initialize System"));
//Init MotorShieldV1
 motor.setSpeed(10);//rpm
stepper.setSpeed(10);//rpm
}

void loop(){
testStepperMS1();
testMotorMS1();
}

void testStepperMS1(){/* function testStepperMS1 */
////Test stepper
Serial.println("Single coil steps");
  stepper.step(NB_OF_STEPS, FORWARD, SINGLE);
  stepper.step(NB_OF_STEPS, BACKWARD, SINGLE);

  Serial.println("Double coil steps");
  stepper.step(NB_OF_STEPS, FORWARD, DOUBLE);
  stepper.step(NB_OF_STEPS, BACKWARD, DOUBLE);

  Serial.println("Interleave coil steps");
  stepper.step(NB_OF_STEPS, FORWARD, INTERLEAVE);
  stepper.step(NB_OF_STEPS, BACKWARD, INTERLEAVE);

  Serial.println("Micrsostep steps");
  stepper.step(NB_OF_STEPS, FORWARD, MICROSTEP);
  stepper.step(NB_OF_STEPS, BACKWARD, MICROSTEP);
}

void testMotorMS1(){/* function testMotorMS1 */
////Test DC motor
Serial.println(F("-------------------------------------"));
  Serial.println(F("Avant "));
  motor.run(FORWARD);
  delay(500);
  Serial.println(F("Arrière "));
  motor.run(BACKWARD);
  delay(500);
  Serial.println(F("Arrêt "));
  motor.run(RELEASE);
  delay(1000);
}




Applications

  • Manage stepper motors, DC motors and actuators of a robot

Sources

Find more tutorials and examples in our code generator
Code Architect