Example Code for Arduino-Motor Control (DC Motor Driver 2×15A Lite)
Last revision 2025/12/31
Use this code to control the HCR move with DC Motor Driver 2×15A – Lite.
This code is for DC Motor Driver 2×15A – Lite.
Hardware Preparation
- Main controller: Arduino Mega ADK/2560
- Motor controller: Arduino Nano
- Motor driver: DC Motor Driver 2×15A – Lite
- Two DC motors with 51:1 Gearbox 8000 rpm
- Two encoders with 13 PPR
- 2 wheels (and one caster wheel)
- Three bumper sensors
- Five Sharp GP2D12 infrared sensors
- Six URM04 v2.0 Ultrasonic Sensors
- GMR board
- Switch system
Software Preparation
- Development tool: Arduino IDE 1.0.x or higher
Wiring Diagram


Other Preparation Work
-
Insert the Mega and Nano on the GMR board, connect the output of the switch system to the power supply of the motor controller and the GMR board.

-
Turn off the switch.
-
Connect the circuits as the connection diagram , connect two motors, the motor controller we will test it first.
-
The codes of the motor control should be used in Nano and the codes of the bumpers and IR sensors should be used in Mega ADK. The communication between Nano and Mega ADK is I2C.
Sample Code
#define LF 0
#define RT 1
int E1 = 9; //M1 Speed Control
int E2 = 10; //M2 Speed Control
int M1 = 8; //M1 Direction Control
int M2 = 11; //M1 Direction Control
int a;
int b;
int counter=0;
void setup()
{
int i;
for(i=4;i<=7;i++)
pinMode(i, OUTPUT);
Serial.begin(57600); //Set Baud Rate
Serial.println("Run keyboard control");
digitalWrite(E1,LOW);
digitalWrite(E2,LOW);
}
void loop()
{
Motor(2000,LF);
Motor(1000,RT);
}
void Motor(int value,byte whichwheel)
{
value = constrain(value,1000,2000);
if(whichwheel == LF) {
if(value>1500) {
a=(value-1500)/1.961;
analogWrite (E1,a);
digitalWrite(M1,HIGH);
}
else {
a=(1500-value)/2;
analogWrite (E1,a);
digitalWrite(M1,LOW);
}
}
else if(whichwheel == RT){
if(value>1500) {
b=(value-1500)/1.961;
analogWrite (E2,b);
digitalWrite(M2,HIGH);
}
else {
b=(1500-value)/2;
analogWrite (E2,b);
digitalWrite(M2,LOW);
}
}
}
Result
Test the speed and the direction of the motor. Make sure the motor will run suit your need.
Was this article helpful?
