Example Code for Arduino-Motor Direction Control

This tutorial will cover how to use PWM to control a motor using the shield. Do the wiring according to the Connection Diagram below, and then upload the sample code below to the Arduino board. Here we use a [**arduino UNO**](https://www.dfrobot.com/product-610.html) as the controller, you should see your motor run forward for 3 seconds and then run reverse for another 3 seconds and then repeat.

Hardware Preparation

Name Model/SKU Quantity Purchase Link
DFRduino UNO (or similar) 1
7A Dual DC Motor Driver DRI0041 1
DC Motor 2
Fuse Fuse@20A 1
Jumper wires Several

Software Preparation

  • Arduino IDE Click to Download Arduino IDE from Arduino®.

Wiring Diagram

DRI0041 2x7A DC Motor Driver Connection Diagram DRI0041 2x7A DC Motor Driver Connection Diagram

Facility Safety and the Personal Safety:
Please add a fuse@20A between the Power source and this shield.

Installation Tips

The back of the driver board has some large bare traces. It is important to make sure that these do not get shorted out against conductive surfaces of your project. Please measure carefully and if needed, apply 1mm of non-conductive epoxy to protect the board.

Pluggable Connector

The connectors are designed to be pluggable. This allows you to attach wires with male or female terminations.
DRI0041 2x7A DC Motor Driver Connection Diagram

Sample Code

/*
* @file Motor driver DRI0042_Test.ino
* @brief DRI0042_Test.ino  Motor control program
*
* control motor positive inversion
*
* @author [email protected]
* @version  V1.0
* @date  2016-8-10
*/
const int IN1=5;
const int IN2=4;
const int ENA=6;

const int IN3=8;
const int IN4=7;
const int ENB=9;

void setup() {
     pinMode(IN1, OUTPUT);
     pinMode(IN2, OUTPUT);
     pinMode(ENA, OUTPUT);

     pinMode(IN4, OUTPUT);
     pinMode(IN3, OUTPUT);
     pinMode(ENB, OUTPUT);
}

void loop() {
 Motor1_Brake();
 Motor2_Brake();
 delay(100);
 Motor1_Forward(200);
 Motor2_Forward(200);
 delay(1000);
 Motor1_Brake();
 Motor2_Brake();
 delay(100);
 Motor1_Backward(200);
 Motor2_Backward(200);
 delay(1000);
}

void Motor1_Forward(int Speed)
{
     digitalWrite(IN1,HIGH);
     digitalWrite(IN2,LOW);
     analogWrite(ENA,Speed);
}

void Motor1_Backward(int Speed)
{
     digitalWrite(IN1,LOW);
     digitalWrite(IN2,HIGH);
     analogWrite(ENA,Speed);
}
void Motor1_Brake()
{
     digitalWrite(IN1,LOW);
     digitalWrite(IN2,LOW);
}
void Motor2_Forward(int Speed)
{
     digitalWrite(IN3,HIGH);
     digitalWrite(IN4,LOW);
     analogWrite(ENB,Speed);
}

void Motor2_Backward(int Speed)
{
     digitalWrite(IN3,LOW);
     digitalWrite(IN4,HIGH);
     analogWrite(ENB,Speed);
}
void Motor2_Brake()
{
     digitalWrite(IN3,LOW);
     digitalWrite(IN4,LOW);
}

Result

You should see your motor run forward for 3 seconds and then run reverse for another 3 seconds and then repeat.

Was this article helpful?

TOP