Robot Car

In this project, you'll learn how to control the two DC motors to make the robot car move around.
 

Parts needed: Robot Chassis L293D H-Bridge

Controlling Two DC Motors

1 To control two DC motors, use the first half (Input 1 and 2) of the L293D chip for the first motor and the second half (Input 3 and 4) for the second motor. You already should have connected the first motor in the DC Motor project. For the second motor we just need to make four new connections highlighted in red in the following connection table.

Arduino L293D Motor 1 & 2 Description
11 & 14 motor 2 wires Connection to motor 2
(yellow and green)
9 (PWM) 10 Input 3 (blue)
10 (PWM) 15 Input 4 (blue)
3 & 6 motor 1 wires Connection to motor 1
(yellow and green)
6 (PWM) 2 Input 1 (purple)
5 (PWM) 7 Input 2 (purple)
Vin 8 6V (red)
5V 16 5V (orange)
GND 5 Ground (black)
8 battery positive (red)
5 battery negative (black)
Two DC motor connections
2 We need to add in the extra code to control the second motor. This second set of code for controlling the second motor is similar to the first set of code that controls the first motor.
// connections for motor 1
int in1Pin = 6;   // Input 1, must be one of the PWM ~ pins
int in2Pin = 5;   // Input 2, must be one of the PWM ~ pins
// connections for motor 2
int in3Pin = 9;   // Input 3, must be one of the PWM ~ pins
int in4Pin = 10;  // Input 4, must be one of the PWM ~ pins

void setup() {
   // controls for motor 1
   pinMode(in1Pin, OUTPUT);
   pinMode(in2Pin, OUTPUT);
   // controls for motor 2
   pinMode(in3Pin, OUTPUT);
   pinMode(in4Pin, OUTPUT);

   // go forward
   analogWrite(in1Pin, 255);  // one input > 0
   analogWrite(in2Pin, 0);    // the other = 0   
   analogWrite(in3Pin, 255);  // one input > 0
   analogWrite(in4Pin, 0);    // the other = 0
   delay(1000);

   // turn left
   analogWrite(in1Pin, 0);    // one input > 0
   analogWrite(in2Pin, 255);  // the other = 0
   analogWrite(in3Pin, 255);  // one input = 0
   analogWrite(in4Pin, 0);    // the other > 0
   delay(1000);

   // stop
   analogWrite(in1Pin, 0);    // stop motor 1
   analogWrite(in2Pin, 0);
   analogWrite(in3Pin, 0);    // stop motor 2
   analogWrite(in4Pin, 0);
}

void loop() {
}
3 Upload and run the program. The robot should go forward for one second, then turn left for another second, and then stop.

If it does something else (i.e., turn first or go backwards instead), then you need to either flip the motor wires or flip the values in the analogWrite commands. (Remember that flipping either the wires or the values in the annalogWrite commands will cause the motor to spin in the opposite direction.)

Make sure that you get this correct before continuing.