Building a Bluetooth Controlled Car Step by Step
Tags: arduino, tutorial, programming, beginners
Published: 2023-06-02 20:04:34

## Chapter 1 - Overview

### :dart: Objective
Build a bluetooth controlled car with arduino and a cellphone
### :video_game: User diagram

![User diagram of functions used in this projects](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zakhwr15g74k39izjkv0.jpg)

### :electric_plug: 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

### :owl: 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](https://roboindia.com/tutorials/bluetooth-terminal-android/)
to understand how HC-05 works and used [this app](https://play.google.com/store/apps/details?id=com.frederikhauke.ArduTooth) on my phone to communicate with bluetooth module

#### The result

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6tyhsprqepn2ypomtrt4.png)

```
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");
        }
    }
 
}
```

### 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](https://forum.arduino.cc/t/how-to-reduce-6v-to-5v-dc/1051627) .So, i used a diode to reduce the voltage.

#### The result

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bmt6owgajbx4mcg0uo44.png)

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

I read [this tutorial](https://blog.eletrogate.com/guia-definitivo-de-uso-da-ponte-h-l298n/) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ebkatn3t6a6f70a8w8ad.png)

```
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);     
}
```

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gwjm2wk3dgxjhne6zm4c.png)

```
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);
}
```
### Final photo

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lhuchk3oesze5lv618ak.png)

Thanks for your time, fell free to contact me.

[Github repo](https://github.com/Thiagodcfarias/controle-remoto-arduino)