DEV Community

Cover image for Building a Bluetooth Controlled Car Step by Step
Thiago Farias
Thiago Farias

Posted on

Building a Bluetooth Controlled Car Step by Step

Chapter 1 - Overview

🎯 Objective

Build a bluetooth controlled car with arduino and a cellphone

🎮 User diagram

User diagram of functions used in this projects

🔌 Components

  • 1 Arduino Uno
  • 1 4WD Robot Car Chassis Kit
  • 4 DC motors
  • 1 H L298N
  • 2 battery 9V
  • 1 Bluetooth HC-05
  • connectors
  • screws

🦉 References

  1. Frederik Hauke for Arduino Bluetooth Terminal app
  2. roboindia.com
  3. forum.arduino.cc
  4. blog.eletrogate.com
  5. Andi.Co for Bluetooth RC Car available on playstore

Chapter 2 - Project Step by Step

Step 1: Communicating between the cell phone and the arduino

I used the tutorial from roboindia.com
to understand how HC-05 works and used this app on my phone to communicate with bluetooth module

The result

Image description

String BT_input;
int LED = 2;
void setup()  
{  
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
  while (!Serial) 
  {
  }
}

void loop() 

 { 
  if (Serial.available())
    {   
        BT_input = Serial.readString();

        if (BT_input=="A")                
        {
          digitalWrite(LED, HIGH);
          Serial.println(BT_input);
          Serial.println("LED is ON");
        }
        else if (BT_input=="B")
        {
          digitalWrite(LED, LOW);
          Serial.println(BT_input);
          Serial.println("LED is OFF");
        }
        else 
        {
          Serial.println(BT_input);        
          Serial.println("Send 'A' to get LED ON");
          Serial.println("Send 'B' to get LED OFF");
        }
    }

}
Enter fullscreen mode Exit fullscreen mode

Step 2: Powering Arduino With a Battery

I used the blink code that i found in example/01. Basics/ to see if everything is ok. However, my battery is 6V and the arduino power is 5V, searching the internet i found this .So, i used a diode to reduce the voltage.

The result

Image description

Step 3: Communicating between the motors and arduino using H L298N

I read this tutorial to understand how H L298N works. So, i used 6v powering scheme and wrote a file to know if everything is going right

Result

Image description

void setup() {
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
}

void loop() {
  digitalWrite(2, HIGH);
  digitalWrite(3, LOW);
  delay(5000);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  delay(5000);
  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
  delay(5000);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  delay(5000);     
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Everything together

Now we want to put everything together. First of all, i found out that powering with just 6V was not enought, so i switched to 2 9v batteries, one for H L298N and other for the others components

Final result

Image description

String input;
int M1_A = 12;
int M2_A = 11;
int M1_B = 10;
int M2_B = 9;

void setup() {
  // right side
  pinMode(M1_A, OUTPUT);
  pinMode(M2_A, OUTPUT);
  // left side
  pinMode(M1_B, OUTPUT);
  pinMode(M2_B, OUTPUT);

  Serial.begin(9600);
  while (!Serial) 
  {
     // wait for serial port to connect. Needed for native USB port only
  }
}

void loop() {

  if (Serial.available())
    {   
        input = Serial.read();   // read input string from bluetooth
        Serial.println(input);

        if(input == "70") {
          Serial.println("foward");
          foward();
        }
        else if(input == "66"){
          Serial.println("back");
          back();
        }
        else if(input == "76"){
          Serial.println("left");
          rotateLeft();
        }
        else if(input == "82"){
          Serial.println("right");
          rotateRight();
        }
        else {
          Serial.println(input);
          stop();
        }


    }
  else{
    Serial.println("NOT AVAILABLE");
  }

}

int foward(){
  digitalWrite(M1_A, HIGH);
  digitalWrite(M2_A, LOW);
  digitalWrite(M1_B, HIGH);
  digitalWrite(M2_B, LOW);
}

int back(){
  digitalWrite(M1_A, LOW);
  digitalWrite(M2_A, HIGH);
  digitalWrite(M1_B, LOW);
  digitalWrite(M2_B, HIGH);
}

int rotateRight(){
  digitalWrite(M1_A, LOW);
  digitalWrite(M2_A, LOW);
  digitalWrite(M1_B, HIGH);
  digitalWrite(M2_B, LOW);
}

int rotateLeft(){
  digitalWrite(M1_A, HIGH);
  digitalWrite(M2_A, LOW);
  digitalWrite(M1_B, LOW);
  digitalWrite(M2_B, LOW);
}

int stop(){
  digitalWrite(M1_A, LOW);
  digitalWrite(M2_A, LOW);
  digitalWrite(M1_B, LOW);
  digitalWrite(M2_B, LOW);
}
Enter fullscreen mode Exit fullscreen mode

Final photo

Image description

Thanks for your time, fell free to contact me.

Github repo

Top comments (0)