Bluetooth module HC 05 interfacing with pic microcontroller

Bluetooth module interfacing with pic microcontrollers, in this article I explained how to interface Bluetooth module with microcontrollers,  What is Bluetooth ? Types of Bluetooth? How Bluetooth communicate with other devices? and How to select most suitable Bluetooth module for your project in which you want to use Bluetooth for wireless communication. Many of you are already familiar with Bluetooth and its working. Because you use Bluetooth daily for  sending and receiving data from one device to another in your mobile phones or computers. But Bluetooth have many other application in wireless audio and video controllers.

What is Bluetooth ?

Bluetooth is a type of wireless communication protocol used to send and receive date between two devices. It is free to use wireless communication protocol. Although its range it lower than other wireless communication protocols like WiFi and ZigBee. But it is still suitable for many low range applications.Bluetooth wireless protocol lies in the same range of frequency of WiFi and ZigBee. It operates on 2.41 GHz frequency.

Applications of Bluetooth :

There are many applications of Bluetooth modules but major applications of Bluetooth is given below :

  • In mobiles , computers , laptops and all other smart computers.
  • wireless Audio and video controllers
  • wireless mouse and keyboards
  • Wireless head phones and Microphones
  • there are man other applications of Bluetooth, Just Google to know more about applications of Bluetooth.

Bluetooth module is work as master or slave. There are many Bluetooth modules available in market which are either master/slave or both. Master Bluetooth module can send or receive data from other Bluetooth modules. But slave Bluetooth can only listen to master Bluetooth module. It depends on your application which Bluetooth module you need for your project.

How to select Bluetooth module ?

Bluetooth module selection depends on following factors :

  1. Maximum power output of Bluetooth module

There are three classes of Bluetooth modules exist. Power output range of  all three class are given below :

  • class 1 : 100 mW
  • class 2 : 2.5 mW
  • class 3 : 1 mW

  2. Distance between your two Bluetooth devices receiver and transmitter :

According to range of Bluetooth devices, there are also three classes of Bluetooth modules :

  • class 1 : 100 meter
  • class 2 : 10 meter
  • class 3 : 10 centimeter

  3. Mode of communication you want to use

Bluetooth modules need communication protocols to interface with other devices. For example in this article our main focus is Bluetooth module interfacing with microcontroller, microcontroller can communicate with Bluetooth device through following wired communications to send or receive data from other Bluetooth device :

  • UART
  • SPI
  • USB

It depends on you which wired  communication from above three you want to use for interfacing your microcontroller with Bluetooth module. There are many Bluetooth modules available in market which support above three communication.  But the name of  some famous serial  Bluetooth modules are given below:

  • HC-04
  • HC-05
  • RN-41
  • RN-42
  • AUBTM
  • BLUESMIRF

All above Bluetooth modules have different range, power output and quality. Before selecting any above Bluetooth module you should make sure either you have a need of master or slave Bluetooth module.You can choose any above Bluetooth module according to your requirements. I recommend you to choose Bluetooth module which have UART communication and it is easy to write program using UART. While selecting any Bluetooth module you should check that it should have built in Bluetooth host controller. Otherwise you have to write your own software for this purpose and it need a lot of time and expertise in software programming.

In this article I have chosen RN-42 Bluetooth module to teach you how to interface it with pic microcontroller. RN-42 is a Bluetooth module product of microchip.

RN-42 Bluetooth module interfacing with pic microcontroller

RN-42 is class 2 Bluetooth module. It have  UART and USB communication interface. It can both send and receive data i.e. it can work both as Master and slave. It can also be use for audio application.

To interface RN-42 module with PIC microcontroller or any microcontroller, you need of voltage level shifting circuit with RN-42. But Microchip also provide it solution by providing complete compatible board to interface it with microcontroller. RN-42 EK board can interface with PIC microcontroller directly by  connecting UART_RX pin of RN-42 Bluetooth module to Tx pin of pic microcontroller and UART_TX pin of RN-42 Bluetooth module to Rx pin of pic microcontroller. RN-42-EK board can also be connected with computer through usb cable.

To download data sheet and to buy RN-42 EK board click on the link below :

RN-42 Bluetooth Evaluation Kit

Block diagram below shows interfacing diagram of pic microcontroller and RN-42 EK board :

block diagram of Bluetooth interfacing with pic microcontroller
block diagram of Bluetooth interfacing with pic microcontroller

How to send/receive commands or data from microcontroller to/from RN-42 EK kit

RN-42 have two modes commands mode and data mode. command mode is used to set configuration of Bluetooth module like its mode either master or slave, baud rate, serial port flow control. Bluetooth module should be configured before using data mode. Otherwise it will use default values of configuration bits and commands. Command mode is used to set device name , pin code and baud rate. One thing you should keep in mind there is a specific time for command mode, you should use command with in this time otherwise. Bluetooth module will reach into data mode after this time.

HC05 Bluetooth module with pic microcontroller

Bluetooth module HC 05 interfacing with pic microcontroller: Hi Everyone, In this article I will explain you how to interface Bluetooth module HC 05 with pic16f877a microcontroller or any other microcontroller like 8051, AVR and Arduino. It is very easy to communicate with android mobile through Hc 05 Bluetooth module. It has many applications and this Bluetooth module Hc 05 is very popular in engineering students. Many students use this module in their microcontroller based projects. Examples of most popular Bluetooth based project are robot control through android mobile and home automation system. You can also use it to measure solar energy on your sell phone through android application.

Introduction to Bluetooth module HC 05

So lets start with basic introduction of Bluetooth module Hc 05. It has single chip on board integrated circuit based on CMOS technology.It has upto +4dBm transmission power.It has wide operating voltages between 3.6 to 6 volt. Default baud rate of this device is 9600. But you can change it according to your requirement by using AT commands. I will explain later how to use AT commands of this bluetooth module. HC 05 work on serial communication. It communicates with microcontroller through UART communication. So you should know how to use serial communication of pic microcontrollerIf you know serial communication programming of pic16f877a controller you can easily interface this module and write a program in C.Bluetooth module HC 05

Bluetooth module HC 05 pin out

pin out of Hc 05 module is given below. As you can see it has six pins. All these pins has specific functions. You can check data sheet of this module to know more about functionalities of these pins. you will need to use only  Vcc, ground, TXD and RXD pin of this Bluetooth module HC 05 to interface it with pic microcontroller. Explanation of all these pins are given below:

  • KEY pin:  This pin is used to enter AT commands. If this pin is connected with ground or no connection, you can simply use it pair it with devices. if it is connected with high logic, it will use to enter in AT commands.This pin is used to remove information stored in hc 05 bluetooth module about paired devices.
  • State pin: It is of no use. It just shows the status of device.
  • Vcc pin: It is a power supply pin. You should connect 5 volt with this pin.
  • Ground pin: Connect ground pin of power supply to this pin.
  • TXD pin: This is data transmission pin. You should connect RXD pin of microcontroller with this pin.
  • RXD pin: This is data receiver pin from microcontroller. you should connect TXD pin of microcontroller with this pin.

Bluetooth module HC 05 interfacing with pic microcontroller

circuit diagram of Bluetooth module interfacing with pic microcontroller is given below:

Bluetooth module HC 05 interfacing with pic16f877a microcontroller

In above we have simply connected RXD pin of microcontroller with TXD pin of Hc 05 BT module and TXD pin of microcontroller with RXD pin of HC 05 BT module. When you turn on BT module by providing power supply to it, it will become searchable by other BT devices. For example with your android mobile you can search for available devices.  It will be available with the name of HC 05. Now you can pair with it. When you try to pair with Bluetooth module Hc 05 first time, you will have to enter a password. Default password for this device is 1234. You can also change the password by using AT commands of this module. Once you are done with pairing of your mobile with hc 05 BT module . Now you can send and receive data from android mobile to BT module through microcontroller and vice versa. you can install any Bluetooth terminal app on your android mobile to send and receive data. you can try this android BT device.

Code for controlling LEDs

char IncData;void main() {
TRISD=0X00;
PORTD=0X00;
TRISC.F7 = 1;
UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100); // Wait for UART module to stabilize
UART1_Write_text(" Bluetooth starting");

while(1)
{ while(!UART1_Data_Ready());
if (UART1_Data_Ready())
{
IncData = UART1_Read();
if ( IncData == 'A')
PORTD.B0 = 1;
if ( IncData == 'a' )
PORTD.B0 = 0;
if ( IncData == 'B' )
PORTD.B1 = 1;
if ( IncData == 'b' )
PORTD.B1 = 0;
if ( IncData == 'C' )
PORTD.B2 = 1;
if ( IncData == 'c' )
PORTD.B2 = 0;
if ( IncData == 'D' )
PORTD.B3 = 1;
if ( IncData == 'd' )
PORTD.B3 = 0;

}

}

}

LED control with Bluetooth module Hc 05 sing pic microcontroller

Below is a video in which I explain how to interface it with pic microcontroller and how to use to control LED’s connected with microcontroller.

Solar energy measurement on android app using pic microcontroller

Below is video lecture on how to measure solar energy on android app using microcontroller and HC 05 Bluetooth module. In this project, I have measured solar energy with pic microcontroller and send it to android app using BT module.

Code of Bluetooth module interfacing with pic microcontroller

Below is code to send different strings to BT module every one second.

int voltage, current, power;
void main() {
 TRISD=0X00;
 PORTD=0X00;
 UART1_Init(9600); // Initialize UART module at 9600 bps
 Delay_ms(100); // Wait for UART module to stabilize
 while(1)

{ 
 UART1_Write_text("voltage = 5.1 Volt\r\n");
 UART1_Write_text("current = 0.5 Ampere\r\n");
 UART1_Write_text("Power = 2.5 watt\r\n");
 delay_ms(1000);

}

}

So this is how we can interface HC-05 Bluetooth module with pic microcontroller. you can easily use it in your embedded systems projects. If you have any issue let me know with your comments. thanks.

7 thoughts on “Bluetooth module HC 05 interfacing with pic microcontroller”

  1. Good day sir!
    I am learning how to interface PIC16F877a to Android via Bluetooth and your article has exactly what I need! But I am running into some issues;
    I am using EasyPIC v7 development board:
    -PIC16F877a is connected in its place
    -An external 8MHz Crystal Oscillator is used
    -HC-05 is connected correctly (GND to PortC GND, VCC to PortC VCC, TX to RC7 and RX to RC6)
    So my circuit is supposedly correct.

    I am using the same code provided in this article with a small addition added to test if the module receives data as well.

    char uart_rd;
    int voltage, current, power;
    void main() {
    TRISB=0xFF;
    UART1_Init(9600); // Initialize UART module at 9600 bps
    Delay_ms(100); // Wait for UART module to stabilize
    while(1)

    {
    UART1_Write_text(“voltage = 5.1 Volt\r\n” );
    UART1_Write_text(“current = 0.5 Ampere\r\n”);
    UART1_Write_text(“Power = 2.5 watt\r\n”);
    delay_ms(1000);
    uart_rd=UART1_Read();
    if (uart_rd == ‘a’)
    {TRISB = 0x00; PORTB = 0xFF;}
    }

    }

    My problem is that no data is shown on any Bluetooth terminal. And according to my code, when I send the letter a from any Bluetooth terminal, PortB becomes output and all pins are 1 (will light red), except nothing happens when I send the letter a.

    Could you please tell me what might be the issue? Are there any mistakes with the code or how I connected the circuit? Or could the PIC be damaged? Or maybe the HC-05’s baud rate is configured with a baud rate different than 9600?
    Also, do you know how can I configure the HC-05 with AT commands using the PIC instead of Arduino?
    Please help! Thanks 🙂

    Reply
  2. hello sir i am learning how to interface bluetooth hc05 to pic 18f4520
    sir is it possible to interface bluetooth to other than serial communication port (like port A)

    Reply
  3. Hlw sir, i am doing a lab on ho to control LED on and of via portd, using HC 05 and pic16f887, hc05 pins are correctly connected, ie, vcc to 5v, gnd to ground, txd to rx of pic and rxd to tx of the pic.
    I am using “bluetooth terminal app” but it seem as if there is no communication between hc 05 and pic microcontroller as i dnt get any outputs, could u please help me if there is any problem that may have occured

    Reply

Leave a Comment