Printf on mbed not working

Apologies all, for the dumb question.

I have been happily using printf for both my nucleo boards using mbed. Then, due to gross stupidity on my part I deleted the programs that had the printf command in them. Now I can’t get it to work again. All I can remember is that, after a lot of searching forums, manuals, spec sheets and other assorted documentation, I found I had to add two lines to the beginning of the program. One of them may have been a #include, but I’m not sure. The other was, I think, of the form “Serial something” or “Serial something(something)”, I honestly can’t remember more than that.

I have found, in my new round of searching, many references to Serial pc(USBTX, USBRX); but I am almost certain that wasn’t what I found last time. Anyway I tried it and it didn’t work, I still don’t see printf output on the serial monitor in pio. Although it does compile, so presumably the output is going somewhere.

Can somebody please point me in the right direction?


Did you create a global Serial object in your main CPP file with the desired baud rate? Pins should be to the ST-Linkv2, referenced by the USBTX/USBRX pins

Serial pc(USBTX, USBRX, "debug", 115200); //TX, RX

Then pc.printf(".....") or just printf() should work

Thanks again Max. I think I found the problem. It’s not that printf is not working, it seems that another function I was using before the printf got stuck in an infinite loop and the printf command was never reached. I was using the mbed timeout command, and had a mytimeout.attach_us(&myfunc, 1500) before the printf. When I removed this line the printf worked fine. I don’t know why the timeout function got stuck, but it just seems to have hung. Maybe it only works in a thread? Dunno. Can’t use threads on my Nano anyway, not enough memory.
Now I can’t get analog read to work. I’m getting a little be disenchanted with mbed…
Thanks again for your help,