Interfacing Bluetooth module with 8051 Microcontroller ( HC05 )

HC05-bluetooth-BT-module-pin-diagram

Bluetooth technology creates a big evolution in way the devices communicate with each other. So it is important to learn interfacing Bluetooth with our MCU’s to build extended system also it offers facility to wireless control. This tutorial focuses in interfacing Bluetooth module with 8051 microcontroller. We are going to use a module known as HC05 in our tutorial.

At the end of this tutorial you will be able to:

Program and interface Bluetooth module with 8051
– Send and receive data’s using 8051 via Bluetooth.
– Build and control any system using Bluetooth devices such as mobile phone ,PC, tablets etc.

INTERFACING BLUETOOTH MODULE WITH 8051 (HC05):

A Bluetooth module widely used with Microcontroller to enable Bluetooth communication. This module cam be interfaced using the UART in 8051 microcontroller where the data are transmitted in the form of packets. The pins TX and RX pin of the HC 05  form the path for data transmission and reception. These TX pin  of  HC05 must be connected to the RX pin of 8051 and vice versa. Whereas the key pin of the module is used to set the password for pairing the module with our devices.

 APPLICATION:

Bluetooth-terminal-application
Snapshot of BT terminal Application

Our devices such as mobile and PC’s need special applications known as “Bluetooth Terminal” to communicate with our microcontrollers via Bluetooth. Not to worry, there are plenty of apps you can find in the internet. These apps are available in plenty irrespective of the you device OS Android, Windows , Mac whatever it may be. Just run a search such as Bluetooth Terminal for “OS name” and search engines will take you to the destination.

These applications are developed in such a way to send characters through your device BT which was received by the BT module connected with our controller. Even some apps offers some interactive GUI buttons which transmits specific characters with the press of each buttons. Later the received character can be processed in our code and force the controller to perform tasks based on the received character. We can use the Bluetooth communication in two ways, either we can use it to receive data from the Controller or control the system using our device Bluetooth.

SCHEMATIC DESIGN:

interface-bluetooth-module-with-8051-microcontroller-hc05

 

STEPS TO PROGRAM:

  • This uses Serial Communication or UART protocol in the 8051 , so those who are not familiar with this kindly go through this article on “UART tutorial in 8051” and “UART interrupt” before getting started with BT interface.
  • Initialize the Serial communication in 8051 using Timer and serial registers.
  • Generate the required baud rate for the communication to take place. The default baud rate of the HC05 is 9600.
  • Initialize serial interrupts in case you need to control the tasks performed by your microcontroller or receive data when requested.

SAMPLE CODE:

The below code was built using Keil uVision 4.

FOR RECEIVING DATA FROM CONTROLLER VIA BT:

Here a specific data can be transmitted whenever a interrupt request occurred from our device via bluetooth. In the below code a processed data was transmitted to our device from the controller when a serial interrupt occurs.

#include<regx51.h>
#include<stdio.h>
#include<string.h> 
int a,b,ans;
char rec[4];
void main()   
{    
a=80;
b=40;
ans=a+b;
sprintf(rec, "%d", ans);
TMOD=0x20;                                //Choosing Timer mode    
TH1=0xFD;                                   //Selecting Baud Rate    
SCON=0x50;                               //Serial mode selection    
TR1=1;    
IE=0x90;                                      //Enabling Serial Interrupt    
while(1);    
}
void ser_intr(void)interrupt 4        //Subroutine for Interrupt  
{
IE=0x00;
short int i;
for(i=0;i<=2;i++)                      //Transmitting data
{
SBUF=rec[i];
while(TI==0);
TI=0;
}
IE=0x90;
}

CODE FOR CONTROLLING CONTROLLER TASKS VIA BT:

Here a specific task is performed on interrupt occurence in the controller via BT. Consider a Motor was connected in P2.0 & P2.1 of the controller and we are about to control its direction of rotation via BT from our device. Sending character “F” will make the motor to rotate clockwise whereas “R” will make the motor to rotate in anti clockwise direction.

#include<regx51.h> 
sbit mot1=P2^0;
sbit mot2=P2^1;
void main()
{
//////////
Initialize serial communication and activate interrupts.
//////////
}
void ser_intr(void)interrupt 4        //Subroutine for Interrupt  
{
char c;
IE=0x00;
while(RI==0);
c=SBUF;
if(c=='F');                               //Controlling motor
{
mot1=1;
mot2=0;
}
else if(c=='R')
{
mot1=0;
mot2=1;
}
IE=0x90;
}

Hope you like this tutorial, leave your queries and feedback in the below comment box.


37 Comments

  1. Dhafir Alani

    hi sir …would you mind to clear your code because i uesd that code is not working ….forward motor and backword…thanks

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Hi Dhafir,
      The above code is just a scheme, you need to build and tailor based on your requirement. Post your code and i may be able to help you

      Reply
  2. Vineeth

    sprintf(data, “%d”, ans);
    I am getting an error here. How to resolve it.

    Reply
  3. bhuvana

    im doing a project called bluetooth controlled notice board with at89s52 is that possible to use the above code to run the process

    Reply
    1. neethu

      do u have the source code for the project “bluetooth controlled notice board with at89s52”

      Reply
    2. Sai vineetha

      Do u have the code for Bluetooth controlled notice board

      Reply
      1. Frank DonaldFrank Donald (Post author)

        Vineetha,
        Apologies, we don’t have that might make one soon

        Reply
  4. sachin

    what is a difference between server and client if i use client can i send data

    Reply
  5. Guillermo Hernandez

    Thanks for your information. Now, I would like to connect the module to a PC via interface TTL-USB cable. It is possible? 😀

    Reply
  6. Ula M

    Hello Frank,

    Thanks a lot for your informative tutorial. I am working on a 8051 based project, and having some difficulties. I need to send a string to a microcontroller, but I can only send one char at a time. Do you know how to send a string? I tried to use loops but it did not work. Thanks in advance

    Reply
  7. siva nageswara raosiva nageswara rao

    i need a programme for blutooth tx and rx. in 8051 mc

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Vsivangulu,
      The code was added in the article, kindly refer it.

      Reply
  8. Murugan

    will u pls say this instruction brefily “void ser_intr(void)interrupt 4”- i know its is interrupt command and y the number 4 mention in this line for, y its is used

    Reply
    1. Frank DonaldFrank Donald (Post author)

      Murugan,
      Actually each interrupt is identified by a specific “interrupt number” in order to convey the controller that in case a type of interrupt is encountered it should jump to the respective sub routine. For serial interrupt the interrupt number is 4, hence i have used “4” there. Kindly go through this Serial interrupts in 8051 Microcontroller article to get a clear idea about it.

      Reply
  9. Shiv

    Hey i am trying to interface HC-05 with my AT89S52 for college project i have done same connections and also burnt program correctly on chip, despite of that there is no communication between my controller and laptop. I am using real term for recieving data .( both software and hc-05 are working fine since i have tested them on arduino) . I am using aseembly to program and i have generated baud rate of 9600. Please help me into this

    Reply
    1. Frank Donald

      Shiv,
      Really i can’t figure out your problem with your comment. I would advice you to test the code by using serial interrupt and performing a led blink when any interruption occurs. Am not familiar with assembly coding, so if you have the code in c, kindly post it along with your next comment.

      Reply
      1. Neethu

        Do u have d code for Bluetooth controlled notice board

        Reply
    2. Shiv

      Well thanx Frank for replying to my query. I dont have C code right now but i dont think there is any problem with my code. I have checked it in Keil one step debugging mode and it runs fine, it sets baud rate to 9600,it transfers requred data to SBUF( i have checked all this in keil debugging mode). IF there r any extra steps or care that i need to take while connecting BT with 8051 do let me know.I have connected BT as per instructed in this site.

      Reply
  10. Anjeesh

    excellent article, love to see some other wireless interfaces with 8051. thanks

    Reply
    1. Frank Donald

      Anjeesh,
      Thank you for your appreciation, will post some articles based on wireless module interfaces soon. Keep visiting

      Reply
      1. Pinky

        Can u send me the code for Bluetooth controlled wireless notice board

        Reply

Leave a Comment

Your email address will not be published. Required fields are marked *