STM32 Serial Communication With PC (STM32 UART To USB)

In this tutorial, we’ll create an STM32 Serial Communication With PC example project. Using an STM32 With UART To USB TLL converter chip to send serial data from the STM32 microcontroller to the PC over UART. Without further ado, let’s get right into it!

Table of Contents

  1. STM32 Serial Communication With PC Example
  2. STM32 Communication With PC Example (UART To USB)
  3. Wrap Up

STM32 Serial Communication With PC Example

This is an overview of the steps for implementing this STM32 Serial Communication With PC example project using UART To USB converter:

  • Configure GPIO input pin (for push-button) & output pins (for LEDs)
  • Configure UART in asynchronous mode @ 9600 bps + Enable RX interrupts
  • Read the button state and send it to the PC via the serial port
  • Read the received characters from the PC and decide which LED to be toggled

STM32 Communication With PC Example (UART To USB)

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 is 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 using 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

STM32 With UART-USB TTL Converter (Wiring)

STM32 Serial Port USB-TTL PC Interfacing UART Send Receive Example

STM32 Serial Communication With PC (UART To USB) Code Example Project

Step #1

Open 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

  • Set the baud rate to 9600 bps
  • Enable UART global interrupts in NVIC tab

Step #5

Configure The Required GPIO Pins For This Project

  • PB12, PB13: Output Pins (For LEDs)
  • PB14: Input Pin (For The Push Button)

Step #6

Goto The RCC Options Tab & Enable External Crystal

How To Use STM32 Nucleo32 Serial Port UART 1

Step #7

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

STM32 Clock Tree Configuration CubeMX

Step #8

Generate The Initialization Code & Open The Project In CubeIDE

Step #9

Write The Application Layer Code For This LAB

Step #10

Build & Debug To Flash The Code

Step #11

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

STM32 Serial Port PC Communication USB-TTL

Step #12

Open The Terminal From CubeIDE or Any Other Terminal

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 Communication With PC (UART To USB) Testing Demo


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 an STM32 Serial Communication With PC using a UART To USB converter. This can be very useful for debugging your STM32 projects and visualizing data in runtime.

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?

2 thoughts on “STM32 Serial Communication With PC (STM32 UART To USB)”

Leave a Comment