Sunday

The HC-05 Bluetooth Module with Arduino

Using Bluetooth module with Arduino


Hello friends, let us learn how to connect a HC-05 Bluetooth Transmitter/ Receiver module with a Arduino Uno. And use it to communicate with a mobile smartphone to turn on/ off electrical load/equipment/light connected to the Arduino through a Relay.

The HC-05


I purchased this module from Amazon India. You can also find one for your self on this link.


This is the most used and suitable module for the kind of projects that we are doing here. There is also a newer module available named HC-06 on Amazon, Initially I was tempted to buy the newer version as it was already available. But then soon realized that HC-05 module has a button on it that can be used for entering AT command mode. HC-05 can thus be used either as a master or a slave. HC-05 can initiate connection with other devices. Whereas the HC-06 module is to be used only as a slave mode. Thus it can be only used to accept connections from other devices. Therefore I decided to stay with HC-05 module.


If you notice in the above image, the pins are marked promptly. We are going to need only four pins in it. The VCC and GND can be connected to Arduino 3.3V and GND Pins respectively. Although this module can be connected to 5V pin on Arduino, but what I read elsewhere is that it can operate well with 3.3V and 5V input can be excessive for it. So to be on the safer side, I chose to connect it to 3.3V pin on Arduino.

There are two pins RXD and TXD there Receiver and Transmitter respectively.




If you notice on the Arduino board (Uno) you will see TX and Rx pins at the location of pin number 0 and 1 .  These are the serial communication ports that send and receive data in the ATMEGA 328P microcontroller. We can connect the TXD and RXD of the Bluetooth module to RX and TX of the Arduino board. Remember here TXD goes to RX and RXD goes to TX. That is how the bluetooth and the micro controller will communicate.
☹️ The common error that can happen here is to connect RXD to RX and TXD to TX pins.

As we will be using the TX and RX pins of the Arduino directly, it will interfere with our Arduino sotware, when we will try to upload the code on the Arduino board. So at this stage, right before you want to upload some code to Arduino, remove the wires from RX and TX of Arduino. Once the Upload is completed, you can re insert the wires back (but remember the configuration. Don't connect similar ports to each other)

So this is how you will wire a bluetooth module to a Arduino board. It is better to use a breadboard to insert the bluetooth module, and connect wires. As we will be connecting 3.3 to VCC, and 5V from Arduino to VCC of the Relay. We will need the extra space on the breadboard to accommodate all the wires.

Testing HC-05 module with Arduino


You can connect the HC-05 module with Arduino as below

VCC - 3.3 v
GND - GND
TXD - RX
RXD - TX

After that, connect a LED on the breadboard to a 330 Ohms resistor. And Connect the (+ve) Cathode of the LED with the Pin 7 on Arduino. 

The program that we are using for this experiment is this

void setup() {
 Serial.begin(9600);
 pinMode(7, OUTPUT);
 digitalWrite(7, LOW);
  }


void loop() {
   if(Serial.available()>0)
   {     
      char x = Serial.read();
      switch(x)
      {
        case 'a': digitalWrite(7, HIGH);break; 
        case 'b': digitalWrite(7, LOW);break;
        default : break;
      }
        delay(50);
   } //end switch
} //end loop 


// We have declared pin 7 as output pin, therefore we will connect it with the LED.
//Serial.begin() establishes serial communication between the bluetooth module and it's serial ports (TX, RX)
// digitalWrite(7,LOW) keeps the LED Off at start
//we can check if there is any data present at the Serial port by Serial.available() function
// We need to declare a char variable to store this data and then use it in the program in switch-case
// We are using a character in the case statement. On your smartphone, in the Bluetooth Serial Terminal Pressing 'a' will turn the LED On, and pressing 'b'  will turn it off. If you get accustomed to the Terminal, then there will be no constraint on you if you decide to control multiple ports on Arduino, all you need to do is to append extra code in the case statement, and of course declare the ports as output in the setup() function.

Some of the apps that provide graphical interface to turn on/off Arduino pins are restrained in their number of ports, so there is no need to get frustrated using them. Using a Bluetooth Terminal is the best way to conduct this experiment. In fact you can control all the digital I/O ports ( from 2 to 13) with ease using bluetooth terminal. 

We will try to connect multiple ports in the next experiment.

You can download the program from this link

I have used an App called Arduino Bluetooth Terminal by Fredrik Hauke. You can install it from here

Adding a Voltage divider in the circuit

We need to add a voltage divider between the TX pin of Arduino and The RXD pin of the HC-05 module.
This is because the signal sent by arduino through it's TX pin 5v and the signal level of the HC-05 is 3.3v. The 5v sent by Arduino through its TX pin can be brought down with a voltage divider.
We will use two extra resistors for this. 1 K and 2 K .
We will place these resistors on the breadboard. One end of the 2K resistor in the common rail of the breadboard. Connect one end of the 1 K resistor to the same row after the 2K resistor, leaving a few holes for the wire coming from the RXD pin of HC-05. Insert the other end of the 1 K resistor in the other side of the breadboard. Connect a wire to the other end of this 1K resistor to the TX pin of Arduino. Thus our voltage divider circuit is complete.
Now connect the Gnd pin of arduino to the common rail.
Connect the Gnd pin of HC-05 to common rail.
Connect Vcc of HC-05 to 3.3 v pin on arduino.
Connect TXD pin of HC-05 to RX pin of arduino.
Connect +ve pin of led to the pin 7 of arduino
The completed wiring would look something like this.

After completing the wiring, you can upload the program in Arduino and test the circuit as explained earlier

Control 4 LEDs with Bluetooth


We controlled one LED with Bluetooth. Now let us control 4 LEDs.
We will need 4 LEDs and 4 resistors of 270 Ohms each.
Let us place all the components on Breadboard
Connect a resistor each between the negative pins of the LEDs and the common rail on breadboard. We are using two separate sections of the breadboard for this circuit, therefore we also need to join the common rails of the two sections later.
Connect a wire between the GND pin of Arduino and the common rail on breadboard.
Connect a wire between the common rail on breadboard and the GND pin of Bluetooth module
Connect a wire between VCC of the Bluetooth module and the 3.3v pin of Arduino
Connect a wire between the TXD pin of the Bluetooth module and the RX pin of Arduino
Connect a wire between the two voltage divider resistors and the RX pin of the Bluetooth module.
Connect a wire between the 1K resistor and the TX pin of Arduno.
We will now connect a wire each to the positive pin of the LED and the digital pins of the Arduino. I have used pin 7, 8, 9, and 10
Now connect a wire between the two common rails on the breadboard. Now the wiring is complete.
Download the program code, open it in your computer and upload it in Arduino. Download the Code file from this link
Now open the App that we had previously installed.
THe LEDs will turn on if you send A, B, C and D. Whereas they will turn off if you send a, b, c, an d. You can also send more than one character and see how the LEDs react to it.

Featured Post

Creating Games in Scratch for ICSE Class 6

An Introduction to Creating Games in Scratch  for ICSE Class 6 Hello friends, I hope you have already read the previous post on An Int...