How to make Bluetooth control Arduino car.


Controlling a car with Bluetooth is fun to do that. Instead of bluetooth module you can use any wireless wifi module but code is littlebit diffrent.
So let's build it.

Component requirement.

Arduino board___(01)
L293D motor driver module___(01)
Dc gear motor 150RPM___(02)
Hc-05 bluetooth module___(01)
Wheels___(02)
Chassis___(01)
Some jumper wires

Circuit diagram.

Arduino code for the project.

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(8, 9); //pin 8 -- BT Tx; 9 -- BT Rx
int m21 = 5;
int m22 = 4;
int m11 = 7;
int m12 = 6;
char state;

void setup() 
{  
pinMode(m11, OUTPUT);  // Digital pin 10 set as output Pin
pinMode(m12, OUTPUT);  // Digital pin 11 set as output Pin
pinMode(m21, OUTPUT);  // Digital pin 12 set as output Pin
pinMode(m22, OUTPUT);  // Digital pin 13 set as output Pin
Serial.begin(9600);
BTSerial.begin(9600);
}

void loop()
{
  while (BTSerial.available() > 0)
  {
  state = BTSerial.read();
  Serial.println(state);
  }

  if( state == 'F') // Forward
    {
      digitalWrite(m11, HIGH);
      digitalWrite(m12, LOW);
      digitalWrite(m21, HIGH);
      digitalWrite(m22, LOW); 
    }
  else if(state == 'B') // Backward
    {
      digitalWrite(m11, LOW);
      digitalWrite(m12, HIGH);
      digitalWrite(m21, LOW);
      digitalWrite(m22, HIGH); 
    }
  
    else if(state == 'L') //Left
    {
    digitalWrite(m11, LOW);
    digitalWrite(m12,  HIGH);
    digitalWrite(m21, HIGH);
    digitalWrite(m22, LOW);
    }
    else if(state == 'R') //Right
    {
    digitalWrite(m11, HIGH);
    digitalWrite(m12, LOW);
    digitalWrite(m21, LOW);
    digitalWrite(m22, HIGH); 
    }
    
  else if(state == 'S') //Stop
    {
    digitalWrite(m11, LOW);
    digitalWrite(m12, LOW);
    digitalWrite(m21, LOW);
    digitalWrite(m22, LOW); 
    }
  else if(state == 'I') //Forward Right
    {
    digitalWrite(m11, HIGH);
    digitalWrite(m12, LOW);
    digitalWrite(m21, LOW);
    digitalWrite(m22, LOW);
    }
  else if(state == 'J') //Backward Right
    {
    digitalWrite(m11, LOW);
    digitalWrite(m12, HIGH);
    digitalWrite(m21, LOW);
    digitalWrite(m22, LOW);
    }
   else if(state == 'G') //Forward Left
    {
    digitalWrite(m11, LOW);
    digitalWrite(m12, LOW);
    digitalWrite(m21, HIGH);
    digitalWrite(m22, LOW);
    }
  else if(state == 'H') //Backward Left
    {
    digitalWrite(m11, LOW);
    digitalWrite(m12, LOW);
    digitalWrite(m21, LOW);
    digitalWrite(m22, HIGH); 
    }
}

// End of program

In this code, we use SoftwareSerial.h library because it is very annoying to disconnect Bluetooth tx and rx pin whenever we want to program Arduino so the help of SoftwareSerial.h library we able to use any digital pin of Arduino as a tx and rx.In void setup, we define the Arduino pin as an output and start serial communication with Bluetooth. After that void loop started and detect any data from the Bluetooth module if Arduino detects any data from Bluetooth it checks for if statement and give output to the motor.
In this project to control car, we use Arduino Bluetooth RC Car. This app also supports gyroscope control.

About the Bluetooth module and setup.



The HC‐05 module is an easy to use Bluetooth SPP (Serial Port Protocol) module, designed for transparent wireless serial connection setup. Serial port Bluetooth module is fully qualified Bluetooth V2.0+EDR (Enhanced Data Rate) 3Mbps Modulation with complete 2.4GHz radio transceiver and baseband. It uses CSR Bluecore 04‐External single chip Bluetooth system with CMOS technology and with AFH (Adaptive Frequency Hopping Feature). 
If you went to customize the name and password of Bluetooth module You went to set up Bluetooth module into at command mode
First, you need to upload this code to Arduino then follow the instruction below.

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(8, 9); // TX | RX
char c=' ';
boolean NL = true;
void setup() 
{
    Serial.begin(9600);
    BTSerial.begin(38400);  
   Serial.print("BTserial started ");
   Serial.println(" ");
  }
void loop()
{
    // Read from the Bluetooth module and send to the Arduino Serial Monitor
    if (BTSerial.available())
    {
        c = BTSerial.read();
        Serial.write(c);
    }
    // Read from the Serial Monitor and send to the Bluetooth module
    if (Serial.available())
    {
        c = Serial.read();
        BTSerial.write(c);   
        // Echo the user input to the main window. The ">" character indicates the user entered text.
        if (NL) { Serial.print(">");  NL = false; }
        Serial.write(c);
        if (c==10) { NL = true; }
    }
}
To enter in at command mode first push the push button and then connect Arduino with the computer then you notice that led start blinking slowly.Then open serial monitor here is the list of command to set up bluetooth module.


Comments

Popular posts from this blog