Bluetooth Module HC-05 With PIC Microcontroller Tutorial

Previous Tutorial Previous Tutorial Tutorial 19 Next Tutorial Next Tutorial
Bluetooth Module HC-05 With PIC Microcontroller Interfacing
Introductory Level ★☆☆☆☆

 

In this tutorial, we’ll control the PIC microcontroller using a smartphone and a PC via Bluetooth. So as not to overload this tutorial, we’ll postpone the communication of 2 PIC microcontrollers via Bluetooth in which the 1st one is a master and the 2nd one is a slave to be in a separate future tutorial. So, let’s get started to see how to control PIC microcontrollers wirelessly with a Bluetooth module.

[toc]


   Required Components   

Qty. Component Name Buy On Amazon.com
1 PIC16F877A Add
1 BreadBoard Add
8 LED Add    Add
1 Resistors Kit Add    Add
1 Capacitors Kit Add    Add
1 Jumper Wires Pack Add    Add
1 HC-05 Bluetooth Module Add
1 LM7805 Voltage Regulator (5v) Add
1 Crystal Oscillator Add
1 PICkit2 or 3 Programmer Add
2 9v Battery or DC Power Supply Add    Add    Add

The Prototyping Board Setup

Prototyping Board - Embedded Systems Tutorials With PIC MCUs


   Introducing The HC-05 Bluetooth Module   

 

The Bluetooth is a wireless technology standard for exchanging data over short distances (using short-wavelength UHF radio waves in the ISM band from 2.4 to 2.485 GHz) from fixed and mobile devices, and building personal area networks (PANs). The communication range is approximately 9 Meters (30 feet). We’ll be using the HC-05 Bluetooth module which communicates with microcontrollers over the serial UART bus. Assuming that you’ve got a basic understanding of UARTs or have already completed the UART Tutorial which was the previous one (#18).

 Device Description 

Bluetooth Module HC-05 Sub

This module is based on the Cambridge Silicon Radio BC417 2.4 GHz BlueTooth Radio chip. This is a complex chip in fact which uses an external 8 Mbit flash memory. You can find more information in the associated datasheet. However, it’s fairly easy to set up and interface to any microcomputer system over the serial UART. HC-05 can be set to be either a Master or a Slave. These small modules run on 3.3V power with 3.3V serial signal levels, They have no pins and is usually soldered to a larger breakout board.

Breakout Boards make these modules easy to use. HC-05 sub-modules are soldered on breakout boards to provide easier connectivity for users. Most of these boards support operation at 5V power and interface to 5V microcontroller signal levels with some techniques of level shifting. A typical “breakout” board is shown below.

Bluetooth HC-05 Annotated

 HC-05 Pin Out 

Bluetooth Module HC-05

 

Pin Name Functionality
1 Enable/Key This pin is used to switch between the Data Mode (set low) and AT Command Mode (set high) Before powering-up the module. By default, it’s in Data mode.
2 Vcc Powers-up the module. Is connected to +5V Supply voltage
3 Ground Ground pin of the module. Is connected to your system’s ground.
4 TX Transmits Serial Data. Everything received via Bluetooth will be given out by this pin as serial data to your microcontroller UART receiver.
5 RX Receive Serial Data. Every serial data given to this pin will be sent via Bluetooth.
6 State The state pin is connected to onboard LED, it can be used as a feedback to check if the Bluetooth is working properly.

The Purpose Of The On-Board LED & Button is indicated as follows:

1- The On-Board LED

The way in which this LED is blinking indicates the status of Module as shown below

  • Blink once in 2 sec: Module has entered Command Mode
  • Repeated Blinking: Waiting for connection in Data Mode
  • Blink twice in 1 sec: Connection successful in Data Mode

2- The On-Board Button

It’s used to control the Enable/Key pin to switch between the Data and Command Mode.

 Technical Features 

Here are some of the technical features for the HC-05 Bluetooth modules:

  • Operating Voltage: 3.3V to 6V ( Typically +5V )
  • Operating Current: 30mA
  • Range: ~ 9m ( 30ft )
  • Works with Serial communication (USART) and TTL compatible
  • Follows IEEE 802.15.1 standardized protocol
  • Uses Frequency-Hopping Spread Spectrum ( FHSS )
  • Can operate in Master, Slave or Master/Slave mode
  • Can be easily interfaced with PCs or Smartphones with a Bluetooth capability
  • Supported baud rate: 9600, 19200, 38400, 57600, 115200, 230400, 460800

 Default Settings 

The typical default factory settings for a new Bluetooth HC-05 module are listed down below.

  • Default Bluetooth Name: “HC-05”
  • Default Password: 1234 or 0000
  • Default Communication: Slave Device
  • Default Mode: Data Mode
  • Default Data Mode Baud Rate: 9600, 8, N, 1
  • Default Command Mode Baud Rate: 38400, 8, N, 1
  • Default firmware: LINVOR

 Connection Diagram 

 

HC-05 Bluetooth Module Connection Diagram

The HC-05 sub-module works at a 3.3v logic level. And the breakout board that we’re using has an onboard voltage regulator, that’s why we can supply it with a 3.6 to 6v. However, the UART logic isn’t at the 5v level as you might have thought, it’s 3.3v in fact. Hence, the RX pin of the module is not 5v tolerant and the voltage level has to be at the 3.3v level. A typical microcontroller’s TTL-UART will be at the 5v level.

That’s why we’re making a voltage divider with a couple of resistors as shown in the diagram above. On the other hand, the TX from the module can be considered an acceptable logic as 3.3v is considered a logic High even with a 5v TTL microcontroller, so it’s OK to use the TX line directly as is.

 Applications 

Bluetooth modules are being used in tons of applications each and every single day. Instead of listing down dozens of applications for the Bluetooth modules, I’ll list down some of the major domains under which most of the Bluetooth applications actually fall.

  • Electronic Wearables
  • Home Automation and/or Smart Homes
  • Robotics (Wireless Control / Monitoring)
  • Wireless communication between 2 MCUs
  • Controlling Embedded Systems with a PC or a Smartphone

 Modes Of Operation 

The HC-05 Bluetooth Module has two different modes of operation:

  • Data Mode
  • AT Command Mode

The default mode is the Data Mode with the following default settings for communication.

  • Device Name: HC-05
  • Password: 1234 or 0000
  • Baud Rate: 9600 bps, Data: 8 bits, Stop Bits: 1 bit, Parity: None, Handshake: None

Data Mode

In Data Mode, the HC-05 Bluetooth module can be configured to operate in one of the following modes [ Master – Slave ]. Where it can transmit or receive data to/from another Bluetooth module/device.

AT Command Mode

In many situations, you may need to change some of the default configurations or their values. That’s what we use the Command Mode for. There are a couple of ways to get into Command Mode:

1- Connect the KEY pin high before applying power to the module. This will set the module into command mode at 38400 baud. This is the default baud rate for the command mode and needed if you don’t know the baud rate the module is set to. You can use a serial monitor to get the job done.

2- Apply power to the module then pull the KEY pin high. This will enter command mode at the currently configured baud rate. This is useful if you want to send AT commands from a microcontroller as the KEY pin can be controlled using one of the microcontroller’s pins. BUT you need to know the currently configured Baud Rate for the AT command mode.

Commands are sent to the module in UPPERCASE and are terminated with a CR/LF pair.

The format of commands is:
Always starts with “AT”

Then “+” followed by

Then either:

  • ? (returns the current value of the parameter)
  • = (Set new value for the parameter)

Few Examples:

AT (AT Test command. Should respond with OK)

AT+VERSION? (show the firmware version)

AT+UART=9600,0,0 (Set baud rate to 9600, 1 stop bit, no parity)

 Other Bluetooth Modules 

The most common Bluetooth modules that are being used in electronics/embedded projects are listed down below:

  • HC-05
  • HC-06
  • BLE Link Bee
  • BLE Mini
  • BlueSMiRF
  • JY-MCU
  • RN-42
  • Bluefruit EZ-Link

You can search for their technical specifications online if you’re interested.

 HC-05 VS HC-06 

These two modules are being shipped and sold with confusingly shaped boards. They do look the same, they have got the same range (9m). But there is a major difference between them.

HC-05

In Data Mode, this module can operate as a master or a slave.

HC-06

In Data Mode, this module can only operate in slave mode.

 


   Configuring The Bluetooth Module (Slave)   

 

In this section, I’ll list down the exact steps for configuring our microcontroller to communicate over the Bluetooth as a slave device to receive data from any surrounding Bluetooth device (e.g. smartphone, tablet, pc, other MCU with BT, etc…).

 Step1  Connect the HC-05 BT module as shown in the diagram

HC-05 Bluetooth Module Connection Diagram

 

 Step2  Initialize the UART receiver at the microcontroller end (side)

We’ll use the routine which we’ve developed in the previous tutorial to set up the UART receiver and have it running @ 9600 bps.

 Step3  Whenever data is received at the UART buffer, just save it !

As we’ve also discussed in the previous tutorial, the best way to latch (save) the currently received data in the UART buffer is to use the interrupt signal generated upon each and every successful reception. That’s why you should do it all in the interrupt service routine (ISR) as follows.

You might have noticed that the UART Received Data Buffer is just a single byte. Which may be overwritten before the data is properly utilized in the main routine that may have been busy doing whatever stuff. That’s why it’s a good practice to use a larger buffer (e.g. 5, 10, even 20 bytes). This will be discussed in detail at the concluding section.

 


   Smartphone Bluetooth-Controlled PIC – LAB   

Smartphone To Microcontroller Bluetooth Communication

Lab Name Smartphone Bluetooth Controller
Lab Number 17
Lab Level Beginner
Lab Objectives Learn how to use the HC-05 Bluetooth module with PIC microcontrollers. And develop the necessary firmware in order to receive data from a Smartphone over the Bluetooth to control embedded systems, RC robots, LEDs or whatever.

 

       1. Coding       

 

Open the MPLAB IDE and create a new project and name it “Bluetooth_RX”. If you have some issues doing so, you can always refer to the previous tutorial using the link below.

Create New Project With MPLAB IDE

Set the configuration bits to match the generic setting which we’ve stated earlier. And if you also find troubles creating this file, you can always refer to the previous tutorial using the link below.

Now, open the main.c file and let’s start developing the firmware for our project.

The first task is to initialize the UART module in order to operate in the receiver (slave) mode. Then, we have to save all the coming data to a buffer variable. The process of data reception and storage must be handled in the ISR as we’ve stated earlier.

In the main while loop, we can check if the received byte matches a specific value or not. To take some action (Switch an LED ON/OFF & Toggle Another LED). However, we’ll be doing this job inside the ISR as it’s not an excessive processing task, so it’s OK to have it done inside the ISR whenever a data frame is received.

The Full Code Listing For This Lab

Note: The Data Bytes (49, 50, and 51) are the ASCII equivalents for the numeric characters (1, 2, and 3 respectively).

 

       2. Simulation       

 

In fact, I have no idea about simulation environments that provide virtual Bluetooth modules for testing. However, you can just use the generic Virtual Terminal in Proteus and just type in the data which you wanna send to the microcontroller. It should give you an idea of what will be happening with the real HC-05 module. And the connection diagram is just the same.

Bluetooth module hc-05 interfacing schematic

Note: Don’t forget the 1k, 2k voltage divider resistors in your Bluetooth connection. It won’t damage the module immediately or even after several minutes of operation. But it’s not recommended in the long run.  

Here is an animation for the running simulation tests.

Bluetooth Module HC-05 Simulation

 

       3. Prototyping       

 

Wiring up this schematic on a breadboard should be an easy task. Just upload your firmware hex file to the microcontroller chip. And download a Bluetooth terminal on your smartphone to start testing.

For Android users, I suggest using an application called Bluetooth Controller on the google play store.

 

Open it after installation is complete.

Bluetooth Controller

search for the HC-05 Bluetooth module around.

Enter the password 1234

Now, we’re paired with the Bluetooth module. Code some data to the buttons, name and save them. Start touching the buttons and see what happens. It should be working as in the following video.

Play Video On YouTube

If you’ve any troubles or got stuck at any point, just drop me a comment. I’ll be always here and ready for help. Or even other readers may do a better job in this.

 


   PC Bluetooth-Controlled PIC – LAB   

PC To Microcontroller Bluetooth Communication

Lab Name PC Bluetooth Controller
Lab Number 18
Lab Level Beginner
Lab Objectives Learn how to use the HC-05 Bluetooth module with PIC microcontrollers. And develop the necessary firmware in order to receive data from a PC over the Bluetooth to control embedded systems, RC robots, LEDs or whatever.

 

Technically, there is no difference in the firmware for this lab. Which mean we’ll be using the exact same code used in the previous lab with the exact same breadboard and connections. The only difference is the Bluetooth master device. Instead of a smartphone, we’ll be using a PC with a Bluetooth host device (a Typical laptop will have it built-in by default).

For this purpose, we’ll use a windows application called Tera Term as a Bluetooth terminal. Using this application, we can search for the HC-05, pair with it, and start communication over the terminal window. You can download Tera Term via this Link.

The installation steps are very simple as any generic software. After it’s done, open the Tera Term.exe file and select the serial COM port (Standard Serial Over Bluetooth). If the new connection window didn’t pop-up in your face, then click the File > New Connection.

Bluetooth Serial Communication HC-05 With PC

 

Now, you should search for the HC-05 Bluetooth module and pair with it before you’re able to communicate with the module over Bluetooth.

Well, now we’re good to go. Type in the terminal window whatever value you want (1, 2, or 3) and observe the response at the receiver end. Here is the final results video for this lab in case you’re curious.

Play Video On YouTube
will be added soon…

 


   Concluding Remarks   

 

  1  

UART Receiver Data Buffer

It has been always a recommended practice to have a hardware buffer in your memory dedicated to the received data via serial ports. For the UART firmware that we’ve developed earlier, we’ve just used a single byte to store the received data. However, you can create an array of a custom length to be your data buffer and you’ll also need another variable to keep track of the last position for reading/writing processes.

Implementing a custom buffer for UART should be an easy task for you, but I will consider giving a source code example for what i’ve described above. For now, you can try making your own buffer so as not to miss any received data even if your system has become laggy for whatever reason.

  2  

Pros and Cons of Bluetooth Communication

Advantages Disadvantages
Wireless (No Need For Cables) Low range (~10m)
Easy interfacing and connectivity Low data rate (Speed)
It’s already built-in our phones and laptops Can be attacked/hacked
Low power Some modules are high-power consuming
Standard protocol
inexpensive

  3  

The Most Commonly Used AT Commands

Command Description
AT AT Test command. Should respond with OK
AT+PSWD=xxxx Set the PIN password for your module
AT+NAME=xxxx Set the name of your Bluetooth module
AT+UART=Baud rate, stop bit, parity bit Set the baud rate, stop bit, parity bit [ 9600,0,0 (Set baud rate to 9600, 1 stop bit, no parity) ]
AT+VERSION? Show the firmware version of your module
AT+ROLE=1 Set the module’s mode to be a master
AT+ROLE=0 Set the module’s mode to be a slave
AT+RESET After changing role, a reset is required
AT+ADDR Display the address of the slave module
AT+RMAAD Clear any paired device
AT+ADDR Display the slave module address
AT+ORGL Restore the original settings by the manufacturer

 

 

Previous Tutorial Previous Tutorial Tutorial 19 Next Tutorial Next Tutorial
Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

16 thoughts on “Bluetooth Module HC-05 With PIC Microcontroller Tutorial”

    • Yes, it’s the version. The old one i was using while writing these tutorials had an identifier for the ISR which was “interrupt”. Now it has been changed to “__interrupt”. Try changing this line
      void interrupt ISR ()
      TO BE
      void __interrupt ISR ()
      And compile it again. And please, tell me if it works for you.

      Reply
      • Sorry just saw this reply! I’m still actually working through this project (senior design project). There were some other tasks I had to attend to put I plan on revisiting this issue within the next week. I’ll let ya know if it works for me! I am attempting to use the PIC16FL876A with the RN-41 bluetooth module. You’re tutorials have been awesome!

        Reply
        • Alright, I’m posting this information for those of you who stumble upon the same issues I’ve been having…

          If your project is set to XC8 v2.00 in C99 mode then this is how you write your ISR handle…

          void __interrupt() my_isr(void)
          {
          //do ABC
          }

          Reply
  1. Good morning my name is Donnie
    I have a question is it possible to chamge the id of HC05 bluetooth module then used microcontroller to paire them. I am not using Arduino

    Reply
  2. hello,
    I think the 1k should be connected between 2k and tx pin of microcontroller, not between 2k and reception pin of hc05. Then only it will act as voltage divider.

    Reply
  3. Hello Brother Khaled

    I would like to thank you so much for the useful information about PIC programming. your writing standard is quite good. the details and explanations are excellent and easy to follow.
    Keep up the good work…..

    Just a quick question.
    have you got some more tutorials about PIC programming when interfaced to i.e. {GPS, GSM, Zgbee and LCD}.
    I’d like to let you know that there are MATLAB support packages for MCU programming. these packages are very helpful for embedded designers. one of the packages like Embedded coder generates a state of the art code for high grade processors (like ARM). I would greatly appreciate if you plan to make tutorials using them.

    I look forward to hearing from you

    Best Wishes,
    Adam Tamimi

    Reply
  4. I make the bluetooth control robot car using pic 16f887 ;hc-06 bluetooth module ; L298n motor driver and dc motor.how to write the code using micro C .please reply me sir.

    Reply
    • You need 2 UARTs for this purpose. I’ll explain!
      AT commands are sent via UART from your MCU to the HC-05 BT module. The response messages from the BT module are sent back to your MCU. You need to see these messages, right? Here the 2nd UART comes to play. Using a second UART module, you can now transfer the BT module’s AT messages to your computer via UART and see the messages using any terminal.
      Example
      > Send “AT?” from the MCU to BT module via UART1
      > the BT module will respond to you by “OK” via UART1
      > You now send the response message “OK” via UART2 to your PC in order to see it
      You should also accept AT commands from the PC to your MCU via UART2 then forward it to the BT module via UART1

      The question here, how can I get an extra UART module if I’ve only one?!
      The short answer is “Bit-Banging” or soft UART
      The long answer will be published in a future article ^^
      Hope This Helps!

      Reply

Leave a Comment