The use of Bluetooth module in STM32 embedded development

 Bluetooth integration enables STM32 microcontrollers to communicate wirelessly with smartphones, PCs, and other devices. Here's a comprehensive guide to implementing Bluetooth in STM32 projects.



1. Bluetooth Module Options for STM32

Popular Bluetooth Modules

ModuleTypeInterfaceKey Features
HC-05/HC-06Classic Bluetooth (2.0)UARTEasy AT commands, widely used
BLE (CC2541, nRF51822)BLE 4.0/5.0UART/SPILow-power, IoT-friendly
ESP32 (Built-in BLE)Dual-mode (BT + BLE)UART/SPIWi-Fi + BLE combo
RN4870/RN4871BLEUARTPre-programmed GATT profiles

2. Hardware Connections

Wiring HC-05 to STM32 (UART)

HC-05 PinSTM32 PinNotes
VCC3.3V/5VCheck module voltage
GNDGNDCommon ground
TXUSART_RX (e.g., PA3)MCU receives data
RXUSART_TX (e.g., PA2)MCU sends data (3.3V logic!)
STATE-Optional (indicates connection)

⚠️ Note:

  • Use a logic level converter if the module is 5V and STM32 is 3.3V.

  • Some modules (like RN4871) require a wake-up pin (HCI_WAKE).


3. Software Implementation

A. Using HAL Library (STM32CubeIDE)

  1. Enable UART in STM32CubeMX

    • Select USART2 (or another UART port).

    • Configure Baud Rate (9600, 38400, or 115200) (match module settings).

  2. Basic UART Transmit/Receive Code

c

#include "stm32f1xx_hal.h"

UART_HandleTypeDef huart2;

void SystemClock_Config(void);
static void MX_USART2_UART_Init(void);

int main(void) {
  HAL_Init();
  SystemClock_Config();
  MX_USART2_UART_Init();

  char tx_msg[] = "Hello Bluetooth!\r\n";
  char rx_buffer[50];

  while (1) {
    // Send data to Bluetooth module
    HAL_UART_Transmit(&huart2, (uint8_t*)tx_msg, strlen(tx_msg), HAL_MAX_DELAY);
    
    // Receive data (e.g., from smartphone)
    if (HAL_UART_Receive(&huart2, (uint8_t*)rx_buffer, sizeof(rx_buffer), 100) == HAL_OK) {
      printf("Received: %s\n", rx_buffer);
    }
    HAL_Delay(1000);
  }
}

B. AT Commands (HC-05 Configuration)

To rename the module or change baud rate:

c

HAL_UART_Transmit(&huart2, (uint8_t*)"AT+NAME=STM32_BT\r\n", 17, HAL_MAX_DELAY);
HAL_UART_Transmit(&huart2, (uint8_t*)"AT+UART=115200,0,0\r\n", 19, HAL_MAX_DELAY);

Common AT Commands:

  • AT+NAME? → Check current name.

  • AT+PSWD="1234" → Set pairing PIN.

  • AT+ROLE=1 → Set as master/slave (1=slave).


4. Bluetooth Low Energy (BLE) with STM32

Option 1: External BLE Module (nRF51822)

  • Uses UART or SPI for communication.

  • Example: Adafruit Bluefruit LE UART Friend.

Option 2: STM32WB Series (Built-in BLE)

  • STM32WB55 has Bluetooth 5.0 + Zigbee.

  • STM32CubeWB provides BLE stack examples.

Example BLE Service Creation (STM32WB):

c

// Define a custom BLE service
static const uint8_t MyServiceUUID[] = {0x00, 0x11, 0x22, ...};
static const uint8_t MyCharUUID[] = {0x33, 0x44, 0x55, ...};

aci_gatt_add_service(MyServiceUUID, PRIMARY_SERVICE, 4, &service_handle);
aci_gatt_add_char(service_handle, MyCharUUID, ...);

5. Debugging & Testing

A. Using a Smartphone

  1. Android/iOS Apps:

    • Serial Bluetooth Terminal (for UART communication).

    • nRF Connect (for BLE debugging).

  2. Pairing & Sending Data

    • Connect to HC-05 (default PIN: 1234).

    • Send data via UART terminal.

B. Logic Analyzer

  • Monitor UART (TX/RX) lines to verify communication.


6. Common Issues & Fixes

ProblemSolution
No response from moduleCheck power, baud rate, and wiring.
Garbage data receivedEnsure baud rates match (9600/115200).
BLE not discoverableVerify advertising mode is enabled.
Connection dropsCheck antenna placement (for BLE).

7. Project Ideas

  1. Wireless Sensor Node

    • STM32 + BLE sends temperature data to a phone.

  2. BLE-Controlled Robot

    • Use an app to send movement commands.

  3. STM32-to-STM32 Communication

    • Two HC-05 modules in master-slave mode.


Conclusion

  • HC-05/HC-06 → Simplest way for UART-based Bluetooth.

  • BLE Modules (nRF, RN4871) → Better for low-power IoT.

  • STM32WB → Best for integrated BLE solutions.

评论

此博客中的热门博文

How To Connect Stm32 To PC?

What are the common HDL languages used in FPGA design?

How do you set up ADC (Analog-to-Digital Converter) in STM32?