Quantcast
Viewing all articles
Browse latest Browse all 215

Interfacing Bluetooth module HC05 with 8051 Microcontroller

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 HC05 with 8051 microcontroller.

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.

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:

Image may be NSFW.
Clik here to view.
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.

DESIGN:


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.
  1. #include<regx51.h> 
  2. int a,b,ans;
  3. char data[4];
  4. void main()   
  5. {    
  6. a=80;
  7. b=40;
  8. ans=a+b;
  9. sprintf(data, "%d", ans);
  10. TMOD=0x20;                                //Choosing Timer mode    
  11. TH1=0xFD;                                   //Selecting Baud Rate    
  12. SCON=0x50;                               //Serial mode selection    
  13. TR1=1;    
  14. IE=0x90;                                      //Enabling Serial Interrupt    
  15. while(1);    
  16. }
  17. void ser_intr(void)interrupt 4        //Subroutine for Interrupt  
  18. {
  19. IE=0x00;
  20. short int i;
  21. for(i=0;i<=2;i++)                      //Transmitting data
  22. {
  23. SBUF=data[i];
  24. while(TI==0);
  25. TI=0;
  26. }
  27. IE=0x90;
  28. }


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.

  1. #include<regx51.h> 
  2. sbit mot1=P2^0;
  3. sbit mot2=P2^1;
  4. void main()
  5. {
  6. //////////
  7. Initialize serial communication and activate interrupts.
  8. //////////
  9. }
  10. void ser_intr(void)interrupt 4        //Subroutine for Interrupt  
  11. {
  12. char c;
  13. IE=0x00;
  14. while(RI==0);
  15. c=SBUF;
  16. if(c=='F');                               //Controlling motor
  17. {
  18. mot1=1;
  19. mot2=0;
  20. }
  21. else if(c=='R')
  22. {
  23. mot1=0;
  24. mot2=1;
  25. }
  26. IE=0x90;
  27. }
Hope you like this tutorial, leave your queries and feedback in the below comment box.

Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 215

Trending Articles