Master Bluetooth HC-08 on STM32: Complete Step‑by‑Step Guide with AT Commands and Code
This tutorial walks you through downloading the source, understanding HC‑08 specifications, wiring the module to an STM32F103C8T6 board, using AT commands to configure roles, power modes, and connections, and implementing UART‑based send/receive code to enable bidirectional communication with a mobile app.
The article introduces the HC‑08 Bluetooth module, a low‑cost BLE 4.0 device widely used in embedded projects such as smart homes, robots, and wearables. It explains why mastering Bluetooth is essential for embedded engineers and promises a hands‑on, code‑first approach.
Source and Prerequisites
All required source files, a STM32F103C8T6 template project, a serial debugging assistant, a mobile Bluetooth helper app, and the CH340 driver are provided via a free download (access through the author’s WeChat account, note 1225). Beginners are directed to related articles for setting up the STM32 development environment and flashing firmware.
HC‑08 Module Overview
The HC‑08 (based on the TI CC25540 chip) operates at 2.4 GHz, supports BLE 4.0, offers a 1 Mbps data rate, and reaches up to 80 m. A comparison table lists common HC series modules (HC‑02, HC‑04, HC‑05, HC‑06, HC‑08, HC‑09, HC‑42) with their roles, protocols, power, and dimensions; HC‑08 is highlighted as the most suitable for beginners.
Pinout
STATE : status output (low when unconnected, high when paired).
RXD : module’s receive line – connect to the MCU’s TX (use a 220 Ω resistor for 5 V MCUs).
TXD : module’s transmit line – connect to the MCU’s RX.
GND : ground.
VCC : 3.2‑6 V supply (do not feed 5 V directly to surface‑mount versions; use a 3.3 V regulator).
KEY : clears paired slave addresses when pulled high for >200 ms.
The module also features an LED that blinks in slave mode and stays solid when a master is connected.
“The term ‘connection’ refers to a Bluetooth protocol link, not a physical wire.”
AT Commands Overview
AT commands are sent over the UART interface when the module is not connected to a peer. Once a connection is established, any data sent is treated as transparent payload.
Basic Commands
AT: test command, expects OK. AT+VERSION: returns firmware version and date. AT+RESET: reboots the module (responds OK after ~200 ms). AT+DEFAULT: restores factory settings (does not clear stored slave addresses).
Configuration Commands
AT+ROLE=?/ AT+ROLE=M / AT+ROLE=S: query or set master/slave role. AT+MODE=0/1/2: select power mode (0 = full speed, 1 = low‑power, 2 = sleep). AT+LED=0/1: turn the status LED off or on (requires a reboot to take effect). AT+ADDR=? / AT+ADDR=xxxx: read or change the MAC address (changing is discouraged). AT+NAME=? / AT+NAME=MyDevice: read or set the Bluetooth name. AT+CONT=0/1: make the module connectable or non‑connectable. AT+CTOUT=? / AT+CTOUT=100: query or set the connection timeout (units of 10 ms, range 10‑3200). AT+AUST=? / AT+AUST=100: query or set the automatic sleep interval (default 20 s, range 1‑300 s). AT+BAUD=xx,y: configure UART baud rate and parity (e.g., 9600,NONE, 115200,NONE).
Hardware Wiring
Connect the HC‑08 to the STM32 via a cross‑wired UART (module TXD ↔ MCU RXD, module RXD ↔ MCU TXD) and share GND and VCC (3.3 V). Use a USB‑to‑TTL adapter for initial testing. The article provides wiring tables for both the PC serial adapter and the STM32 pins (e.g., VCC → 3.3 V, RXD → A2, TXD → A3, GND → GND).
For flashing the firmware, the ST‑Link V2 is connected to the STM32’s SWCLK, SWDIO, GND, and 3.3 V pins.
UART Driver Code (STM32 HAL)
uint8_t bt_uart_rx_buf[BT_RX_BUF_SIZE];
uint8_t bt_uart_tx_buf[BT_TX_BUF_SIZE];
uint16_t bt_uart_rx_len = 0;
void bt_init(uint32_t baudrate)
{
bt_uart_handle.Instance = BT_INTERFACE; // BT UART
bt_uart_handle.Init.BaudRate = baudrate;
bt_uart_handle.Init.WordLength = UART_WORDLENGTH_8B;
bt_uart_handle.Init.StopBits = UART_STOPBITS_1;
bt_uart_handle.Init.Parity = UART_PARITY_NONE;
bt_uart_handle.Init.Mode = UART_MODE_TX_RX;
bt_uart_handle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
bt_uart_handle.Init.OverSampling = UART_OVERSAMPLING_16;
HAL_UART_Init(&bt_uart_handle);
}
void bt_rx_clear(void)
{
memset(bt_uart_rx_buf, 0, sizeof(bt_uart_rx_buf));
bt_uart_rx_len = 0;
}
void BT_IRQHandler(void)
{
uint8_t receive_data = 0;
if(__HAL_UART_GET_FLAG(&bt_uart_handle, UART_FLAG_RXNE) != RESET)
{
if(bt_uart_rx_len >= sizeof(bt_uart_rx_buf))
bt_uart_rx_len = 0;
HAL_UART_Receive(&bt_uart_handle, &receive_data, 1, 1000);
bt_uart_rx_buf[bt_uart_rx_len++] = receive_data;
}
if(__HAL_UART_GET_FLAG(&bt_uart_handle, UART_FLAG_IDLE) != RESET)
{
printf("recv: %s
", bt_uart_rx_buf);
bt_rx_clear();
__HAL_UART_CLEAR_IDLEFLAG(&bt_uart_handle);
}
}
void bt_send(char *fmt, ...)
{
va_list ap;
uint16_t len;
va_start(ap, fmt);
vsprintf((char *)bt_uart_tx_buf, fmt, ap);
va_end(ap);
len = strlen((const char *)bt_uart_tx_buf);
HAL_UART_Transmit(&bt_uart_handle, bt_uart_tx_buf, len, HAL_MAX_DELAY);
}In main(), the HAL library is initialized, system clock set to 72 MHz, a debug UART (115200 bps) is started, and the Bluetooth UART is initialized at 9600 bps. A simple loop repeatedly sends the string "bt send\r\n" every second.
int main(void)
{
HAL_Init();
sys_stm32_clock_init(RCC_PLL_MUL9);
delay_init(72);
usart_init(115200);
bt_init(9600);
printf("蓝牙实验……
");
while(1)
{
bt_send("bt send
");
delay_ms(1000);
}
}Testing with a PC Serial Assistant
Using the USB‑to‑TTL adapter, open a serial terminal at 115200 bps, send AT and verify the OK response, then query AT+VERSION to confirm the module firmware. If the module does not respond to AT commands after a successful Bluetooth connection, try a different terminal program.
Mobile App Interaction
After confirming the module works via the PC terminal, launch a Bluetooth helper app on a smartphone, connect to the HC‑08, and observe the LED behavior (blinking = waiting, solid = connected). Data sent from the phone appears on the MCU’s debug UART, and data sent from the MCU (via bt_send) is displayed on the phone app.
Conclusion
The guide equips readers with the knowledge to download example code, understand HC‑08 specifications, wire the hardware, use AT commands for configuration, and write STM32 HAL code for UART‑based Bluetooth communication, enabling rapid prototyping of IoT and embedded projects.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
