How to Use HC-05 Bluetooth Module With Arduino

Bluetooth

Bluetooth is a technology for wireless communication. It is designed to replace cable connections. Usually, it connects small devices like mobile phones, PDAs and TVs using a short-range wireless connection. And it uses the 2.45Ghz frequency band. The connection can be point-to-point or multipoint where the maximum range is 10 meters. The transfer rate of the data is 1Mbps (or a maximum of 2Mbps).

Bluetooth
Figure 1:
Bluetooth

What we will do

We will create an Arduino-model where we turn on a LED light using Bluetooth commands. And we will that instruction using our Android Phone. In short, we will control our LED using a phone.
And via Bluetooth we will the ON/OFF instruction.

Bluetooth stick
Figure 2: Bluetooth stick ( Pic Courtesy: instructable.com)

So, what we need:

  • Arduino Uno Board
  • Bluetooth Module (HC-05)
  • 1x 3.5mm LED
  • 1k Ohm Resistor
  • Jumper Wires

Most of the pre-requisites we have used already in my past articles, except for the Bluetooth Module HC-05.

Bluetooth Module
Figure 3: Bluetooth Module

It is a class-2 Bluetooth module with Serial Profile that can be used as Master or Slave. In Arduino, we will use Serial Communication for various purposes. Since it also suports Serial Communication and you can treat it as a replacement.

HC-05 Specifications

  • 2.45Ghz Frequency
  • Asynchronous Speed 2.1Mbps (max) .160Kbps
  • Security: Authentication
  • Profile: Bluetooth Serial Port
  • Power Supply: +3.3 VDc
  • Working Temperature: >20C
  • Cost : Around INR 300

HC-05 Description

So, we have six (or four) leads in this module. But we will genuinely care about only four of them. Where, the two are for Vcc and GND.

Vcc= Power Supply (in other words 5V or 3.3V)

GND= Ground (in other words 0 volt)


And the next two leads are for RX (Receiving End) and TX (Transmitting End). From the basic idea, we can say the RX of the module will go to the TX of the Arduino UNO. In the same way, we connect the TX of the module with the RX of the Arduino UNO.

connect TX
Figure 4: Arduino UNO

In addition, you can add a LED to determine the instruction more correctly. So, add a LED to digital PIN 12.

Sketch

In this sketch, we will write the basic code where we are accepting 0 or 1 (in other words true or false) from the serial port and process accordingly.

  1. int LED= 12;  
  2. char input;  
  3.   
  4. void setup() 
  5. {  
  6.   Serial.begin(9600);  
  7.   pinMode(LED, OUTPUT);  
  8.   Serial.println(">> START<<");  
  9. }  
  10.   
  11. void loop() 
  12. {  
  13.   if(Serial.available()>0)  
  14.   {  
  15.     input= Serial.read();  
  16.     if(input=='1')  
  17.     {  
  18.       Serial.println("ON");  
  19.       digitalWrite(LED, HIGH);  
  20.       delay(2000);  
  21.     }  
  22.     else if(input=='0')  
  23.     {  
  24.       Serial.println("OFF");  
  25.       digitalWrite(LED, LOW);  
  26.       delay(2000);  
  27.     }  
  28.     else  
  29.     {  
  30.       Serial.println("NO INPUT");  
  31.       Serial.println(input);  
  32.     }  
  33.   }  
  34.     
  35. }  
Illustration

In the Definition part, we declare the LED variable that accepts 12 (in other words the Digital PIN). And the char input that we will further use for accepting the user's instructions.

Until now, we have just declared two variables.

Next we have a setup() block where we will initialize a LED and serial communication. So, we have pinMode() and Serial.begin().

In the next loop() block we do all the actual coding.

First, we decide whether or not the serial port is available. If yes, then we will do the communication. For that, we have a Serial Method available().

On Yes, we proceed and ask for the user's input via the serial port. And, the read() method is there that accepts input from the serial port.

Whatever value we get from the serial port we store in the input variable that we defined earlier.

And, on input it will decide to turn it on or not.

See, we haven't written any code for Bluetooth communication. So, that's because Bluetooth communication is implicitly Serial Communication. And in Arduino, what you can do with the Serial Monitor in Arduino is that you can also implement Bluetooth Serial Communication.

Before we connect to the Android Phone, install an app that will do the rest of the job.

ArdruDroid

AndruDroid
Figure 5: ArdruDroid by TechBitar

Note: Play Store link.

This App is designed for Arduino Bluetooth Communication. With That SEND button we can send instructions to the HC-05 module. Whereas, the GET button is used to see what HC-05 is returning as return value.

Since, here we have two instructions, 1 (true) to turn on the LED and the other is 0 (false) to turn off the LED.

Output

Use the following procedure.

 

  1. Upload the code to the Arduino UNO where we have successfully attached the Bluetooth HC-05 module.

    Note: When uploading the sketch please remove the RX and TX wires from the Arduino UNO. I don't even know why they don't work when they are attached. After successfully uploading, attach RX and TX to the board.

    Arduino UNO
    Figure 6: Program

  2. After successfully the Uploading check whether the Bluetooth module is working or not. (The easiest way is to see if the RED indicator is blinking).

    blinking RED
    Figure 7: Blinking RED indicator

  3. Now, go to the Bluetooth Settings in your phone and pair your phone with the HC-05 Device. The key is 1234.

    go to Bluetooth Setting
    Figure 8: Bluetooth on

  4. After successfully pairing, open the ArduDroid App and connect with the HC-05 module.

    ArduDroid App
    Figure 9: ArduDroid App

  5. After doing this, now you can send instructions to the HC-05 with your app. So, we are sending 1 as input.

    send instruction to HC
    Figure 10: Send instruction to HC-05

    After sending, you see something like this.

    After sending
    Figure 11: After sending

  6. And 0 to turn off the LED.

     0 to turn off the LED
    Figure 12: 0 to turn off the LED

    And then:

    output
    Figure 13: Turn off the LED

Conclusion

Using the HC-05 Module, I showed you how to implement it with a LED without using any external library. Next, we try to explore something more with this module using analog inputs. Until, then enjoy coding.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now