STM32 UART Tutorial: Transmit Strings and Numbers to a Serial Terminal with HAL
UART is the simplest way to move text from an STM32 to a serial terminal on a PC, which makes it the first tool most developers use for debugging. This is Part 1 of the STM32 UART series.
In this tutorial you will configure a UART in STM32CubeMX at 115200 baud, connect the board to a PC through the ST-Link Virtual COM Port or an FT232 USB to UART adapter, and send a string and a number with HAL_UART_Transmit in blocking mode. The output is then checked in a serial terminal. If you have not created an STM32 project before, begin with the Getting Started with STM32 LED blink tutorial. When blocking mode is no longer enough, Part 2 covers transmitting with interrupt and DMA, and receiving data starts in Part 3.
STM32 UART Basics
UART (Universal Asynchronous Receiver/Transmitter) sends data one bit at a time, with one wire for each direction. The link is asynchronous, which means the two devices do not share a clock line. They use only a TX line and an RX line plus a common ground, and they agree in advance on the baud rate and the frame format.
A typical STM32 has several UART or USART instances, and each one can be configured in STM32CubeMX and driven with the HAL library. The same peripheral can talk to a PC, a GPS module, a Bluetooth module or another microcontroller.
STM32 UART Features
- Configurable baud rate, from 9600 baud up to 1 Mbit/s and beyond on many series.
- Full-duplex operation, so the device can send and receive at the same time on separate lines.
- Polling, interrupt and DMA operation for different CPU load requirements.
- Frame format with 8 or 9 bits per word (7 bits on some series), 1 or 2 stop bits and optional parity.
Why Use UART in STM32 Projects
UART needs only two signal wires, and the HAL library provides ready-made functions such as HAL_UART_Transmit, so a first message takes only a few lines of code. Common uses are:
- Sending debug messages to a serial terminal on a PC.
- Connecting modules such as GPS, GSM and Bluetooth.
- Moving sensor data to another microcontroller.
- Carrying industrial protocols such as Modbus RTU when paired with an RS-485 transceiver.
If you prefer register-level access, the same peripheral is covered in the LL UART tutorials.
STM32 UART vs USART
UART stands for Universal Asynchronous Receiver Transmitter, and USART stands for Universal Synchronous Asynchronous Receiver Transmitter. A USART can work like a UART, and it can also output a clock signal on a third pin, so the receiver samples each data bit on a defined clock edge. In this mode a USART can behave like an SPI master or communicate with a smartcard. Instances that are named UART, such as UART4 and UART5 on many STM32F4 devices, have no synchronous mode.
| Feature | UART | USART |
|---|---|---|
| Full name | Universal Asynchronous Receiver Transmitter | Universal Synchronous Asynchronous Receiver Transmitter |
| Communication type | Asynchronous only | Asynchronous and synchronous |
| Clock signal | Not used | Clock output on the CK pin in synchronous mode |
| Pins used | 2 (TX and RX) | 2 in asynchronous mode, 3 in synchronous mode |
| Framing | Start and stop bits | Start and stop bits in both modes, with the clock output during the data bits in synchronous mode |
| Typical use | PC terminal, GPS, Bluetooth | Smartcard communication, SPI-like transfers |
This series uses the UART mode, because it has the widest range of applications.
STM32 UART Hardware Connection
The STM32 sends its data to the PC through either the USB connection of the ST-Link or a separate USB to UART adapter. The choice depends on the board.
Boards with a Virtual COM Port
Many Nucleo and Discovery boards connect one UART of the STM32 to the ST-Link, which makes the same USB cable that programs the board also carry the serial data. This feature is called the Virtual COM Port (VCP). Not every board has it, so check the schematic of your board. The Nucleo-F446RE routes USART2 on PA2 (TX) and PA3 (RX) to the ST-Link, and the Discovery F412 board also provides a VCP.
With either of these boards no extra hardware is needed, and the UART data appears as a COM port on the PC.
Boards without a Virtual COM Port
The STM32F4 Discovery board has no VCP, as its schematic shows.
On such boards, use a USB to UART converter such as an FT232 module. The UART always uses a crossed connection, where the TX pin of the microcontroller goes to the RX pin of the adapter and the RX pin of the microcontroller goes to the TX pin of the adapter.
Set the adapter to 3.3 V logic if it has a voltage selector, and connect the grounds of the two devices. When no ST-Link VCP is available, a USB CDC virtual COM port on the STM32 itself is another option, which is explained in the USB CDC Virtual COM tutorial.
STM32 UART Pinout Table
| STM32 pin (USART2) | Adapter pin | Function |
|---|---|---|
| PA2 (TX) | RXD | Data from the STM32 to the PC |
| PA3 (RX) | TXD | Data from the PC to the STM32 |
| GND | GND | Common ground |
The pins above are those of the Nucleo-F446RE. Other boards may use different pins, so confirm them in the CubeMX pinout view.
STM32 UART CubeMX Configuration
Open the Connectivity category in CubeMX and enable the UART instance that your board uses. For the Nucleo-F446RE this is USART2, and the generated handle is named huart2.
| Parameter | Value |
|---|---|
| Mode | Asynchronous |
| Baud rate | 115200 bits/s |
| Word length | 8 bits (including parity) |
| Parity | None |
| Stop bits | 1 |
| Data direction | Receive and Transmit |
| Oversampling | 16 samples |
The receiving device must use the same baud rate, word length, parity and stop bits. Other settings are left at their defaults.
STM32 UART Baud Rate and Frame Format
Baud Rate
The baud rate is the number of bits sent per second, and 115200 is a common value for debug output. Both devices must use the same value. The STM32 derives the baud rate from its peripheral clock, so a wrong clock configuration produces a baud rate that differs from the one shown in CubeMX.
Frame Format
Each byte travels inside a frame made of a start bit, the data bits, an optional parity bit and one or two stop bits.
With 8 data bits, no parity and 1 stop bit, which is written 8N1, every byte takes 10 bit times. At 115200 baud the link therefore carries at most 11520 bytes per second, and a 12-byte message needs about 1 ms.
Transmission time = number of bytes x bits per frame / baud rate
12 bytes x 10 bits / 115200 = about 1.04 msWord Length and Parity
The word length in CubeMX includes the parity bit. A word length of 8 bits with parity enabled therefore carries 7 data bits, and a word length of 9 bits with parity carries 8 data bits. A word length of 7 bits is available only on some STM32 series.
Oversampling
Oversampling sets how many times the receiver samples each bit. The default of 16 samples gives the best tolerance to clock differences between the two devices, while 8 samples allows a higher maximum baud rate from the same peripheral clock.
STM32 UART Transmit String
A UART sends raw bytes, and the terminal on the PC displays them as ASCII characters. A C string is already a sequence of ASCII bytes, so it can be passed to HAL_UART_Transmit directly. The code below sends a message once per second.
#include <string.h>
uint8_t msg[] = "Hello world\r\n"; /* the CR+LF pair moves the cursor to the next line */
while (1)
{
if (HAL_UART_Transmit(&huart2, msg, strlen((char *)msg), 1000) != HAL_OK)
{
Error_Handler();
}
HAL_Delay(1000);
}HAL_UART_Transmit takes four parameters:
| Parameter | Value in this example | Meaning |
|---|---|---|
| UART handle | &huart2 | The UART instance to use |
| Data pointer | msg | Address of the bytes to send |
| Size | strlen((char *)msg) | Number of bytes to send, without the terminating zero |
| Timeout | 1000 | Maximum time in milliseconds before the function gives up |
Using strlen keeps the size correct when the text changes. The timeout must be longer than the transmission time of the data, otherwise the function returns HAL_TIMEOUT before all bytes have left. The function returns HAL_OK, HAL_ERROR, HAL_BUSY or HAL_TIMEOUT. For a periodic message without HAL_Delay, use a timer interrupt as explained in the STM32 Timer Tutorials.
STM32 UART Transmit Number
The byte value 123 is not the text 123. If the number is sent as a raw byte, the terminal shows the character with ASCII code 123, which is a curly bracket. To display the digits, the number must first be converted to text, and the function snprintf does this.
#include <stdio.h>
uint8_t number = 123;
char numarray[16];
while (1)
{
int len = snprintf(numarray, sizeof(numarray), "%d\r\n", number);
if ((len > 0) && (len < (int)sizeof(numarray)))
{
if (HAL_UART_Transmit(&huart2, (uint8_t *)numarray, (uint16_t)len, 1000) != HAL_OK)
{
Error_Handler();
}
}
HAL_Delay(1000);
}The format specifier %d converts the integer to its decimal digits, and snprintf returns the number of characters written, which is used as the size for the transmit call. The buffer must have room for every digit, the line ending and the terminating zero, so the string 123 followed by CR and LF needs at least 6 bytes. Using sizeof in snprintf prevents the buffer from overflowing.
To send a real measurement, replace the constant with a value read by the ADC, as shown in the ADC single channel polling tutorial.
STM32 UART Serial Terminal Settings
The serial terminal on the PC must use the same settings as the STM32. Select the COM port of the ST-Link or the adapter, then set 115200 baud, 8 data bits, no parity, 1 stop bit and no flow control. Programs such as PuTTY and RealTerm work for this.
The terminal then shows the text sent by the STM32 once per second.
The number sent by the second example appears in the same way.
STM32 UART Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Nothing appears although HAL_UART_Transmit returns HAL_OK | Wrong COM port, wrong baud rate, or TX and RX not crossed | Check the port in the terminal, the baud rate on both sides and the wiring |
| Garbled characters | Baud rate or frame format differs, or the clock configuration is wrong | Match both sides and check the clock tree in CubeMX |
| The function returns HAL_TIMEOUT and the text is cut off | Timeout shorter than the transmission time | Increase the timeout above bytes x 10 / baud rate |
| Text is cut off or extra characters appear | Size argument does not match the data | Use strlen or the value returned by snprintf |
| Every line starts further to the right | Only LF is sent | Send CR and LF together as \r\n |
| Floating point values print as empty | Newlib-nano has no float printf by default | Enable the float option for printf in the CubeIDE project settings |
| HAL_UART_Transmit returns HAL_BUSY | The same UART is in use by an interrupt or by another task | Wait for the earlier transfer, or protect the UART with a FreeRTOS mutex |
| No COM port on the PC | ST-Link cable in the wrong port, or the driver is missing | Use the ST-Link USB port and check the Device Manager |
Conclusion
In this tutorial you configured a UART in CubeMX, connected the board through a Virtual COM Port or an FT232 adapter, and sent a string and a number with HAL_UART_Transmit. The baud rate, word length, parity and stop bits must be the same on the STM32 and on the receiver, otherwise the terminal shows garbled text.
Blocking mode suits debug messages and simple logging. At 115200 baud a 12-byte message takes about 1 ms, which is acceptable for most uses but adds up in time-critical loops or with large buffers. Part 2 of the series removes this wait, and receiving data follows in Part 3.
Download Project Here
Download the CubeMX project files and HAL source code — all tested on real hardware.
If you find this useful, you can support us with a Donation as well.
Browse More STM32 UART Tutorials

Arun is an embedded systems engineer with 10+ years of experience in STM32, ESP32, and AVR microcontrollers. He created ControllersTech to share practical tutorials on embedded software, HAL drivers, RTOS, and hardware design — grounded in real industrial automation experience.
Recommended Tools
Essential dev tools
Categories
Browse by platform











Hi, I have followed you for many years. I use your tutorials, Thank you so much
“Here is a comparison table between UART and USART based on the information you provided:”
Thanks GPT. Now I know not to use this site ever again. I did have suspicions that it was AI generated but now I’m 100% sure.
Really ? Based on that line you concluded that the post is generated by AI ? Yes that table is AI generated, but didn’t you see “ based on the information you provided:”.
It is easier to generate table with AI by providing the relevant info than typing all the rows and columns by myself.