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.

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() {  
 void loop() {  
  while(mySerial.available()) {   //read the serial input  
   input += (char);  
  if (input.length()>0) {   //if the input is not empty  
   setOutputs(input);    //call the setOutputs function  
 /*set Outputs according to the inputs*/  
 void setOutputs(String input){  
  else if(input.indexOf("B")>-1)  
  else if(input.indexOf("C")>-1)  
  else if(input.indexOf("D")>-1)  
  else if(input.indexOf("1")>-1)  
  else if(input.indexOf("2")>-1)  
  else if(input.indexOf("3")>-1)  
  else if(input.indexOf("4")>-1)  

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.