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!

    • Hello,
      I’m using PIC16f877a and I want to send some series of variable data using UART. Would you please help me if you know.

      • I don’t have hands-on experience with PIC, but it should be quite similar…at least the conversion between numeric values to characters is 100% the same.

  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.

  6. Thank you for the code. But I don’t understand why you use value[–i] when send the data

    • Because char value[10] is filled in reverse, meaning signficant digits are at the end of the buffer, so they need to be sent reversed.

      • Thank you
        But when I send a negative number, I use value[0] for the sign + – and set int i = 1, the sign is in the wrong place. Do you have any way to set it right?

  7. Somehow I manage to set it right
    Thank you for sharing this with us! It really helped.

  8. mohammadmehdi

    can you release your getstring function ?

  9. HI,

    Is there any way to send a full file like .exe/.bin (from SD card) via UART from STM32?

    To other device or PC ?

    Thank you,

    • Yes, of course! A file is just a series of bytes. You can read the file byte by byte and on the receiving end append the bytes together to recreate the original file.

  10. Where do I find the usart.h file? I tried compiling and it says its missing? – the usart_utlities.h file is trying to call usart.h

    • I’m using STM32L496ZGT6. I’m not sure where to find the firmware package, I’m pretty sure I already downloaded the package, but I don’t have the usart.h in my files.

  11. Hi
    how can I receive a number by the USART?
    for example I want to receive 14 by usart in the STM32 and use it a number.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.