cancel
Showing results for 
Search instead for 
Did you mean: 

How do I print floats, fixed point numbers, integers, arrays full of some data type, etc. to a serial monitor?

CR72
Associate II

Hi, I would like to print floating point, fixed point values, integers, arrays of values, etc. to a serial monitor of some kind for debugging purposes. I want to be able to see exactly what my code is doing (I know floats are HUGE in embedded code so I really won't use them, but the principle is what counts). It seems that using CDC_Transmit_FS(&buff, sizeof(buff)) from the USB VCOM class doesn't work. I've even tried to connect my stm32 to an arduino via uart to maybe send the data to the arduino and have the arduino print to the serial monitor (yeah, I know that's a really dumb way to do it), but that hasn't worked out so far. I've researched a lot to try and figure out how to do this. All I can seem to find has me make a uint8_t variable[] = "Text\r\n" (not sure why the empty square brackets are used), and I can't figure out how this might or might not apply to those fixed point, integer, and array, etc. data types.

I also wonder what the maximum fixed point or integer integer value, or array size, etc. that I can print. I'm not sure what I need to understand in order to know the answer to that question though.

I can get this to work and that's about it:

uint8_t buffer[] = "Hello, World!\r\n";

CDC_Transmit_FS(buffer, sizeof(buffer));

I don't want to only print strings, however, but that's all I can do at the moment.

I'm also pretty new to stm32.

Thank you in advance for taking the time to read and respond :smiling_face_with_smiling_eyes:

14 REPLIES 14

can you use things like itoa() and dtoa() ?

uint8_t buffer[64];

CDC_Transmit_FS(buffer, sprintf((char *)buffer, "%ld\r\n", dbl));

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Guillaume K
ST Employee

Are you using an ST board like a Nucleo or Discovery board ? the ST boards have a STLINK processor used to program and debug the STM32, but also it can output one of the STM32 UART to USB virtual COM port. On STM32 side it looks like you output characters to serial UART port and on PC side it looks like a serial COM port on USB. You just have to know which serial port to use on STM32 side. On PC side, if you install STM32CubeIDE or STM32CubeProgrammer the USB driver are installed. You can use tera term or others for serial terminal emulator.

Andrew Neil
Evangelist III

@CR72​ - "I don't want to only print strings"

Use sprintf() to create formatted strings from all sorts of different data types.

"I'm also pretty new to stm32."

This is not specific to STM32 - it's general C stuff.

Are you familiar with any other microcontroller(s)?

Are you familiar with C programming in general?

@Guillaume K​ - "On STM32 side it looks like you output characters to serial UART"

It doesn't just look like it - that is exactly what you are doing!

@Community member​ 

"can you use things like itoa() and dtoa()"

 I'm not sure, but I'll check

"uint8_t buffer[64];

CDC_Transmit_FS(buffer, sprintf((char *)buffer, "%ld\r\n", dbl));"

I'll give this a go and get back to you

@Guillaume K​ I am using the STM32F072B-DISCO board

@Andrew Neil​ 

Thank you, I'll try the sprintf() function.

"Are you familiar with any other microcontroller(s)?"

I have used Arduino and that's about it, and I thought STM32 was a good way to improve my embedded programming skills and knowledge

"Are you familiar with C programming in general?"

I am also pretty new to C, only used what aspects of Arduino programming qualify as C (which I think it technically is)

KKIM.6
Senior

Your situation is very similar with me. I also started aduino and I'm working on stm32 from 1 month ago.

I also had a problem printing float number and I found that we have to activate an option before use printf() commend for float number.

follow is photo.0693W00000NpH2rQAF.pngFirst, right click of your project and enter the property. next go to c/c++ build/tool setting/MCU setting/ and check "Use float with printf from newlib-nano (-u _printf_float)".

Otherwise, you will see "?" massge only when you print float number.

To see the massage using UART, you can use printf() like below.

float abc = 3.123456

printf("massaga %.3f", abc);

result: massage 3.123

KKIM.6
Senior

and use fflush(stdout); followed by printf().

Otherwise, there will be a delay to print the massage.