Introduction: Simple Bluetooth RC Car (Arduino Nano + HC-05)

About: Lazy Software Developer

I will share my simple project on how to use your android phone to control robot car.

Step 1: The Hardware

  • 1 x Arduino Nano
  • 1 x HC-05 Bluetooth module
  • 1 x Motor Driver Module (TB6612FNG)
  • 2 x Motors ( with Wheel)
  • 1 x Battery for Arduino
  • 2 x Battery for Motors

Step 2: Connections

Arduino - HC-05

  • D11 - Rx
  • D10 - Tx
  • +5v - VCC
  • GND - GND

Arduino - TB6612FNG

  • D3 - PWMA
  • D4 - AIN2
  • D5 - AIN1
  • D6 - STBY
  • D7 - BIN1
  • D8 - BIN2
  • D9 - PWMB
  • GND - GND

TB6612FNG - Battery

  • GND to V-
  • VM to V+

Arduino - LED (Optional)

  • D13 - LED+
  • GND - LED-

Step 3: Codding

/* RC_Bluetooth_2018_v6<br> * By:    Khamlek HOMSOMBATH
 * Date:  Jan 17, 2018
 * Download Controller for Android : <a href="https://play.google.com/store/apps/details?id=com.lekpkd.duinojoy">  https://play.google.com/store/apps/details?id=com...>
 */

#include <softwareserial.h></softwareserial.h><SoftwareSerial.h>

// arduino>>bluetooth
// D10 (as RX)   >>>  Tx
// D11 (as TX)  >>>  Rx
SoftwareSerial bluetooth(10, 11); // RX, TX

#define ledpin 13 // Connected to LED

#define PWMA 3
#define AIN2 4
#define AIN1 5
#define STBY 6
#define BIN1 7
#define BIN2 8
#define PWMB 9
void setup() {
  Serial.begin(19200); 
  bluetooth.begin(9600);
  
  pinMode(ledpin,OUTPUT);
  pinMode(PWMA,OUTPUT); // PWM A
  pinMode(PWMB,OUTPUT); // PWM B
  pinMode(BIN1,OUTPUT); // B
  pinMode(BIN2,OUTPUT); // B
  pinMode(AIN1,OUTPUT); // A
  pinMode(AIN2,OUTPUT); // A
  pinMode(STBY,OUTPUT); // STBY

  digitalWrite(STBY,1);
analogWrite(PWMA, 0);
  analogWrite(PWMB, 0);
} 
String data = "";
void loop() {
  while(bluetooth.available()){
    char a = bluetooth.read();
    if(a==')') {
      setData(data);
      data = "";
      return;
    }
    data += a;
  }
}
int ledVal = 0;
void setData(String data){
  data.trim();
  int index = data.indexOf(",");
  if(index != -1){
    int angle = data.substring(0,index).toInt();
    int strength = data.substring(index + 1).toInt();

    digitalWrite(BIN1,0);
    digitalWrite(BIN2,0);
    digitalWrite(AIN1,0);
    digitalWrite(AIN2,0);
    if (angle < 180){
      digitalWrite(BIN1,1);
      digitalWrite(AIN1,1);

      if(angle < 90){
        analogWrite(PWMA, strength * 255 / 100);
        analogWrite(PWMB, strength * (angle * 255 / 90) / 100);
      }else if(angle > 90){
        analogWrite(PWMA, strength * ((180 - angle) * 255 / 90) / 100);
        analogWrite(PWMB, strength * 255 / 100);
      }else{
        analogWrite(PWMA, strength * 255 / 100);
        analogWrite(PWMB, strength * 255 / 100);
      }
    }else if(angle > 180){
      digitalWrite(BIN2,1);
      digitalWrite(AIN2,1);
      
      if(angle > 270){
        analogWrite(PWMA, strength * 255 / 100);
        analogWrite(PWMB, strength * ((360 - angle) * 255 / 90) / 100);
      }else if(angle < 270){
        analogWrite(PWMA, strength * ((90 - (270 - angle)) * 255 / 90) / 100);
        analogWrite(PWMB, strength * 255 / 100);
      }else{
        analogWrite(PWMA, strength * 255 / 100);
        analogWrite(PWMB, strength * 255 / 100);
      }
    }
    return;
  }

  if(data.equals("a")){
    if(ledVal==1) ledVal=0; else ledVal=1;
    digitalWrite(ledpin, ledVal);
    return;
  }
  
  Serial.print("Recieved: ");
  Serial.println(data);
}

Step 4: Testing

Please download Duino Joy Android App at

https://play.google.com/store/apps/details?id=com....

  1. Pair your Arduino Car to your Mobile phone in bluetooth setting on your phone
  2. Open Duino Joy app
  3. Click 'Connect' button
  4. Select 'HC-05' or any name you set for your HC-05
  5. Let's play!!