Step-by-Step: Bluetooth Controlled Car


I have always loved RC cars, but sadly I got to buy one when I was no more a kid. Recently I published an app to control any Bluetooth enabled car or robot. So why not to make a Bluetooth car to test it! Here I’m sharing step by step guide you can follow to make your own bluetooth controlled car with speed control.

To complete this project, you should have some basic electronics knowledge. If you have never worked on Arduino then first make a hello world project with Arduino to learn the basics.

Required Hardware
  • L298 Dual H-bridge IC
  • 2 x 0.5 ohm 2W resistors
  • Arduino Uno, Nano or any other version with at least 2 pwm channels.
  • HC-05 Bluetooth module
  • 7.2V LiPo/Li-Ion battery (I used two mobile phone batteries connected in series)
  • Veroboard and soldering equipment
  • 2 x Tyre + motor
  • 1 x Caster wheel
  • Car chasis

If you want, you can skip the first two parts in the list and buy prebuilt Motor Driver Shield instead. It will save you from soldering.

Required Software

Step 1: Create the Motor Driver Circuit

Skip this step in case you choose to buy prebuilt Motor Driver Shield

H-Bridge is a circuit used to drive the motor in both directions. L298 makes H-Bridge circuit very simple. Here is the schematic diagram given for the circuit.

H-bridge

Step 2: Program Arduino

We will use BlueDuino app to control our car via Bluetooth. This app works on any windows 10 device. If you wanna control it using android phone, you might need to tweak the Arduino code a bit. Download the code (.ino file) and then upload it to Arduino. Make sure nothing is connected with the Rx pin of Arduino before you upload your code, otherwise upload will fail.

Reading a Chunk from Bluetooth
String ReadLine()
{
  String s;
  char c;

  while (1)
  {
    if (Serial.available())
    {
      c = Serial.read();
      if (c == '*')
      {
        continue;
      }
      else if (c == '#')
      {
        return s;
      }
      else
      {
        s += c;
      }
    }
  }
}

This function will return a string in a format like this: X,Y , where X and Y are single byte ASCII values of x-axis and y-axis of the joystick.

Getting Joystick Position

The app sends data in a format like this *X,Y# . Every command starts with * and ends with #. The app sends this string whenever the joystick is moved. Here X and Y are unsigned char. It’s value can vary from 0-255. When the joystick is in mid then x-axis = 127, y-axis = 127. On serial monitor a command might look like this *a,<#

void UpdateAxis(int *x, int *y, String chunk)
{
  String s1, s2;
  bool comma;

  if (s.length() != 3) return;

  *x = (int)(byte)s[0] - 127;
  *y = (int)(byte)s[2] - 127;
}

x and y are stored as global variables in the Arduino code.

Translating Axis Values to Motor Speeds

Now we need to convert x-axis , y-axis values to drive motors using differential drive. When joystick is at extreme forward i.e. x-axis = 255, y-axis = 127, then both motors should have maximum speed in the forward direction.

void ToDifferentialDrive(int xValue, int yValue)
{
    if (yValue > 0)
  {
    if (xValue < 0)
    {
      left = mag + (HANDLING * xValue);
      right = mag;
    }
    else
    {
      left = mag;
      right = mag - (HANDLING * xValue);
    }
  }
  else
  {
    if (xValue < 0)
    {
      left = -mag;
      right = -mag - xValue;
    }
    else
    {
      left = -mag + xValue;
      right = -mag;
    }
  }
}

This function translates the x-axis and y-axis values into left and right motor speeds and updates the respective global variables. HANDLING is a constant that can be anywhere between 0-1. For me 0.6 is a suitable value.

Driving Motors

Now finally we will use those speed values to drive the motors.

void DriveMotor(int motor, int spd, int maxSpeed)
{
  spd = 255 * ((double)spd / maxSpeed);
  
  Serial.print(spd);
  Serial.print(" === \t");
  if (spd > 0)
  {
    analogWrite(motorPins[motor][0], spd);
    digitalWrite(motorPins[motor][1], LOW);
  }
  else
  {
    analogWrite(motorPins[motor][0], (255 + spd));
    digitalWrite(motorPins[motor][1], HIGH);
  }
}

maxSpeed determines the peak value of the left and right variables, which is 128. Increasing this value doesn’t increase the speed, so don’t change it!

Step 3: Connect the Circuit

  • Connect the Arduino and motor driver circuit as follows

Arduino     Motor Driver

pin 5 ———- pin 1

pin 6 ———- pin 2

pin 9 ———- pin 3

pin 10 ——– pin 4

5V ———— pin 5

GND——— pin 6

  • Now connect motors with H-Bridge:

Motor Driver         Motor

Out 1 —————  Motor 1 pin 1

Out 2 —————- Motor 1 pin 2

Out 3 —————- Motor 2 pin 1

Out 4 —————- Motor 2 pin 2

  • OR If you are using Motor Driver Shield:
L298 Motor Driver

Motor Driver Shield

Arduino     Motor Driver Shield

pin 5 ———- Input 1

pin 6 ———- Input 2

pin 9 ———- Input 3

pin 10 ——– Input 4

5V ———— 5V

GND——— GND

Also connect battery +ve to the terminal specified in the picture above and battery -ve to GND.

In case the motor turns opposite, swap the motor terminals for the respective motor.

  • Connect the Bluetooth module (HC-05/HC-06) to Arduino as:

Arduino         Bluetooth Module

Tx —————- Rx

Rx —————- Tx

3.3V ————- Vcc

GND ———— GND

Now you can power the Arduino directly through the battery using the power jack. On power jack, Arduino can bear upto 20V but the recommended voltage is 7-12V.

Step 4: Assemble the Car

Now we have everything ready, let’s assemble our car. My car is ugly as I don’t like to spend my time on decorating it but of course you can break convention of the geeks and make it beautiful.

Bluetooth car

Motors and hbridge

Motors and H-Bridge

 

Step 5: Start Driving!

After you download BlueDuino app from windows store. If you want to control the car using an Android or iPhone then you might need to tweak the code a bit according to the app you are using.

  1. Pair your phone to your HC-05 and connect using mobile phone settings. Default pairing code is 1234.
  2. Open the app and connect to your HC-05 from app.
  3. Go to joystick tab in the app and control your car using joystick!

If you experience problem in step one then make sure your device supports Bluetooth Smart Ready not Bluetooth Smart (BLE) and also check if Bluetooth is turned on. If your device is Bluetooth Smart then you require BLE module which is currently not supported by the app.

You can download the code here: BluetoothCar.ino

2 thoughts on “Step-by-Step: Bluetooth Controlled Car

    1. it depends on the app.. first u need to see the data sent by the app on Arduino serial monitor.. then modify the ReadLine() and UpdateAxis() functions accordingly

Leave a Reply

Your email address will not be published. Required fields are marked *