Introduction: Bluetooth Controlled Arduino RC Car

This Project is something i started for my Microcontroller class. We were tasked with picking either Arduino or Raspberry pi and then get the MCU to control an RC car or do something fancy with an RC car; Then once we decided what we were going to do we were to make a tutorial so that we too could show others what we were doing. I chose to control the remote control car using my Android phone via bluetooth.

For this project you will need these parts(A link to place where to get them will be provided if possible):
1 x Arduino UNO R3 || http://www.adafruit.com/products/170
1 x Motor Shield || http://www.adafruit.com/products/1438
RC Car or Motor or Alternative || Hobbyist Store, Adafruit, Toy Store
RC Car Battery 5v+ || Should come with the RC car if you buy one.
1 x HC-06 Bluetooth Module or Similar || Hobbyist Store, Ebay
1 x Female to female 4 pin cable || N/A, mine came with the bluetooth module
Jumper Cables || Hobbyist Store, Ebay
9v Battery pack || http://www.adafruit.com/products/67
Android Device

Step 1: The Motor Shield.

First of all we need to setup the Arduino and Motor shield. If you bought the motor shield you're going to have to solder, Grab your solder and you soldering iron and follow this tutorial: http://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino/install-headers

Step 2: Setting Up the Arduino Board

Now that you have got your motor shield stack it on top of the Arduino board. Once you have done this you need to setup the HC-06 the correct pins. Attach the female to female jumper cable to the HC-06, than put jumper cables into the other end. on the bottom of the HC-06 there's directions for what goes where VCC And GND are for power, and TXD and RXD are for transmitting and receiving. On the Arduino board; The VCC goes into either the 3v or 5v pin, and the GND goes into the GND PIN, thus powering your bluetooth module. now for the TXD and RXD, the TXD goes into the 2nd digital pin, and the RXD goes into the 3rd digital pin.

Attach motors to M1 header and M3 header. Now plug in power for the Arduino and the car battery to the power header, don't forget to take off the VIN Jumper so that the motor shield is powered separately!

Step 3: The Arduino Code Part 1

The Arduino Code is Relatively simple to understand, once we have declared the things such as motors and pins we can start making serial connections for the bluetooth serial connection; And we can set speeds and turn the motors on, this is all done in the setup function. it should look a little like this so far: 

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
#include <SoftwareSerial.h>

int bluetoothTx = 2;
int bluetoothRx = 3;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *backMotor = AFMS.getMotor(1);
Adafruit_DCMotor *frontMotor = AFMS.getMotor(3);

void setup()
{
  //Setup usb serial connection to computer
  Serial.begin(9600);

  //Setup Bluetooth serial connection to android
  bluetooth.begin(115200);
  bluetooth.print("$$$");
  delay(100);
  bluetooth.println("U,9600,N");
  bluetooth.begin(9600);


  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz

  // Set the speed to start, from 0 (off) to 255 (max speed)
  backMotor->setSpeed(255);
  backMotor->run(FORWARD);
  // turn on motor
  backMotor->run(RELEASE);

  frontMotor->setSpeed(255);
  frontMotor->run(FORWARD);
  // turn on motor
  frontMotor->run(RELEASE);

}


Now we're a step closer to being able to receive messages from our android device. Firstly to start reading the message send via bluetooth and writing to usb serial and it looks a something like this

void loop()
{
  //Read from bluetooth and write to usb serial
  if(bluetooth.available())
  {
    char toSend = (char)bluetooth.read();
    Serial.print(toSend);
  }

// Only really used to test to see if the Arduino is receiving the correct message
//Read from usb serial to bluetooth
if(Serial.available())
  {
    char toSend = (char)Serial.read();
    bluetooth.print(toSend);

  }
}

A summary for what our code does so far is: Receives a message from an android device that is running the correct app  via Bluetooth and then prints it to the serial monitor.  we'll touch on getting the motors to work in part Two of the Arduino code

Step 4: The Arduino Code Part 2

to get motors working for you need to add code to the if(Bluetooth.available) code so it will look like this:

if(bluetooth.available())
  {
    char toSend = (char)bluetooth.read();
    Serial.print(toSend);


    // After the android device sends the data the arduino takes the information and reads it as a char,
    // This code is here to read that data and turn it into actions for the motors.
    if(toSend == 'f'){
      backMotor->run(FORWARD);
    }

    if(toSend == 'b'){
      backMotor->run(BACKWARD);
    }

    if(toSend == 'l'){
      frontMotor->run(FORWARD);
    }

    if(toSend == 'r'){
      frontMotor->run(BACKWARD);
    }

    if(toSend == 'rf'){
      frontMotor->run(BACKWARD);
      backMotor->run(FORWARD);
    }

    if(toSend == 'lf'){
      frontMotor->run(FORWARD);
      backMotor->run(FORWARD);
    }

    if(toSend == 'rb'){
      frontMotor->run(BACKWARD);
      backMotor->run(BACKWARD);
    }

    if(toSend == 'lb'){
      frontMotor->run(FORWARD);
      backMotor->run(BACKWARD);
    }

    if (toSend == 's'){
      frontMotor->run(RELEASE);
      backMotor->run(RELEASE);
    }

  }

in the if statements that turn the motors there are letters such as "f" for this first if statement, this is the message that is received from pressing the forward button on the Android App! Now that we can read a message sent from an Android device we can run an app to send these messages!


Step 5: Android App!

Unfortunately due to time constraints I'm not providing a tutorial on the android app, however i will provide the .apk and the java project for it so you can see what i have done. to open the java project you're required to have java and the Android SDK tools
you can download it as a bundle here: http://developer.android.com/sdk/index.html

the app itself is easy to use, simply pair your Android device and the Bluetooth module you used together; once you have paired to two devices together launch the app and enter the name of the paired device, hit the Connect Button or the "C" Button wait for a message to say that the devices are connected. Now that its connected you can control your Arduino RC car with your Android Device!!

Step 6: Hopefully This Helped!

Well hopefully this helped, it sure would have helped me if i had it when i was making the RC car!

Here are some links that are useful and for the code!
http://www.mediafire.com/download/0sf50f7vp5csn6c/bluetoothRC.apk
http://www.mediafire.com/download/batlk0hit55yoz8/blueToothRC.zip
http://www.mediafire.com/download/hzi93iigvbwrtr8/ArduinoRCcar.ino
http://www.adafruit.com/
http://developer.android.com/sdk/index.html
http://arduino.cc/



Thank you for reading my tutorial or guide, Constructive criticism is welcomed! Thanks again!