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
Module | Type | Interface | Key Features |
---|---|---|---|
HC-05/HC-06 | Classic Bluetooth (2.0) | UART | Easy AT commands, widely used |
BLE (CC2541, nRF51822) | BLE 4.0/5.0 | UART/SPI | Low-power, IoT-friendly |
ESP32 (Built-in BLE) | Dual-mode (BT + BLE) | UART/SPI | Wi-Fi + BLE combo |
RN4870/RN4871 | BLE | UART | Pre-programmed GATT profiles |
2. Hardware Connections
Wiring HC-05 to STM32 (UART)
HC-05 Pin | STM32 Pin | Notes |
---|---|---|
VCC | 3.3V/5V | Check module voltage |
GND | GND | Common ground |
TX | USART_RX (e.g., PA3) | MCU receives data |
RX | USART_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)
Enable UART in STM32CubeMX
Select USART2 (or another UART port).
Configure Baud Rate (9600, 38400, or 115200) (match module settings).
Basic UART Transmit/Receive Code
#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:
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):
// 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
Android/iOS Apps:
Serial Bluetooth Terminal (for UART communication).
nRF Connect (for BLE debugging).
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
Problem | Solution |
---|---|
No response from module | Check power, baud rate, and wiring. |
Garbage data received | Ensure baud rates match (9600/115200). |
BLE not discoverable | Verify advertising mode is enabled. |
Connection drops | Check antenna placement (for BLE). |
7. Project Ideas
Wireless Sensor Node
STM32 + BLE sends temperature data to a phone.
BLE-Controlled Robot
Use an app to send movement commands.
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.
评论
发表评论