Transmitting strings and numerical values by USART in STM32 microcontrollers

Have you ever wondered how to send strings and numbers using USART in micro-controllers? No worries! The answer is here! The other day I was cleaning up and giving some order to  my STM32 based projects, to extract various useful utilities and functions that I wrote for my work or university projects. Over the year I wrote many of these functions but whenever I start a new project, going half the way of writing a function I remember that I have already done that particular function in another project in the past. So I decided to manage these utilities in a GitHub repository for quick access whenever I would need them. Then I said, why not share it with others so they can benefit too? So I hope in next few weeks I gradually gather and add these utilities to the GitHub repository that I have created. Now, for the first batch of cleanup, I found some useful functions which make life easier when it comes to transmitting strings and numerical values over USART. So lets see what it is!

This will be a pair of header and source file, used to easily transmit strings and numerical values over USART. It has basically three functions. All these three functions are prefixed with UU_ which stands for UsartUtils. Here goes the first and must important one:


This function checks for availability of given USART port and then puts a single character in the desired USART’s send register and orders the USART to transmit the character afterwards:


This function receives a string such as “stm32f103” as its second argument, and then uses the UU_PutChar function to transmit each character of the given string from beginning to end (of course, until it reaches the termination character):


This one is a bit more complicated. Imagine you want a plain integer (let it be u8 up to u32). If you want to see it as ASCII code on the receiver side, you must convert the integer to characters! And guess what, the following function does exactly this (and I believe it has a reasonable conversion speed!)

Please note that the largest integer which can be transmitted using this function is 4,294,967,295 or 0xFFFFFFFF. If you exceed this number, it will be turncated e.g. if you pass 4,294,967,296 as the second argument, what you will receive on the other side, would be 0!

Another point is that, as you can see, these calculations are rather expensive. A better solution, if applicable, is to send raw bytes to your target (e.g. PC) and parse the values on the target side.


To use the functions, just add the header and the source file (You can get it from the GitHub repository) and call the functions whenever you want! Please note that these functions are all based on the ST’s  HAL Driver (Hardware Abstraction Layer). But If you are not using HAL in your project, I guess it is not difficult at all to convert the functions to meet your needs!

Some examples

Check the complete header and source files HERE.

Saeid Yazdani

An Electronics Engineer with passion in Embedded Systems, Wireless Sensor Networks, Assembly, Desktop and Mobile Development, Web Development and generally anything that has to do with hardware and software of any kind!


  1. If you faced any problems with this utility, let me know!

  2. Thank you for the code. How do you receive a string ?

    • It is a bit tricky but I will write a guide very soon. basically you have to make a buffer of characters, then on each receive of character over usart, you should monitor the data received interupt and put the newly received character inside that buffer.

  3. How can i recieve an array in an function …..

    • You need 2 extra arguments….1)pointer to the beginning of the array and 2) number of elements in the array.

      Then use a for or while loop to go over the elements.

  4. Thank you.
    How can i putnumber point?
    example.. 1234.567

    • Simple…first transmit the integer part, then transmit a decimal point like a normal character, then transfer the fraction part (after the decimal point)

  5. Marcos Matos

    Great tutorial! I just had to change the code a bit:
    In function:

    void UU_PutChar(USART_TypeDef* USARTx, uint8_t ch)
    while(!(USARTx->SR & USART_SR_TXE));
    USARTx->DR = ch;

    Change SR and DR to ISR and TDR:

    while(!(USARTx->ISR & USART_ISR_TXE));
    USARTx->TDR = ch;

    Worked like a charm!
    I´m using STM32F0.

    Anyways, thanks for sharing this with us! It really helped.

Leave a Reply

Your email address will not be published. Required fields are marked *