Overview of Bluetooth
The HC-05 is a Bluetooth module used for wireless communication, operating through serial communication (USART). It is a 6-pin module and can function in two modes: data mode and command mode.
- Data mode is used for transferring data between devices, while command mode is used to modify the Bluetooth module’s settings. To use the HC-05 in command mode, AT commands are required.
The module operates on either 5V or 3.3V and includes an onboard 5V to 3.3V regulator. Since the HC-05 uses 3.3V levels for RX/TX and the microcontroller can detect 3.3V, there is no need to shift the transmit level of the HC-05 module. However, it is necessary to shift the transmit voltage from the microcontroller to the RX pin of the HC-05 module.
For detailed information on the HC-05 Bluetooth module and its usage, please refer to the Bluetooth Module HC-05 topic in the Sensors and Modules section.
For insights on USART in PIC18F4550 and how to utilize it, check out the USART in PIC18F4550 topic in the PIC Inside section.
http://electronicwings.com/sensors-modules/bluetooth-module-hc-05-
http://electronicwings.com/pic/pic18f4550-usart
HC-05 Bluetooth Module
Connection Diagram of Bluetooth Module HC-05 to PIC18F4550
HC-05 Bluetooth Module Interfacing with PIC18F4550
Control the LED using HC-05 Bluetooth Module and PIC18F4550
Let’s create a simple application where we can control an LED (turn it ON or OFF) through a smartphone.
This is achieved by interfacing the PIC18F4550 microcontroller with the HC-05 Bluetooth module. Data is transmitted and received serially via USART communication on the PIC18F4550.
Application Description:
- When 1 is sent from the smartphone, the LED will turn ON.
- When 2 is sent, the LED will turn OFF.
- If any other data is received, a message will be sent back to the mobile device, instructing the user to select the correct option.
Programming the HC-05:
- Initialize USART Communication on the PIC18F4550 to communicate with the HC-05 Bluetooth module.
- Receive Data from the HC-05 module.
- Check Received Data:
- If the received data is ‘1’, turn the LED ON.
- If the received data is ‘2’, turn the LED OFF.
- For any other input, send a response to the mobile device prompting the user to select a valid option.
HC-05 Bluetooth Module Code for PIC18F4550
/* Interface HC-05 Bluetooth module with PIC18F4550
* http://www.electronicwings.com
*/
#include <pic18f4550.h>
#include "Configuration_Header_File.h"
#include "USART_Header_File.h"
#define LED LATD0
void main()
{
OSCCON=0x72; /* use internal oscillator frequency
which is set to 8 MHz */
char data_in;
TRISD = 0; /* set PORT as output port */
USART_Init(9600); /* initialize USART operation with 9600 baud rate */
MSdelay(50);
while(1)
{
data_in=USART_ReceiveChar();
if(data_in=='1')
{
LED = 0; /* turn ON LED */
USART_SendString("LED_ON"); /* send LED ON status to terminal */
}
else if(data_in=='2')
{
LED = 1; /* turn OFF LED */
USART_SendString("LED_OFF"); /* send LED OFF status to terminal */
}
else
{
USART_SendString(" select 1 or 2"); /* send msg to select proper option */
}
MSdelay(100);
}
}