Introduction: How to Control DC Motors Using Arduino

We recently finished designing our compact plug-and-play Arduino robot and released it on Kickstarter. We are currently looking for backers.

With this robotic kit, it is very easy to connect motors, sensors and communication modules by simply plug them in. You don't need to deal with messy wiring. You will be able to build a functional robot in no time.

In this Instructable, I am going to show you how to control DC motors using our kit. In the near future, instructions will be provided to show how to use WiFi, Bluetooth, RF, Gyroscope and even GPS.

Step 1: Identify the Connectors on Fabric Shield

Fabric shield is not an ordinary PCB. It also serves as a robot chassis. There are holes at four corners specifically designed to attach the motors using screws.

Four digital pins (D3, D6, D9 and D10) on Arduino Nano could be used to control motors. Since motors takes a lot of current, a motor driver chip (L293D) is applied in fabric shield to provide much higher current capacity. Motors could be connected to either the header pins (highlighted in purple) or to the screw terminals. Input voltage (VIN) should match the rating of the motors, but it also needs to be 5V and above. In other words, if you use 6V DC motors, VIN needs to connect to 6V.

Step 2: Connect the Motors to Fabric Shield

Use screw driver to attach the motors and caster wheel to the back side of the fabric shield. Each motor has two wires. Connect the wires to the jumpers as shown in the diagram.

In the close-up, it shows how the pins are arranged. There are two header pins each for D6, D9, 5V and GND. This makes it easy to connect two DC motors or two servo motors using these 8 pins. Similarly, there is another group of 8 pins for D3, D10, 5V and GND to control another two DC / servo motors.

Step 3: Program Arduino Nano

Program your Arduino Nano (or compatible) using standard IDE from Arduino.cc

Insert your Arduino Nano controller to the female headers as shown in the picture. Finally, you may attach the power through the DC power jack or screw terminals.

Step 4: The Code

The following code demonstrates basic control of the DC motors. Motor1 is controlled through digital pins 3 and 10. Motor 2 is controlled through digital pins 6 and 9. In addition, motor driver could be completely turned off using analog pin A2.

In the setup() function, it enables the motor driver by calling setupMotor().

In the main loop, it calls the functions to move the robot forward, left , backward and right in sequence. In each step, it waits for 200 milliseconds.

// pins controller motors
#define motor1_pos 3
#define motor1_neg 10
#define motor2_pos 6
#define motor2_neg 9
#define motor_en   A2


void setup()
{
   Serial.begin(57600);
   setupMotor();
}


void loop() 
{
    robotForward(200);
    robotLeft(200);
    robotBackward(200);
    robotRight(200);
    robotStop(500);
}
   
void setupMotor() {
  pinMode(motor1_pos,OUTPUT);
  pinMode(motor1_neg,OUTPUT);
  pinMode(motor2_pos,OUTPUT);
  pinMode(motor2_neg,OUTPUT);
  pinMode(motor_en,OUTPUT);
  enableMotor();
  robotStop(50);
}


//-----------------------------------------------------------------------------------------------------
// motor
//-----------------------------------------------------------------------------------------------------


void enableMotor() {
  //Turn on the motor driver chip : L293D
  digitalWrite(motor_en, HIGH); 
}


void disableMotor() {
  //Turn off the motor driver chip : L293D
  digitalWrite(motor_en, LOW); 
}


void robotStop(int ms){
  digitalWrite(motor1_pos, LOW); 
  digitalWrite(motor1_neg, LOW); 
  digitalWrite(motor2_pos, LOW); 
  digitalWrite(motor2_neg, LOW); 
  delay(ms);
}


void robotForward(int ms){
  digitalWrite(motor1_pos, HIGH); 
  digitalWrite(motor1_neg, LOW); 
  digitalWrite(motor2_pos, HIGH); 
  digitalWrite(motor2_neg, LOW); 
  delay(ms);
}


void robotBackward(int ms){
  digitalWrite(motor1_pos, LOW); 
  digitalWrite(motor1_neg, HIGH); 
  digitalWrite(motor2_pos, LOW); 
  digitalWrite(motor2_neg, HIGH); 
  delay(ms);
}
void robotRight(int ms){
  digitalWrite(motor1_pos, LOW); 
  digitalWrite(motor1_neg, HIGH); 
  digitalWrite(motor2_pos, HIGH); 
  digitalWrite(motor2_neg, LOW); 
  delay(ms);
}


void robotLeft(int ms){
  digitalWrite(motor1_pos, HIGH); 
  digitalWrite(motor1_neg, LOW); 
  digitalWrite(motor2_pos, LOW); 
  digitalWrite(motor2_neg, HIGH); 
  delay(ms);
}


<br>