How to Configure UART & Transmit Data in STM32
This tutorial explains how to configure UART in STM32 using STM32CubeMX and transmit data using HAL UART functions. It covers basic UART setup, selecting the correct baud rate, and sending strings or numbers through the STM32 UART peripheral in blocking mode. This is the first step in learning serial communication with STM32, ideal for beginners looking to interface STM32 with a computer or external modules using UART.
This is the first tutorial in our series on the UART peripheral in STM32 microcontrollers. In this tutorial, you will learn how to set up UART communication in STM32 using STM32CubeMX and send data using the HAL UART function. You will understand:
- The difference between UART and USART
- How to set the baud rate, data bits, and other UART settings
- How to use STM32CubeMX to generate code
- How to write code to send text and numbers over UART
- How to view the data on a serial monitor using a USB-to-TTL converter
This guide is perfect if you’re just starting with STM32 serial communication.
Here are some more STM32 UART tutorials you may find useful:
- UART with Interrupt and DMA
→ Learn how to send data without blocking the CPU using interrupt and DMA. - How to Receive Data using UART
→ Learn how to receive data 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.
Complete UART Code Example
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.
Project FAQs
Yes, you can connect STM32 to a PC using a USB-to-TTL converter. The data sent over UART can be read by serial monitor tools like PuTTY, RealTerm, or Arduino Serial Monitor.
STM32CubeMX usually sets the default baud rate to 115200. However, you can manually change it to match the requirements of your external device or serial monitor.
You can connect the TX pin of the STM32 to a USB-to-TTL module, open a serial monitor, and check if the transmitted data appears correctly. You may also use an oscilloscope to view the signal.
While it’s possible, blocking mode is not ideal for real-time data logging. It may cause delays in sensor reading. For better performance, consider using UART with interrupt or DMA.
In most cases, HAL_UART_Transmit
handles internal timing, so delays aren't required. But in time-sensitive applications or when sending large chunks of data, a small delay may help prevent data overflow or ensure smooth transmission.
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! 💙