Connect Arduino with Android using BT Communication

Introduction

 
In my previous article, I explained working with Ultra Sonic Sensor, and in this article, I'll show you the connection between Arduino and Android using Bluetooth Module.
 
 Requirements:
  • Arduino Uno
  • Bluetooth Module HC-05
  • Android Phone
  • Mit App Invertor 
  • Led
  
Connections:
  
Bluetooth Module to Arduino:
  1. Vcc to 5v
  2. Gnd to Gnd
  3. Rx pin to Tx Pin
  4. Tx pin to Rx Pin 
  5. Led to Digital Pin 5
MIT App Invertor:
  1. Go to MIT App Invertor 2.
  2. Start a new project.
  3. And the design of the app for our BT Connection.
  4. In the left side corner, we have the palette windows in that we have the required tools just drag and drop and complete the design process.
  5. After that, we want to make the code for our design part go to the Blocks section, and make the following code shown in figure2.
                                          
Figure 1: Designer
 
BLOCKS WINDOW
 
 
Figure 2: Blocks  
 
Arduino BT Program:
  1. int ledpin = 5;  
  2. String readString;  
  3. void setup()  
  4. {  
  5.     Serial.begin(9600); // Baud Rate  
  6.     pinMode(ledpin, OUTPUT);  
  7. }  
  8.   
  9. void loop()   
  10. {  
  11.     while (Serial.available()) //Send data only when you receive data  
  12.     {  
  13.         delay(4); //delay time  
  14.         char c = Serial.read();  
  15.         readString += c;  
  16.     }  
  17.     if (readString.length() > 0)  
  18.     {  
  19.         Serial.println(readString);  
  20.         if (readString == "ON")   
  21.         {  
  22.             digitalWrite(ledpin, HIGH); // LED ON  
  23.         }  
  24.         if (readString == "OFF")   
  25.         {  
  26.             digitalWrite(ledpin, LOW); //LED OFF  
  27.         }  
  28.         readString = "";  
  29.     }  
  30. }  
Output: 
 
 
Explanation: 
 
Initialize the variable ledpin=5 declare the string as readString.
 
void Setup()
 
In the Setup() function set the baud rate 9600 it sends the data bits per/sec and sets the pinMode to led as OUTPUT and.
 
void loop():
 
In the loop() function we want to make the conditions of how BT works based upon our conditions of how the Bluetooth will act.
 
Serial.available:
  • Serial.available is one type of function.
  • Serial is used for the communication between the Arduino and other devices like computers, mobile phones, etc.
  • All the Arduino boards have an RX pin and the TX pin.
  • Based upon these pins the BT makes communication between the Arduino.
  • And Serial.Read() it reads the incoming serial data from the Bluetooth.
  • If(readString =="ON") this condition is true the led will ON.
  • If(readString =="OFF")  the led is OFF.

Conclusion

 
And finally, we conclude that we made the Connection between Arduino to Android using a Bluetooth Device.