views
Are you stuck getting UART to work on your STM32 using HAL? It can feel so slow when data doesn’t send or an interrupt never fires. And when you do get it working briefly, a bug stops it cold and you lose time and patience. This guide on ControllersTech fixes that by showing clear steps you can follow today to get STM32 UART transmit, receive, interrupts, idle-line, one-wire, and LIN working reliably and fast.
What is STM32 UART and Why HAL Makes It Easy
STM32 UART is a way to send or read data over serial lines. The STM32 HAL library gives built-in code to start, stop, send, and get data without doing every register. That means you spend more time on what you want to build, not what you need to write in bits.
-
It cuts many register names into friendly HAL calls.
-
It works with basic functions like
HAL_UART_Transmit
,HAL_UART_Receive
, and IRQ callbacks. -
It works across STM32F103, STM32F4, STM32 Nucleo boards with one code base.
-
You can set up advanced modes like idle-line detect, single-wire half-duplex, one-wire protocol, or LIN mode with only a few configs.
How to Set Up UART Transmit and Receive
Start with STM32CubeMX to pick your MCU (F103, F4, Nucleo). Enable UART, set baud, word length, parity.
Then in code:
-
Call
HAL_UART_Init(&huartx);
inMX_UARTx_Init
. -
To send:
HAL_UART_Transmit(&huartx, (uint8_t*)data, len, timeout);
-
To receive:
HAL_UART_Receive(&huartx, buf, len, timeout);
Make sure your clock and pin config match your board’s docs. That cuts most common bugs fast.
How to Use UART Interrupts
Interrupt mode helps you get data as soon as it arrives. Set Receive_IT
mode:
-
Call
HAL_UART_Receive_IT(&huartx, buf, len);
-
In
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
, process your data. -
Restart the next receive call.
This avoids polling and makes apps feel fast.
Idle Line Detection for Variable-Length Data
Idle-line means you notice when no data arrives for a bit. It helps when packets are different sizes.
-
Enable idle-line interrupt (set
UART_IT_IDLE
). -
In ISR check
__HAL_UART_GET_FLAG
for idle. -
Use
__HAL_UART_CLEAR_IDLEFLAG
and findhuartx.Instance->RDR
to see how much came in. -
Restart
HAL_UART_Receive_IT
.
That handles unknown packet lengths with no fuss.
Single-Wire UART Setup (half-duplex)
Want half-duplex on one wire?
-
Enable
UART_ALTERNATE
for half-duplex in CubeMX. -
Call
HAL_HalfDuplex_Init(&huartx);
-
Use same transmit/receive calls or the IT version.
-
The HAL handles switching TX/RX pin internally.
That’s great for RS-485-like uses or when you only have one line for both.
One-Wire Protocol Example (like DS18B20)
For a one-wire sensor you can bit-bang via UART:
-
Set low baud (e.g. 9600) to send reset pulses.
-
Switch to custom parity to read presence bits.
-
Then do normal speed for reading data.
Many devs use STM32 HAL + UART pins to talk to one-wire sensors fast without extra hardware.
LIN Protocol with STM32 UART
LIN is a simple auto bus. STM32 supports LIN mode via UART.
-
In CubeMX turn on LIN mode.
-
Use
HAL_LIN_SendBreak
then normalTransmit
. -
To receive just use the HAL receive call.
That makes adding LIN to your STM32 easy, a good helper for car or robot projects.
Extra Terms to Know
Here are 8 related terms you may see:
-
Baud rate
-
Stop bits
-
Parity
-
DMA with UART
-
CTS/RTS flow control
-
UART wakeup from stop mode
-
Double-buffering with DMA
-
UART noise filter
Use these if you search deeper or show related posts.
FAQ
Who can use this STM32 UART HAL tutorial?
Anyone working with STM32F103, STM32F4, or Nucleo boards aiming to send or receive serial data.
What do I need to begin?
STM32 board, UART pins, CubeMX, HAL code generated, a terminal on PC or another device.
Where do I see received data?
Use a USB-to-serial adapter with a terminal app like PuTTY or Tera Term.
Why choose HAL for UART?
HAL cuts register work, speeds up code, and works across STM32 families.
How do I debug HAL UART issues?
Check pin config, clock enable, correct huartx
instance, and call MX_UARTx_Init
.
Will this work on STM32F1 and F4?
Yes. The HAL API stays same across families, only CubeMX config steps change.
How to read variable-length data easily?
Use idle-line interrupt, then check length and restart receive.
How do I set up LIN mode UART?
Enable LIN in CubeMX, then use HAL LIN calls plus normal send/receive.
Can I reuse UART for one-wire sensors?
Yes—low speed + parity trick works well for sensors like DS18B20.
What if I need full-duplex or DMA?
HAL supports DMA and full-duplex UART calls too. Just enable in CubeMX.
What This All Means
You now have steps for using STM32 UART, STM32 HAL, UART transmit, UART receive, UART interrupt, UART idle line detection, single-wire UART, one-wire protocol, and LIN protocol, across STM32F103, STM32F4, and STM32 Nucleo boards. Start simple, test as you go, and you’ll get working serial links fast.
If you like this guide, leave a comment, share what board you use, or tell the UART trick that helped you most. We’d like to hear what you build next!
