• nithya7rns

Interfacing HC-05 Bluetooth module with Arduino

Updated: Dec 31, 2020

We all have heard about home #Automation and some of us have been using automated devices at home. We always have thought of automated devices to be something complex and tough but every device can be designed easily from the basic design connections.


In this article, we are going to build a circuit that can control an LED using a Bluetooth connection through the Arduino Bluetooth controller app. So, let's get started.


Hardware Requirements


  1. Arduino UNO

Develop a stronger concept in Arduino through this article:- What is Arduino?

Arduino UNO

Arduino board is a microcontroller that is used to accept inputs from sensors connected and provide an output action on the desired device connected to it. The sensor inputs can be that from light-detecting sensors, flame sensors, temperature sensors, etc. The output function done by the devices can be turning on an LED, the sound of a buzzer, or a Serial monitor display.


Buy Arduino UNO from here.


2. HC-05 Bluetooth Module


HC-05 Bluetooth Module

It is a Serial Bluetooth module for Arduino and other microcontrollers. The range of Operating Voltage is 4V to 6V. It operates with a current of 30mA. The Bluetooth devices have a certain radius of distance inside which it can communicate to the device connected. This device has a range of around 100m. The Bluetooth module uses serial communication to control other devices connected. It can operate in Master, Slave, or Master/Slave mode though the default mode is Slave mode.


Buy the HC-05 Bluetooth Module from here.


3. Jumper Wires


Jumper Wires


These are the main components that are used to establish the connections between different devices of the circuit.


Buy Jumper Wires from here.


4. LED

Build your technical concepts in LED by this article:- Light Emitting Diode


LED

Light Emitting Diode is a commonly used light source. It is a semiconductor. When current flows through it, LED emits light.

Buy LED from here.


5. Resistor


1.5k Ohm Resistor

Resistors are passive devices that restrict the flow of current or divide the voltage through the circuit. The resistors used for the proper functioning of this circuit are 1.5k Ohm Resistor.


Buy Resistors from here.


Software Requirements


  1. Arduino IDE


Arduino IDE Logo

#Arduino IDE (Integrated Development Platform) which is used to interface the boards like NodeMCU to run the desired code designed to interface hardware devices.


2. Arduino Bluetooth Controller App for Android



Arduino bluetooth controller- all in one App - logo

This is an Application for Android devices that is used to interface the device or circuit built. It allows controlling operations using Buttons, Timers, Dimmers, and Controllers, etc.



Circuit Connections


Let's see the connections made between the Arduino UNO Board and the HC-05 Bluetooth module.


Circuit Connections

The below image shows the pins of the HC-05 #Bluetooth chip.


HC-05 Bluetooth module

There are 6 pins in the Bluetooth module out of which we will be using 4 pins. We will use VCC, GND, TXD, and RXD pins. The VCC pin as we know is for the power supply. VCC of the HC-05 module is connected to the 5V pin of the Arduino Board. The GND terminal of the Bluetooth module is connected to the Ground pin of the Arduino Board.


TXD stands for #Transmission of Data and RXD stands for #Receiving Data. TXD terminal of the Bluetooth module is connected to the RX of the Arduino Board and the RXD of the Bluetooth module is connected to the TX of the Arduino. This means that both the HC-05 module and the Arduino can receive and transmit data. The data received by the Bluetooth module is transmitted to the Arduino board and vice versa. The same happens with the receiving end also.


The Resistor is connected from the Negative terminal (shorter terminal) of the #LED to the Ground (GND) pin of the Arduino. Finally, the Arduino UNO Board is connected to the system where the #IDE is installed and the code is written.


NOTE: The connections made to the TX and RX pins on the Arduino board need to be removed before interfacing the Arduino to the IDE. Ignoring this can lead to faulty operation because, as mentioned earlier, the Arduino Board is also capable of transmitting and receiving data of its own and this might lead to incorrect or unwanted data.



Code


The code explanation is as follows.

#define ledPin 13

First, a constant is defined named "ledPin" which holds the value 13 throughout the program. This is basically the PIN of the Arduino board to which the LED is connected.

int state = 0;

A variable ''state" is declared and is initialized to 0. (LOW)

void setup() {
  pinMode(ledPin, OUTPUT);

Under the void setup( ) function, the Mode of the pin is assigned to which the LED is connected using the pinMode( ) function. The mode is set as OUTPUT as the LED is going to glow when the input is given is successful.

 digitalWrite(ledPin, LOW);

The digitalWrite( ) function is used to assign the first value of the LED pin. Here we declare the LED Pin to be LOW. So, the LED is OFF (doesn't glow) in the beginning.

Serial.begin(38400); 
}

The serial transmission begins with the baud rate of 38400 which is usually the value required for communicating with the Bluetooth module. The setup( ) function ends here.

void loop() {
  if(Serial.available() > 0){  
    state = Serial.read();     //Statement 1
    Serial.println(state);     //Statement 2
 }

Next, the loop( ) function is defined. The loop( ) function contains an 'if'' condition which checked if the data is received through Serial transmission i.e. from the Serial port.


If the condition is True, the value of the state variable, declared earlier and initialized to be 0 (LOW), will now be rewritten with the value the Serial port receives. The Serial. read( ) function does the same as shown by Statement 1 in the above code. After reading the value, the value of the "state" is printed on the Serial Monitor as shown in Statement 2. The 'if' condition ends here.

if (state == '0') {
  digitalWrite(ledPin, LOW);     // Statement 1
  Serial.println("LED: OFF");   // Statement 2
  state = 0;                    // Statement 3
 }

Another "if" is written to check the value of the variable "state". If the value of the state variable is 0, Statements 1,2,3 execute accordingly. The LED pin is assigned with the value 0 or LOW using the digitalWrite( ) function. This means that when state=0, the LED is OFF. The same is printed on the Serial Monitor. "LED: OFF" (Statement 2).


Again, at the end of the "if" condition, the value of the state variable is assigned back to 0. This "if" condition ends here.

else if (state == '1') {
  digitalWrite(ledPin, HIGH);   //Statement 1
  Serial.println("LED: ON");    //Statement 2
  state = 0;                    //Statement 3
 } 
}

Lastly, an "else if" condition is written again to check the value of the state variable. If the value of the state variable is 1 (HIGH), Statements 1,2, and 3 are executed. The LED pin value is now written to be HIGH and so the LED glows (ON). The same is printed on the Serial Monitor using Statement 2. "LED: ON".


Again the value of the variable "state" is initialized back to 0. The "else if" condition ends here followed by the loop( ) function.


COMPLETE CODE


#define ledPin 13
int state = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  Serial.begin(38400); // Default communication rate of the Bluetooth module
}

void loop() {
  if(Serial.available() > 0){ // Checks whether data is coming from the serial port
    state = Serial.read(); // Reads the data from the serial port
    Serial.println(state);
 }

 if (state == '0') {
  digitalWrite(ledPin, LOW); // Turn LED OFF
  Serial.println("LED: OFF"); // Send back, to the phone, the String "LED: ON"
  state = 0;
 }
 else if (state == '1') {
  digitalWrite(ledPin, HIGH);
  Serial.println("LED: ON");;
  state = 0;
 } 
}

For the complete code for this program, download the following document.


Code
.txt
Download TXT • 670B

Note: Please change the file format to .ino(Arduino IDE) format before you upload the code to the Arduino board.



Working


We can understand the working of this circuit in stages.


Image of Device when interfaced.

Stage 1: Configuring the Arduino Bluetooth Controller App.

First, we have to switch on Bluetooth in our #smartphone and search for the HC-05 Module, whose name will be displayed in terms of a few digits like an IP Address. (refer to video). Once the device is connected and paired to the phone, open the App downloaded, search for the devices using the search icon. Select the HC-05 module to configure buttons, dimmers, etc according to how we want to control the device.


In this project, we will configure a button to control the LED. The button icon is selected after the #HC-05 module is paired and connected. A name should be given to the button and particular numerical values need to be given to the input control and output control (Refer to video). Then using the button, we can control the LED by turning it ON or OFF from our smartphone.


Stage 2: Connections between the Arduino and Bluetooth Module.

After we have done the connections and have interfaced the device to our smartphone, we need to know how the device works. The command sent from our phone is received by the Bluetooth module. The same command is then sent to the Arduino board. On receiving the command, which is a LOW or HIGH value for the LED, the LED is turned OFF or ON. This way we can control the LED by #interfacing the HC-05 Bluetooth module with Arduino.


NOTE: The Bluetooth #module and the Smartphone must be present in the given radius or range of distance. For this HC-05 module, it is around 100m. Only then we will be able to establish a stable connection between our phone and the Bluetooth device.


Watch the video given below for the practical demonstration of the Project.


Project video By - Farhan Ahamed

This is just a basic step towards #HomeAutomation systems. But is equally necessary and vital to know how the device works from scratch as this gives scope for designing bigger devices with more components that can be controlled automatically through smartphones with Bluetooth connection. Start designing your device now and explore.


SEE ALSO:

  • Facebook
  • YouTube
  • Instagram
  • LinkedIn
LearnElectronics
Contact Us
© Copyright - LearnElectronics 2020.