2x2A Arduino Motor Shield Twin wiki-DFRobot

2A Motor Shield For Arduino Twin

Introduction

This motor shield allows Arduino to drive two channel DC motors. It uses a L298N chip which deliveries output current up to 2A each channel.

Specifications

Application

PinOut

DRI0017_pinout_en.png

Name Function
En Mn Speed control(PWM)
Mn Mn Direction Control
En Mn State
L X Disable Mn
H L Mn Foreward(Mn+ is positive)
H H Mn Backward(Mn+ is negative)

Note: n of "Mn" or "En" is 1, 2

Tutorial

DC Motor Control

Target: Control speed and direction of DC motor

Step1: Hardware List

Step2: Software List

Step3: Wiring

`DRI0017_MotorConnect_en.png`

Step4: Sample Code

  1. Open Arduino IDE
  2. Upload the code to UNO

/**set control port**/
const int E1Pin = 10;
const int M1Pin = 12;
const int E2Pin = 11;
const int M2Pin = 13;

/**inner definition**/
typedef struct {
  byte enPin;
  byte directionPin;
} MotorContrl;

const int M1 = 0;
const int M2 = 1;
const int MotorNum = 2;

const MotorContrl MotorPin[] = { {E1Pin, M1Pin}, {E2Pin, M2Pin} } ;

const int Forward = LOW;
const int Backward = HIGH;

/**program**/
void setup() {
  initMotor();
}

void loop() {
  int value;
  /**test M1 **/
  setMotorDirection( M1, Forward );
  setMotorSpeed( M1, 100 );
  delay(1000);
  setMotorSpeed( M1, 0 );
  delay(100);

  setMotorDirection( M1, Backward );
  setMotorSpeed( M1, 50 );
  delay(1000);
  setMotorSpeed( M1, 0 );
  delay(100);

  /**test M2**/
  setMotorDirection( M2, Backward );
  for (value = 0 ; value <= 100; value += 5) {
    setMotorSpeed( M2, value );
    delay(100);
  }
  setMotorSpeed( M2, 0 );
  setMotorDirection( M2, Forward );
  for (value = 0 ; value <= 100; value += 5) {
    setMotorSpeed( M2, value );
    delay(100);
  }
  setMotorSpeed( M2, 0 );
}

/**functions**/
void initMotor( ) {
  int i;
  for ( i = 0; i < MotorNum; i++ ) {
    digitalWrite(MotorPin[i].enPin, LOW);

    pinMode(MotorPin[i].enPin, OUTPUT);
    pinMode(MotorPin[i].directionPin, OUTPUT);
  }
}

/**  motorNumber: M1, M2
direction:          Forward, Backward **/
void setMotorDirection( int motorNumber, int direction ) {
  digitalWrite( MotorPin[motorNumber].directionPin, direction);
}

/** speed:  0-100   * */
inline void setMotorSpeed( int motorNumber, int speed ) {
  analogWrite(MotorPin[motorNumber].enPin, 255.0 * (speed / 100.0) ); //PWM
}

Step5: Result

M1 will forward at full speed, and then half speed inversion; M2 velocity from fast to slow, reverse first, and then forward.

FAQ

More question and cool idea, visit DFRobot Forum

More Documents

DFshopping_car1.png Get 2x2A Motor Shield For Arduino Twin from DFRobot Store or DFRobot Distributor.

Category: DFRobot > Arduino > Arduino Shields

Turn to the Top