How to Configure UART & Transmit Data in STM32
Learn how to configure STM32 UART in CubeMX, set 115200 baud, 8N1 frame, and use HAL_UART_Transmit to send strings & numbers to a serial console. The project is available for download at the end of the post.
UART is one of the most commonly used serial communication protocols in microcontrollers like STM32. This guide explains what UART is, how to configure it in STM32CubeMX, and how to use HAL_UART_Transmit() to send data to a serial terminal. We’ll also explore the difference between UART and USART, and typical use cases like STM32H723ZG pinouts, and sending strings and numbers over UART.
VIDEO TUTORIAL
You can check the video to see the complete explanation and working of this project.
Check out the Video Below
Introducing STM32 UART
The UART (Universal Asynchronous Receiver/Transmitter) peripheral in STM32 microcontrollers provides a simple and efficient way to perform serial communication. It allows data exchange between the microcontroller and other devices such as PCs, GPS modules, Bluetooth modules, or other microcontrollers using standard asynchronous protocols.
UART communication is asynchronous, meaning it doesn’t require a separate clock line—only TX (Transmit) and RX (Receive) lines are used. STM32 MCUs often provide multiple UART/USART interfaces, and they can be configured using STM32CubeMX and HAL libraries to suit different baud rates, word lengths, parity, and stop bits.
STM32 supports both UART and USART peripherals, where USART adds synchronous mode (with a clock line) in addition to asynchronous UART mode.
Features of STM32 UART:
- Configurable Baud Rate
Supports a wide range of baud rates (e.g., 9600 to 1+ Mbps), allowing flexible communication speed based on the application. - Full-Duplex Communication
Simultaneously sends and receives data through separate TX and RX lines. - Interrupt and DMA Support
UART can be operated in polling, interrupt-driven, or DMA mode for efficient, low-latency data handling. - Flexible Frame Format
Supports custom configurations like 8/9 data bits, 1/2 stop bits, and optional parity for error checking.
UART vs USART
UART stands for Universal Asynchronous Receiver Transmitter whereas the USART stands for Universal Synchronous Asynchronous Receiver Transmitter. The term Synchronous enables the USART to send an additional clock signal to the receiving device. The data is then sampled at a predefined edge (Rising or Falling) of the clock. The USART mode uses 3 pins (clock, Tx and Rx) compared to the 2 pins (Tx and Rx) used in the UART.
The most widely used synchronous communications are I2C, SPI, etc. The USART, in a way, can act as the SPI master to send the data to the slave. The synchronous communication is also used when communicating with a smartcard.
Here is a comparison table between UART and USART based on the information you provided:
Feature | UART | USART |
---|---|---|
Full Form | Universal Asynchronous Receiver Transmitter | Universal Synchronous/Asynchronous Receiver Transmitter |
Communication Type | Asynchronous only | Supports both Synchronous and Asynchronous |
Clock Signal | Not used | Uses an additional clock line in synchronous mode |
Number of Pins Used | 2 (TX and RX) | 3 in synchronous mode (TX, RX, and CLK) |
Data Sampling | Based on start and stop bits (internal clock) | Data is sampled on the rising or falling edge of external clock |
Application Example | Basic serial communication with PCs, Bluetooth modules | Smartcard communication, SPI-like data transfers |
Acts Like SPI Master? | ❌ No | ✅ Yes, in synchronous mode |
Use of Start/Stop Bits | Required for data framing | Optional in synchronous mode |
We will use the UART for the major part of this series as it has wider applications compared to the USART.
What Is UART Protocol in Microcontrollers?
UART (Universal Asynchronous Receiver/Transmitter) is a protocol used for asynchronous serial communication. It works with just two lines—TX and RX—without the need for a clock line. In STM32 microcontrollers, UART is used for logging, debugging, Bluetooth communication, GPS modules, and more.
In contrast to USART, which supports both synchronous and asynchronous modes, UART is simpler and widely adopted across microcontroller platforms. STM32CubeMX and HAL drivers make it easy to configure UART for your custom needs.
CONNECTION
We will use the STM32 MCU to send the data to the computer. Some of the Nucleo and Discovery dev boards from ST supports the virtual com port. This feature enables the USB connected for the ST-link to be also used for the data transmission between the MCU and the computer.
The Virtual Com Port is supported by many Nucleo and Discovery boards but not all. You need to check the schematic of the board to confirm whether the respective board supports it.
Below are the images from the schematic of the Nucleo F446RE and Discovery F412.
As you can see in the images above, both Nucleo F446RE and Discovery F412 supports the USB Virtual Com Port. So if you are using either of these boards, you do not need to use an additional module to communicate to the computer. The USB used for the ST link can also be used for the communication.
Not all the boards support this Virtual Com port feature. Below is the image from the schematic of the very famous STM32F4 Discovery board.
As you can see in the image above, there is no virtual com port in the F4 Discovery board. In such cases we can use some module to convert the UART signals to the USB, which is connected to the computer.
Below is the image showing the connection between the MCU and the FT232 USB to UART converter.
The UART is always connected in the cross connection, connecting the TX pin of the MCU to the RX of the device and the RX to the TX of the device. The module then connects to the computer using the USB.
STM32 CubeMX UART Configuration
Below is the image showing the UART configuration in CubeMX.
We will use the Asynchronous Mode for the communication. Only 2 pins are used in the Asynchronous mode, TX and RX. The baud rate is set to 115200. We need to use the same baud rate for the receiver device also.
Word length consists of the data bits and the parity bit. STM32 supports different word lengths of 7 bits, 8 bits and 9 bits. A typical UART frame is shown in the image below.
The frame consists of a start bit, data bits and stop bits. we are using 8 data bits with no parity bit and 1 stop bit. We need to use the same configuration in the receiver device also. The data direction is set to Receive and Transmit.
The oversampling is used to increase the tolerance of the receiver to the clock deviation. But it also reduces the maximum baud rate the device can achieve. The image below explains the oversampling and max baud rate.
This is all the configuration we need to do in the cubeMX. Let’s see the code now.
THE CODE
We can only send the ascii characters via the UART. Below we will see how to send a simple string and a number, by converting it to the character format.
Send string
We can directly send a string via the UART. The code below sends the string every 1 second.
uint8_t data[] = "Hello world\n";
int main()
{
....
while (1)
{
HAL_UART_Transmit(&huart2, data, 12, 1000); HAL_Delay(1000);
}
}
Here we define an array (data) which contains the string to be sent to the UART. Then call the function HAL_UART_Transmit in the while loop every 1 second.
The parameters of the function are as follows:
- @UART Instance, huart2
- @pointer to the data array, data
- @size of the data in bytes, 12
- @timout in case of error, 1000ms
Send number
As I mentioned we can not send the number directly to the UART. It only transmits the data in the ascii format. To send the number, we first need to convert each digit of the number to the character format and then send the data to the UART.
#include <stdio.h>
uint8_t number = 123;
uint8_t numarray[4];
int main()
{
....
while (1)
{
sprintf(numarray, "%d\n", number);
HAL_UART_Transmit(&huart1, numarray, 4, 1000);
HAL_Delay(1000);
}
}
Here we will send the number 123 to the UART. We first need to define an array of size at least the number of digits of the number.
The sprintf function is used to convert the number to the character format. The format specifier, %d, is used to convert the integral value (123).
After conversion the characters are stored in the array, numarray. We send this array to the UART every 1 second.
RESULT
The Serial console on the computer should have the same configuration as we did for the STM32 in the cubeMX. The image below shows the configuration with the baud rate of 115200, 8 data bits with 1 stop bit and no parity.
The data sent by the STM32 is received in the serial console. The image below shows the data, “Hello world”, printed several times. It was sent every 1 second by the MCU.
We also sent the number to the UART. The image below shows the number output on the console.
In this STM32 UART example, we’ve covered how to configure asynchronous communication, send strings and numbers using HAL_UART_Transmit(), and understand how UART works in microcontrollers. We also clarified the difference between UART and USART, and looked at how to handle UART pinouts like in STM32H723ZG. For more advanced topics, stay tuned for our DMA-based UART communication guide.
PROJECT DOWNLOAD
Info
You can help with the development by DONATING Below.
To download the project, click the DOWNLOAD button.
STM32 UART FAQs
UART (Universal Asynchronous Receiver/Transmitter) enables serial communication over two wires—TX and RX—with configurable options like baud rate, word length, parity, and stop bits. Unlike USART, which supports synchronous mode with a clock line, UART works asynchronously and is widely used for logging, debugging, and communication with modules like GPS or Bluetooth.
Enable the UART peripheral (e.g. USART1) in CubeMX.
-> Set frame format to commonly used 8 data bits, no parity, 1 stop bit (8N1).
-> Choose a baud rate (e.g. 115200 bps).
-> Assign TX and RX pins to the correct alternate-function GPIOs.
This config ensures compatibility between devices and provides stable communication
Use HAL_UART_Transmit(&huartX, dataBuffer, length, timeout)
to send data in blocking mode. You can send strings or numeric values over UART, typically to a serial terminal via a PC or host device. Transmission occurs in the foreground, blocking until complete or timeout
-> Blocking mode: CPU is paused while sending; simple but may stall other tasks.
-> Interrupt mode: Uses HAL_UART_Transmit_IT()
, allowing other tasks to run; callback triggers on completion.
-> DMA mode: Offloads data transfer from the CPU, ideal for large or continuous data streams—CPU remains free to perform other operations
Common use cases include:
-> Logging or printing debug messages to a serial console.
-> Communicating with external modules (Bluetooth, Wi‑Fi, GPS, etc.).
-> Transferring strings, sensor data, or formatted numeric values over serial interfaces using HAL_UART_Transmit()
or its interrupt/DMA variants
You May Also Like
🙏 Support Us by Disabling Adblock
We rely on ad revenue to keep Controllerstech free and regularly updated. If you enjoy the content and find it helpful, please consider whitelisting our website in your ad blocker.
We promise to keep ads minimal and non-intrusive.
Thank you for your support! 💙