Robot Car

In this project, you'll first learn how to connect batteries to provide power for the robot car, and then you'll learn how to control the two DC motors to make the robot car move around.
 
Parts needed: Robot Chassis L293D H-Bridge

Using Batteries to Power the Robot Car

1 Remove power to the Arduino by disconnecting the USB cable from the computer. Make sure that the on/off switch for the battery power is in the off (0) position.
2 To have the robot run using just the batteries (i.e., without the USB cable connected to the computer), and also to make the DC motors spin faster, we need to make the following modifications (disconnect one wire and connect three new wires):
 
  • Remove the current connection from pin 8 on the L293D chip that is connected to the 5V power on the Arduino
  • Connect the 6V positive (red wire) from the battery to the Vin pin on the Arduino (using the red row of pins on the breadboard)
  • Connect the negative (black wire) from the battery to GND (using the blue row of pins on the breadboard)
  • Connect the 6V positive (red wire) from the battery to pin 8 on the L293D chip (using the red row of pins on the breadboard)
The new connections are highlighted in red in the following connection table. (The remaining connections in the table have already been made.)

Arduino L293D Motor 1 6V Battery Description
Vin + positive 6V Arduino power
GND 4 – negative Ground (negative)
8 + positive 6V motor power
11 1 Enable for motor 1
10 2 Input 1
9 7 Input 2
3 & 6 motor 1 wires Output 1 & 2
5V 16 5V (positive) power
DC motor connection using more power
3 We can use the same code as before to test it out. Turn on the battery power and you should see the motor spin faster.
int enable12Pin = 11;  // must be one of the PWM ~ pins
int in1Pin = 10;
int in2Pin = 9;

void setup() {
  pinMode(enable12Pin, OUTPUT);
  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);  

  analogWrite(enable12Pin, 255);   // set speed for motor 1
  digitalWrite(in1Pin, LOW);       // set direction for motor 1
  digitalWrite(in2Pin, HIGH);
}

void loop() {
}

Controlling Two DC Motors

4 To control two DC motors, use the second half of the L293D chip to connect to the second motor. We just need to make four new connections.

The new connections are highlighted in red in the following connection table. (The remaining connections in the table have already been made.)

Arduino L293D Motor 1 & 2 6V Battery Description
6 9 Enable for motor 2
5 10 Input 3
4 15 Input 4
11 & 14 motor 2 wires Output 3 & 4
11 1 Enable for motor 1
10 2 Input 1
9 7 Input 2
3 & 6 motor 1 wires Output 1 & 2
GND 4 – negative Ground (negative)
5V 16 5V (positive) power
Vin 8 + positive 6V motor power
Two DC motor connections
5 We need to add in the extra code to control the second motor.
int enable12Pin = 11;   // enable pin for motor 1
int in1Pin = 10;        // input 1
int in2Pin = 9;         // input 2
int enable34Pin = 6;    // enable pin for motor 2
int in3Pin = 5;         // input 3
int in4Pin = 4;         // input 4

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

   // go forward
   analogWrite(enable12Pin, 255);   // set maximum speed for motor 1
   analogWrite(enable34Pin, 255);   // set maximum speed for motor 2
   digitalWrite(in1Pin, HIGH);      // set direction of motor 1
   digitalWrite(in2Pin, LOW);       // the two inputs are always inverses of each other
   digitalWrite(in3Pin, HIGH);      // set direction of motor 2
   digitalWrite(in4Pin, LOW);       // the two inputs are always inverses of each other
   delay(2000);

   // turn left
   digitalWrite(in1Pin, HIGH);      // set direction of motor 1
   digitalWrite(in2Pin, LOW);       // the two inputs are always inverses of each other
   digitalWrite(in3Pin, LOW);       // set direction of motor 2
   digitalWrite(in4Pin, HIGH);      // the two inputs are always inverses of each other
   delay(2000);

   // stop
   analogWrite(enable12Pin, 0);
   analogWrite(enable34Pin, 0);

}

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

If it does something else (i.e., turn first or go backwards instead), then you need to flip the HIGH and LOW values for one motor (i.e., one pair of the digitalWrite commands)

Make sure that you get this correct before continuing. Remember the combination of the HIGH and LOW values that you need for the robot to go forward and to turn left.