STM32 Serial Print & Monitor (UART Data Debug)

In this tutorial, we’ll discuss implementing an STM32 Serial Print using UART and display the debug data on STM32CubeIDE Serial Monitor & Serial Terminal on a PC using a USB-TLL Converter. We’ll perform the STM32 serial print examples using the STM32 Blue Pill board and the Nucleo32-L432KC board.

As we’ve seen in an earlier tutorial the STM32 Serial Wire Debug (SWD) Trace feature requires access to the SWO pin which is not accessible directly in the ST-Link v2 USB clone, and also not connected on the Nucleo32-L432KC board. So we weren’t able to test the SWD trace! However, there are still a couple of other methods to debug your projects, one of which is the UART serial print. And that’s what we’ll do in the tutorial.

STM32 Debugging With UART

Table of Contents

  1. STM32 Serial Print (UART To Monitor) | Nucleo32-L432KC
  2. STM32 Serial Print & CubeIDE Monitor (USB-TTL) | Blue Pill
  3. Wrap Up

STM32 Serial Print (UART To Monitor) | Nucleo32-L432KC

The STM32L432KC microcontroller has 3 USART modules (USART1, USART2, and USART3). You can actually use any one of them to send serial data to your PC’s terminal using a USB-TTL converter. However, the USART2 module is connected to the onboard ST-Link programmer/debugger and it gets a virtual COM port on your PC. So there is no need to connect an external USB-TTL converter if you’re using USART2 peripheral. And that’s what we’re going to do in this section.

Here is the schematic diagram for the Nucleo32 board where you can see the pins PA2, PA3 (RX, TX For USART2) are connected to the onboard ST-Link MCU. I’ll have to check other Nucleo boards if this holds true for all other boards or not. You can also double-check the schematic to make sure which USART module is being used for the virtual COM serial port communication.

How To Use Nucleo32 Serial Port UART

Step #1

Open CubeMX & Create New Project

STM32 CubeMX Create New Project

Step #2

Choose The Target MCU & Double-Click Its Name

How To Use STM32 Nucleo32 Serial Port UART

Step #3

Enable USART2 Module (Asynchronous Mode)

STM32 Nucleo Serial Port Print To Terminal

Step #4

Choose The Desired Settings For UART (Baud Rate, Stop Bits, Parity, etc..)

Step #5

Go To The Clock Configuration & Set The System Clock To 80MHz

STM32 Nucleo32 Serial Port UART

Step #6

Generate The Initialization Code & Open The Project In CubeIDE

STM32 Debugging Nucleo Serial Port Print To PC Terminal

Step #7

Write The Code For Your Project & Use HAL_UART_Transmit() To Print

Step #8

Build & Debug To Flash The Code

The Nice Part About It That The Same USB Cable Used To Flash The Code Is Now Being Used To Send The Serial Data As Well.

STM32 Nucleo32 Serial Port UART 1

Step #9

Go To The Device Manager & Check The ST-Link COM Port Num.

STM32 Nucleo32 Serial Port UART 2

Step #10

Open The Terminal From The STM32CubeIDE

Window > Show View > Console

In Console:

click on the NEW icon on its menu bar > Command Shell console > Connection type: Serial port > set Baud Rate & Connection Name > Encoding: UTF-8 > And Click OK!

Alternatively, You Can Use Any Terminal On Your PC (Like Tera Term, Arduino Serial Monitor, etc..)

STM32 Serial Port Print Data With UART

Download The Project Folder


STM32 Serial Print & CubeIDE Monitor (USB-TTL) | Blue Pill

The Blue Pill development board lacks an onboard ST-Link programmer/debugger, unlike Nucleo boards. That’s why we use the external USB ST-Link clone. And also it worth mentioning that the USB port on the blue pill board is connected to the STM32F103C8 hardware USB peripheral. Therefore, it can actually be used for debugging but you’ll develop a USB application for it and it’s a topic for a future tutorial.

However, the UART peripherals in the microcontroller can be used to send serial data to the PC serial COM port and display it on a terminal using a USB-TTL converter board. Hence, you’re not restricted to use a specific UART module (UART1, UART2, or UART3).

❕ Note

The STM32F103C8 microcontrollers’ pins are not all 5v tolerant. Hence, you must be careful when receiving input signals from the USB-TTL converter. You can send a 3.3v signal from the MCU TX pin to the USB-TTL RX pin and still get the data identified absolutely fine.

However, it won’t work the other way around without shifting the signal’s level. The TX from the USB-TTL can over-drive the MCU’s RX input pin. By checking the diagram below, you’ll notice that the pins for UART1 & UART3 are 5v tolerant while UART2 is not.

stm32f103c8t6_pinout_voltage01

For this tutorial, we’ll use the UART1 module to send the serial data for debugging. Here is the connection diagram for this LAB. Note that we won’t send data from the terminal to the terminal, that’s why I didn’t connect that wire. If you need to send data to the microcontroller, you’ll have to connect it and configure the UART in your code so it handles data reception.

STM32 Debugging With UART Serial Print

STM32 Serial Data Print (For Debugging) Project

Step #1

Open CubeMX & Create New Project

STM32 CubeMX Create New Project

Step #2

Choose The Target MCU & Double-Click Its Name

STM32 CubeMX Choose The Part Step

Step #3

Enable USART1 Module (Asynchronous Mode)

STM32 Nucleo Serial Port Print To PC Terminal1

Step #4

Choose The Desired Settings For UART (Baud Rate, Stop Bits, Parity, etc..)

STM32 Debugging Nucleo Serial Port Print To PC Terminal1

Step #5

Goto The RCC Options Tab & Enable External Crystal

How To Use STM32 Nucleo32 Serial Port UART 1

Step #6

Go To The Clock Configuration & Set The System Clock To 72MHz

STM32 Clock Tree Configuration CubeMX

Step #7

Generate The Initialization Code & Open The Project In CubeIDE

How To Use STM32 Nucleo32 Serial Port UART 2

Step #8

Write The Code For Your Project & Use HAL_UART_Transmit() To Print

Step #9

Build & Debug To Flash The Code

STM32 Debugging With UART Serial PrintSTM32 Nucleo32 Serial Port UART 1

Step #10

Go To The Device Manager & Check The USB-TTL COM Port Num.

STM32 Nucleo32 Serial Port UART 4

Step #11

Open The Terminal From STM32CubeIDE Monitor

Window > Show View > Console

In Console:

click on the NEW icon on its menu bar > Command Shell console > Connection type: Serial port > set Baud Rate & Connection Name > Encoding: UTF-8 > And Click OK!

Alternatively, You Can Use Any Terminal On Your PC (Like Tera Term, Arduino Serial Monitor, etc..)

STM32 Blue Pill Serial Port UART Print 1
STM32 Blue Pill UART Serial Port Data Printing


Required Parts For STM32 Examples

All the example Code/LABs/Projects in this STM32 Series of Tutorials are done using the Dev boards & Electronic Parts Below:

QTY.Component NameAmazon.comAliExpresseBay
1STM32-F103 BluePill Board (ARM Cortex-M3 @ 72MHz)AmazonAliExpresseBay
1Nucleo-L432KC (ARM Cortex-M4 @ 80MHz)AmazonAliExpresseBay
1ST-Link V2 DebuggerAmazonAliExpresseBay
2BreadBoardAmazonAliExpresseBay
1LEDs KitAmazonAmazonAliExpresseBay
1Resistors KitAmazonAmazonAliExpresseBay
1Capacitors KitAmazonAmazonAliExpress & AliExpresseBay & eBay
1Jumper Wires PackAmazonAmazonAliExpress & AliExpresseBay & eBay
1Push ButtonsAmazonAmazonAliExpresseBay
1PotentiometersAmazonAliExpresseBay
1Micro USB CableAmazonAliExpresseBay

★ Check The Links Below For The Full Course Kit List & LAB Test Equipment Required For Debugging ★

Download Attachments

You can download all attachment files for this Article/Tutorial (project files, schematics, code, etc..) using the link below. Please consider supporting our work through the various support options listed in the link down below. Every small donation helps to keep this website up and running and ultimately supports the whole community.


Wrap Up

In conclusion, we’ve explored how to set up the STM32 UART Serial Print to Terminal (Serial Monitor) & STM32CubeIDE Monitor. This is very useful for debugging most of your STM32 projects to visualize data in runtime without using breakpoints in your code as you’d do with a debugger.

You can build on top of the example provided in this tutorial and/or explore the other parts of this STM32 UART tutorial series linked below.

This Tutorial is Part of The Following Multi-Part Tutorial Series:

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

3 thoughts on “STM32 Serial Print & Monitor (UART Data Debug)”

  1. Hi,

    I am really enjoying the Tutorials. I’ve shared them on LinkedIn, and I hope you keep doing them. IF you could do an I2C one that would be amazing, specifically making an STM32 a slave in a network would really help me and my projects.

    I did notice two things though that seems to be an issue when I run the code on my STM32F103CT8(BluePill) using the ST-Link. The line
    __HAL_AFIO_REMAP_SWJ_DISABLE(); stops the device immediately after programming with the ST-Link. Once commented out the code works fine and the debugger works too.

    Another issue that seems to be with my set up is that the sprintf in this tutorial seems to overwrite the huart address space and resets the configuration registers to contain garbage. Once commenting out the UART transmission worked fine but not sure why the sprintf causes this. I ran your code as well, and it causes the same issues. It only ever happens after the sprintf statement.

    All in all, still really helpful tutorials, please continue!

    Reply
  2. Thanks for the tutorials! They’re really helping.

    I did notice one thing in the NUCLEO section here: when initializing MSG, it’s set to 25 characters instead of 35. This truncates the string when it’s assigned using the sprintf statement (omits the %d\r\n part), producing a not-as-expected console output without the incrementing X or line feed. The Blue Pill code has the correct size for MSG.

    Reply

Leave a Comment