Saturday, February 10, 2018

Connecting Arduino to Android with HC-06 Bluetooth module

HC-06 Bluetooth module

Many of my colleagues and friends often ask me help on connecting their Arduino boards with Android apps, so that they could control external devices using their smartphones. For this task, I always recommend the HC-06 Bluetooth slave module. It is cheap, reliable and does the job with a minimum setup.  

How does it work?

HC-06 works as a UART to Bluetooth radio, which broadcasts whatever the data read from the UART interface, and vice versa. Therefore, no special commands are required to communicate over Bluetooth using HC-06. It will simply transmit the data sent to it by using Arduino's Serial.print() function.

Hooking up HC-06 with an Arduino UNO


Getting Started: The Android app

To get started, I recommend using the Bluetooth Control for Arduino from Play Store. This app provides a good interface to control digital outputs.


Each of the four large buttons on the front screen of this app could be used to turn on and off four digital output pins separately.  When each button is pressed for the first time, they will send out the characters '1', '2', '3' and '4' respectively. When the are pressed again, the characters 'A', 'B','C' and 'D' will be sent out respectively.
The first set of characters could be used to turn on the outputs, and the second set of characters could be used to turn them off. 

Getting Started: The Arduino code


 #include <SoftwareSerial.h>  
 SoftwareSerial mySerial(10,11); //Software Serial, RX=pin 10, TX=pin 11  
   
 const int output1=3;  
 const int output2=4;  
 const int output3=5;  
 const int output4=6;  
 String input;  
   
 void setup() {  
  Serial.begin(9600);  
  mySerial.begin(9600);  
  pinMode(output1,OUTPUT);  
  pinMode(output2,OUTPUT);  
  pinMode(output3,OUTPUT);  
  pinMode(output4,OUTPUT);  
 }  
   
 void loop() {  
  while(mySerial.available()) {   //read the serial input  
   input += (char)mySerial.read();  
   delay(5);  
  }  
  if (input.length()>0) {   //if the input is not empty  
   setOutputs(input);    //call the setOutputs function  
  }  
  input="";  
 }  
   
 /*set Outputs according to the inputs*/  
 void setOutputs(String input){  
  if(input.indexOf("A")>-1)  
   digitalWrite(output1,LOW);  
  else if(input.indexOf("B")>-1)  
   digitalWrite(output2,LOW);  
  else if(input.indexOf("C")>-1)  
   digitalWrite(output3,LOW);  
  else if(input.indexOf("D")>-1)  
   digitalWrite(output4,LOW);  
   
  else if(input.indexOf("1")>-1)  
   digitalWrite(output1,HIGH);  
  else if(input.indexOf("2")>-1)  
   digitalWrite(output2,HIGH);  
  else if(input.indexOf("3")>-1)  
   digitalWrite(output3,HIGH);  
  else if(input.indexOf("4")>-1)  
   digitalWrite(output4,HIGH);  
  }  

This code will turn on and off four LEDs attached to digital pins 3, 4, 5 and 6. They could be replaced with relays to control devices that requires AC mains to operate. Eventhough there are other complex methods to get this task done, this simple method is useful as it keeps things simple and does its job well.