Bluetooth module (HC-05/HC-06) detailed explanation

0. Bluetooth overview

Bluetooth (Bluetooth) is a technical standard for wireless communication that allows devices to exchange data and communicate over short distances. It was launched by Ericsson in 1994 to replace the traditional wired connection and enable low-power, low-cost data transmission and communication between devices.
insert image description here

Features of Bluetooth technology

  1. Wireless communication: Bluetooth allows devices to communicate within close range (typically around 10 meters, depending on the device version) without the use of cables or other physical connections.

  2. Low power consumption: Bluetooth technology is designed as a low-power communication method, which makes it widely used on mobile devices, such as smartphones, tablets, Bluetooth headsets, etc.

  3. Multi-device connection: Bluetooth allows a master device (such as a mobile phone) to connect to multiple slave devices (such as Bluetooth headsets, Bluetooth speakers, etc.) at the same time to achieve more flexible data transmission and communication.

  4. Versatility: Bluetooth technology is widely used in many devices and applications, such as wireless headsets, keyboards, mice, Bluetooth connections in cars, smart home devices, and more.

  5. Security: Bluetooth technology is constantly being developed and improved to increase its security to prevent unauthorized access and data leakage.

Bluetooth technology has different features and functions in different versions, from classic Bluetooth (Bluetooth Classic) to low-power Bluetooth (Bluetooth Low Energy, referred to as BLE), each version is aimed at different application scenarios and requirements.

1. Common bluetooth module

  1. HC-05/HC-06:

    • HC-05 and HC-06 are widely used classic Bluetooth modules, which are often used for wireless communication with microcontrollers such as Arduino.
    • They are based on the Bluetooth 2.0 standard and support the serial port communication (UART) protocol, which makes the connection and data exchange with the microcontroller relatively simple.
    • HC-05 can be used as a master device or a slave device, and supports Bluetooth SPP (serial port communication) and AT command mode.
    • HC-06 usually acts as a slave device and only supports Bluetooth SPP mode.
    • They have a basic communication range, usually around 10 meters.
      insert image description here
  2. HM-10/CC2541:

    • The HM-10 and CC2541 are Bluetooth Low Energy (BLE) modules for Internet of Things (IoT) devices and sensor networks.
    • They are based on the Bluetooth 4.0 standard and feature low power consumption and short-range communication.
    • They support BLE communication protocols such as GATT (Generic Attribute Profile) and ATT (Attribute Protocol) for data transfer and connection management.
    • The HM-10 has a larger feature set, including master and slave modes, while the CC2541 acts primarily as a slave.
    • They are commonly used to connect and control sensors, smart home devices, and more.
      insert image description here
  3. RN42/RN52:

    • The RN42 and RN52 are classic Bluetooth modules produced by Microchip (formerly Roving Networks).
    • Based on the Bluetooth 2.1+EDR standard, they provide rich functions and easy-to-use interfaces.
    • These modules support serial port communication (UART) protocol and have a large communication range (usually above 30 meters).
    • They are suitable for various applications such as audio transmission, data communication and Bluetooth pairing.
    • The RN52 also provides additional audio features such as stereo audio transmission and audio configuration options.
      insert image description here
  4. ESP32:

    • ESP32 is a powerful Wi-Fi and Bluetooth combo module developed by Espressif Systems.
    • It integrates dual-core processor, Wi-Fi, Bluetooth, low power consumption technology and rich peripheral interfaces.
    • ESP32 supports Bluetooth Classic and Bluetooth Low Energy (BLE), which has greater flexibility and functionality.
    • It is widely used in Internet of Things (IoT) applications, embedded systems, and smart device development.
      insert image description here

2. HC-05/HC-06

2.1 Concept

HC-05 and HC-06 are two common classic bluetooth modules, they have some differences in design and function. The following is a detailed explanation of them:

  1. HC-05:

    • HC-05 is a powerful classic Bluetooth module produced by Aisin Electronics (EGBT).
    • It is based on the Bluetooth 2.0 standard and supports multiple transmission protocols, including serial communication (UART) and Bluetooth SPP (serial communication).
    • HC-05 can be switched between master mode (Master) or slave mode (Slave), which has high flexibility.
    • It supports the setting of Bluetooth connection password to improve security.
    • HC-05 has 6 pins, including VCC (power supply), GND (ground), TXD (transmit data), RXD (receive data), STATE (status) and EN (enable).
    • The STATE pin can be used to detect connection status and module working mode.
    • HC-05 can be configured and controlled through AT commands, such as changing the Bluetooth name, baud rate, pairing password, etc.
      insert image description here
  2. HC-06:

    • The HC-06 is another common classic Bluetooth module, also produced by Aisin Electronics (EGBT).
    • Compared with HC-05, HC-06 is relatively simplified and is mainly used in slave device mode (Slave).
    • It only supports the Bluetooth SPP (Serial Port Communication) protocol and cannot be switched to master mode.
    • HC-06 has 4 pins, including VCC (power supply), GND (ground), TXD (transmit data) and RXD (receive data).
    • It does not have STATE and EN pins, so it cannot provide information related to connection status and operating mode.
    • The HC-06 can pair and communicate directly with other host devices such as smartphones or computers.
    • Similar to HC-05, HC-06 can also be configured through AT commands, such as changing the Bluetooth name and pairing password.
      insert image description here

2.2 Differences

  1. Operating mode:

    • HC-05: The HC-05 module can be switched between master mode (Master) or slave mode (Slave). That means it can act as a Bluetooth master to establish connections with other slaves, and it can also act as a slave to accept connection requests from a master.
    • HC-06: The HC-06 module is mainly used as a slave device mode (Slave). It cannot switch to the master device mode, it can only accept connection requests from the master device.
  2. Supported protocols:

    • HC-05: The HC-05 module supports a variety of transmission protocols, including serial communication (UART) and Bluetooth SPP (serial communication) protocols. It can perform data transmission and communication with other devices through these protocols.
    • HC-06: HC-06 module only supports Bluetooth SPP (serial port communication) protocol. It does not have the support of UART transmission protocol, so when using HC-06 to communicate with other devices, it needs to transmit data through the serial port.
  3. Pins and functions:

    • HC-05: The HC-05 module has 6 pins, including VCC (power supply), GND (ground), TXD (transmit data), RXD (receive data), STATE (status) and EN (enable). The STATE pin can be used to detect connection status and module working mode.
    • HC-06: The HC-06 module is relatively simplified, with only 4 pins, including VCC (power supply), GND (ground), TXD (transmit data) and RXD (receive data). It does not have STATE and EN pins, so it cannot provide information related to connection status and operating mode.
  4. Application scenario:

    • HC-05: Since HC-05 supports master mode and multiple transmission protocols, it is more common in applications that need to act as a master device to connect other slave devices. It is suitable for projects that require flexible control connections.
    • HC-06: HC-06 is mainly used as a slave device for applications that need to accept connections from a master device. It is suitable for simple Bluetooth serial communication scenarios.

Choose to use HC-05 or HC-06 according to specific application requirements. HC-05 may be a better choice if you need master device mode, support for multiple transport protocols, and flexible connection with other devices. If you only need simple slave device mode and Bluetooth serial communication function, HC-06 can meet the demand.

3. STM32 uses HC-05 communication

3.1 Method

  1. Connect hardware:

    • Connect the VCC pin of the HC-05 module to the 3.3V power supply pin of the STM32, and connect the GND pin to the ground pin of the STM32.
    • Connect the TXD pin of the HC-05 module to a UART receive pin of STM32 (eg USART1_RX), and connect the RXD pin to a UART transmit pin of STM32 (eg USART1_TX).
    • If you need to configure the module or enter the AT command mode, you can connect the EN pin of the HC-05 module to a GPIO pin of the STM32, and control the state of the pin in the code.
  2. Configure the serial port:

    • In the STM32 code, the corresponding UART serial port needs to be configured to communicate with the HC-05 module. You can configure using the STM32's HAL library or directly using register-level programming.
    • Set the baud rate, data bit, stop bit and parity bit of the serial port to match the communication parameters of the HC-05 module.
    • Enable the receiving and sending interrupts of the serial port to send and receive data.
  3. Communication with HC-05:

    • Use UART send function to send data to HC-05 module. For example, when using the HAL library, HAL_UART_Transmit()functions can be used.
    • Use the UART receive function to receive data from the HC-05 module. For example, when using the HAL library, HAL_UART_Receive()functions can be used.
    • According to the communication protocol and AT instruction set of the HC-05 module, write corresponding codes to control the behavior of the HC-05 module, such as configuring module parameters, establishing Bluetooth connections, etc.
  4. Data processing:

    • In the STM32 code, process the data received from the HC-05 module according to your application requirements. You can send data to other devices, process or respond accordingly, etc.

3.2 Sample code

#include "stm32f4xx.h"
#include "stm32f4xx_hal.h"

// 定义串口句柄和接收缓冲区
UART_HandleTypeDef huart1;
char rxBuffer[50];
int rxIndex = 0;

// 函数原型
void SystemClock_Config(void);
void GPIO_Init(void);
void UART_Init(void);
void UART_Transmit(char *data);
void UART_Receive(void);

int main(void)
{
    
    
    HAL_Init();
    SystemClock_Config();
    GPIO_Init();
    UART_Init();

    while (1)
    {
    
    
        // 接收数据
        UART_Receive();

        // 如果收到换行符'\n',表示接收到完整的一行数据
        if (rxBuffer[rxIndex - 1] == '\n')
        {
    
    
            // 处理接收到的数据
            // 在这里可以添加您的处理代码,例如解析数据、响应等

            // 发送数据回复
            UART_Transmit("Received: ");
            UART_Transmit(rxBuffer);

            // 清空接收缓冲区和索引
            memset(rxBuffer, 0, sizeof(rxBuffer));
            rxIndex = 0;
        }
    }
}

void SystemClock_Config(void)
{
    
    
    RCC_OscInitTypeDef RCC_OscInitStruct;
    RCC_ClkInitTypeDef RCC_ClkInitStruct;
    __HAL_RCC_PWR_CLK_ENABLE();
    __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
    RCC_OscInitStruct.HSIState = RCC_HSI_ON;
    RCC_OscInitStruct.HSICalibrationValue = 16;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
    RCC_OscInitStruct.PLL.PLLM = 8;
    RCC_OscInitStruct.PLL.PLLN = 168;
    RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
    RCC_OscInitStruct.PLL.PLLQ = 7;
    HAL_RCC_OscConfig(&RCC_OscInitStruct);

    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 |
                                  RCC_CLOCKTYPE_PCLK2;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
    HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
}

void GPIO_Init(void)
{
    
    
    __HAL_RCC_GPIOA_CLK_ENABLE();

    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}

void UART_Init(void)
{
    
    
    __HAL_RCC_USART1_CLK_ENABLE();

    huart1.Instance = USART1;
    huart1.Init.BaudRate = 9600;
    huart1.Init.WordLength = UART_WORDLENGTH_8B;
    huart1.Init.StopBits = UART_STOPBITS_1;
    huart1.Init.Parity = UART_PARITY_NONE;
    huart1.Init.Mode = UART_MODE_TX_RX;
    huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    huart1.Init.OverSampling = UART_OVERSAMPLING_16;
    HAL_UART_Init(&huart1);
}

void UART_Transmit(char *data)
{
    
    
    HAL_UART_Transmit(&huart1, (uint8_t *)data, strlen(data), HAL_MAX_DELAY);
}

void UART_Receive(void)
{
    
    
    HAL_UART_Receive_IT(&huart1, (uint8_t *)&rxBuffer[rxIndex], 1);
    rxIndex++;
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    
    
    if (huart->Instance == USART1)
    {
    
    
        HAL_UART_Receive_IT(&huart1, (uint8_t *)&rxBuffer[rxIndex], 1);
        rxIndex++;
    }
}

Connect the HC-05 module to the USART1 serial port of STM32 (PA9 is USART1_TX, PA10 is USART1_RX). It uses the HAL library to initialize the serial port, send and receive data.

In main()the function, it receives the data from the HC-05 module by polling, and processes and replies to the data after receiving the newline character '\n'. You can modify the code for processing and replying as needed.

UART_Init()The function is used to configure the parameters of the USART1 serial port, including baud rate, data bits, stop bits, parity bits, etc.

UART_Transmit()The function is used to send data to HC-05 module.

UART_Receive()The function uses interrupt mode to receive data from HC-05 module, and store the received data in rxBuffer.

HAL_UART_RxCpltCallback()The function is the callback function of the UART receiving completion interrupt. When a byte of data is received each time, it will trigger the next reception.

Guess you like

Origin blog.csdn.net/m0_56694518/article/details/131483256