Bluetooth module HC 05 interfacing with pic microcontroller




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.

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-System

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
READ  Line following Robotic Vehicle using Microcontroller

 Connection Diagram: 

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…).

READ  Interfacing GPS Receiver with 8051 Microcontroller -AT89C52

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

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 Bluetooth Controller..

 

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.

Creating New Project

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.

confiiguration

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).

READ  Object Detecting Android Mobile Phone Controlled Bluetooth Robot Using PIC Microcontroller 16F877A

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.


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.




Current Project / Post can also be found using:

  • interfacing hc -06 with pic
  • pic microcontroller and jdy-09 bluetooth tutorial

Leave a Comment

*
= 3 + 4

Read previous post:
BRAIINS RELEASES OPEN SOURCE BITCOIN MINER

Braiins Systems has taken the initiative of releasing the first bitcoin mining pool, which is compatible with Antminer and Dragonmint mining rigs and works with...

Close
Scroll to top