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.
STM32 ESP8266 ThingSpeak Video Tutorial
This written guide explains all the steps, wiring, CubeMX setup, and code required to send BME280 sensor data from STM32 to ThingSpeak using ESP8266. However, watching the process in action can make it even clearer. I’ve created a complete video walkthrough where I demonstrate the configuration, coding, and live data upload to ThingSpeak. Follow the written instructions here while watching the video to better understand each step and avoid common mistakes.
Watch the Video- Hardware Required for STM32 UART Example
- Introduction to STM32 UART
- STM32 UART Features
- STM32 UART Hardware Setup
- STM32 CubeMX UART Configuration
- Write Code to Transmit Data via UART
- Testing UART Output
- Blocking Mode Explained
- Next Steps for STM32 UART
- Conclusion
- STM32 UART Tutorial Series
- PROJECT DOWNLOAD
- STM32 UART FAQs
Hardware Required for STM32 UART Example
Before we begin with the project, let’s look at the hardware and software needed for this STM32 UART tutorial. Below are the affiliate links you can consider before purchasing the items.
Introduction to 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.
Why use UART in STM32 projects
UART is very useful in STM32 projects because it is simple, reliable, and supported by many devices. You can use UART to:
- Send debug messages to a serial terminal on your PC
- Connect STM32 to modules like GPS, GSM, or Bluetooth
- Transfer sensor data to another microcontroller
- Communicate with external devices without complex wiring
Another reason to use UART is that it works with only two wires: TX (transmit) and RX (receive). This makes hardware connections easy. Also, STM32 HAL provides ready-to-use functions like HAL_UART_Transmit
, so you can start sending data with just a few lines of code.
STM32 UART Features
STM32 UART provides many features that make data communication flexible and reliable. You can control how fast data is sent, how many bits are used, and how errors are checked. These options help you connect STM32 to many types of devices, such as sensors, modules, and PCs. Let us look at some key features.
Baud rate, data bits, stop bits, and parity
The baud rate defines how fast data is sent over UART. Common values are 9600, 115200, and 1M baud. A higher baud rate means faster communication, but both devices must use the same value.
The data bits tell how many bits are sent for each character. In STM32, you can choose 8 or 9 data bits. Most projects use 8 data bits.
The stop bits mark the end of a data frame. You can set 1 or 2 stop bits. Using 1 stop bit is common, while 2 stop bits can make communication more reliable in noisy systems.
The parity bit is an optional error-checking feature. You can select even, odd, or no parity. Many STM32 projects disable parity to keep communication simple.
Together, these settings define the UART frame. For example, “115200 8N1” means baud rate = 115200, 8 data bits, No parity, and 1 stop bit.
Modes of communication – blocking, interrupt, DMA
STM32 UART supports three main modes of communication. You can choose one based on the speed and complexity of your project.
- Blocking mode: In this mode, the CPU waits until the UART finishes sending or receiving data. It is simple and easy to use but keeps the CPU busy. Beginners usually start with this mode.
- Interrupt mode: In this mode, the CPU can do other work while UART sends or receives data. When the transfer is complete, an interrupt notifies the CPU. This makes programs faster and more efficient.
- DMA mode: DMA (Direct Memory Access) transfers data directly between memory and UART without using the CPU. This mode is the fastest and best for large amounts of data, such as audio or continuous sensor streams.
By starting with blocking mode and then moving to interrupt or DMA, you can build UART communication in STM32 step by step.
STM32 UART Hardware Setup
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.
The image below shows 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
The image below shows 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.
Write Code to Transmit Data via UART
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.
Sending string via UART
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
Sending number via UART
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.
Testing UART Output
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.
Blocking Mode Explained
Blocking mode is the simplest way to use UART in STM32. In this mode, the CPU waits until data is fully transmitted or received before moving to the next instruction. Many beginners start with blocking mode because it requires very little code and is easy to understand.
Advantages of blocking mode
- Easy to use: The code is simple and does not need advanced settings.
- Good for learning: Beginners can clearly see how UART works step by step.
- Reliable for small data: Sending a few characters, numbers, or short messages works very well.
- No extra hardware needed: It works directly with STM32 HAL functions like
HAL_UART_Transmit
.
Because of these advantages, blocking mode is perfect for testing and debugging UART communication in STM32.
Limitations of blocking mode
- CPU stays busy: The processor waits during the transfer and cannot do other tasks.
- Not good for large data: Long messages or high-speed transfers can slow down the program.
- Poor efficiency: In real-time applications, blocking mode can cause delays.
For projects that need multitasking or continuous communication, you should use interrupt or DMA modes instead of blocking mode.
Next Steps for STM32 UART
Once you understand blocking mode, you can move to advanced modes of UART in STM32. These modes improve efficiency and allow your program to handle more tasks at the same time.
Using UART with interrupts
In interrupt mode, the CPU sends or receives data without waiting. When the transfer is complete, the UART hardware generates an interrupt. The CPU then runs a function to handle the finished transfer.
This method frees the CPU to do other work while UART communication is in progress. It is useful for medium-speed applications like reading GPS data or sending sensor values.
Using UART with DMA
DMA mode is the fastest and most efficient way to handle UART communication. DMA moves data directly between memory and UART without involving the CPU.
With DMA, you can send or receive large amounts of data continuously. This makes it perfect for projects like data logging, audio streaming, or real-time monitoring.
Receiving data with UART
So far, we have focused on transmitting data. But in real projects, receiving data is equally important. STM32 HAL provides functions like HAL_UART_Receive
to capture incoming bytes.
You can use blocking, interrupt, or DMA mode for receiving. For small messages, blocking mode works fine. For continuous streams, interrupt or DMA is better. This step will prepare you for building full two-way communication systems with STM32.
Conclusion
In this tutorial, you learned how to configure STM32 UART transmit data using STM32CubeMX and HAL in blocking mode. We covered UART vs USART, key settings like baud rate, data bits, stop bits, and parity, and explained the pros and cons of blocking mode.
UART is simple, reliable, and perfect for sending strings, numbers, and debug messages. After mastering blocking mode, you can explore interrupt and DMA modes for faster and more efficient communication. This step-by-step approach will help you use STM32 UART in real projects such as IoT, sensors, and data logging.
STM32 UART Tutorial Series
STM32 UART PART 3 – Receive Data in Blocking & Interrupt mode
STM32 UART PART 4 – Receive Data using DMA
STM32 UART PART 5 – Receive Data using IDLE LINE
STM32 UART PART 6 – Communication using Single Wire
STM32 UART PART 7 – How to use 1-Wire Protocol
STM32 UART PART 8 – Lin Protocol PART 1
STM32 UART PART 9 – Lin Protocol PART 2
PROJECT DOWNLOAD
Info
You can help with the development by DONATING Below.
To download the project, click the DOWNLOAD button.
STM32 UART 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! 💙
Recent Posts
Random Picks
