#227 ✨printf for Arduino✨(and ESP32, ESP8266) easy, formatted output

preview_player
Показать описание

► You can now help support my channel! Buy Me A Coffee!

► READING

► CODING
Download: Embedded artistry's modified printf for the Arduino

Reference: Marko Paland's original printf (on which the above is based)

► Example sketch (as per the demo) PLUS the library for Arduino users

► Why floats (and doubles) might be bad for your sketch

► RECOMMENDED COMPONENTS
Coming soon! Products I've bought and used and which I've found great!

► List of all my videos
(Special thanks to Michael Kurt Vogel for compiling this)

► If you like this video please give it a thumbs up, share it and if you're not already subscribed please consider doing so and joining me on my Arduinite (and other μControllers) journey

My channel, GitHub and blog are here:
------------------------------------------------------------------
------------------------------------------------------------------
Рекомендации по теме
Комментарии
Автор

Good info, but if I am forced to use an older board such as an uno, nano, pro, etc then I tend to go the sprintf route as it doesn't require loading additional libraries and achieves the same thing. Bonus, the code works on pretty much anything(no outside library dependence that may not support your new MCU). And I know you mentioned that Serial.printf works on the ESP8266/32, but it also supported on a lot of the newer development boards out there as well (though your mileage may vary when trying to print floats/doubles). But as I mentioned earlier, if you stick with sprintf and Serial.print, I don't think you'll ever run into a system it fails on. It may be an extra line to print(over printf), but it is still far better than what you showed at the beginning of the video.

bipolarchemist
Автор

Those who can read should check out the books... Although, the BBC series was quite faithful to the written work.

If you understand floats, they work quite well. The computer uses binary -- not decimal -- so decimal values must be converted to binary before they are used/stored and the results must be converted back to decimal before printed. This is entirely different from calculators that use binary coded decimal (BCD). Only 24/32 bits are float precision; the other 8/32 bits are for the exponent.

printf() has about 2 kBytes of program memory overhead, so for microcontrollers smaller than the ATMega328p, using that might limit your program size.

byronwatkins
Автор

I use sprintf() a fair bit on both the Arduino and esp32. Didn't know I could use printf() on the esp32, but certainly will now. And I think I'd better download this arduino printf library too.
You always teach me something new Ralph. Thank you Sir.
Watching on my phone (as always).

TYGAMatt
Автор

The reason the original Arduino library omitted printf was the standard implementation is huge, and would consume an unfeasible amount of the ROM and RAM space on the ATMEGA324 used on the original arduinos. Marco's implementation is much smaller making it practical on an Uno. Of course, in reality most simple Arduino projects don't use much code space, so the convenience of printf may be warranted, especially while debugging. Its a lot easier to comment out a line or 2 of debug statements than huge blocks of code!

IanSlothieRolfe
Автор

Many thanks for this update. I am certainly going to take full advantage of 'printf' as you describe it in this video. I have always hated Arduino IDE for NOT including this feature. My last comment: your thumb had an "issue" which I assume hs been 'sorted'. Like the new video format you are using, too.
UPDATE: Using VSC & PlatfromIO, I placed in my project folder's /lib folder, a folder /LibPrintf which has LibPrintf.cpp and LibPrintf.h. AND I had to include the /extras folder, too. So far this is brilliant and you 'prodding' has done the trick. However, when I had PlatformIO download and install the LibPrintf library in my project folder, it couldn't find the /extra folder it said. VERY CURIOUS!

thorntontarr
Автор

Ive used the sprintf in the past but i didnt know about the *. that is pretty interesting. Thank you for the video.

annybodykila
Автор

Hey Ralph, congrats on another nice video. Since you're on the subject of Serial.print, Debug, printf, etc. you might be interested in this library: Streaming by Mikal Hart. I never write a program without it because instead of all those repetitive Serial.print statements, you can do this:
Serial << "Counter1=" << counter1 << " Counter2=" << counter2 << endl;
The programmer can just 'stream' items out one after one. (endl is the same as \n but also clears buffers).
With the Streaming library, << also works for any .print or .println. So you can do: Oled << "This Is The Title"; instead of Oled.print("This Is The Title");.
This type of print formatting is not as powerful as printf, but it's really easy to use when you don't need a lot of specialized formatting. The Library also includes some rudimentary formatting as described in the examples.

johntoe
Автор

Great work Ralph, I have been using printf since your video #168 where you introduced MCUdude minicore. He implements it natively in Serial so you can use Serial.printf(...) . What did you do to your thumb?

andrewtoogood
Автор

Thank you for taking the time and effort to create this tutorial.

AtlantaTerry
Автор

Great video. This library has proven to very useful in my projects. However I have 2 caveats for you viewers using PlaformIO. If one uses PlatformIO library manager to install this library, for some unexplained reason, the folder "Extras" which is referred in the LibPrintf .cpp and .h files, is not installed and therefore the project fails to compile. The solution is to download it from GitHub and manually install it. The second caveat is with the latest version of the library, V1.2.6. Although the library should use the Serial stream as its default, it does not. One has to initialize the printf object with the command "printf_init(Serial) in the setup function. Interestingly these "undocumented features" are not present when one uses the Arduino IDE. Thanks again for your great video.

jacquestheberge
Автор

Ralph S. Bacon, a hoopy frood who knows where his towel is.

catandtheostrich
Автор

Who's not heard of The Hitchhikers Guide To The Galaxy?
When I was on a trip to London in 1999, I managed to get the BBC radio play on cassette tapes :-)
I had failed to find it in Norway, but was happy when I got it :-)

thorbjrnhellehaven
Автор

Another great and very informative video, particularly with your earlier video on saving space with debugging statements.

OsoPolarClone
Автор

Best thing before sliced bread? Cornish pasty?

dazryan
Автор

Thanks Ralph. I'll be pointing my students to this video. Very well explained!

gpTeacher
Автор

alternately, without having to include a library:
char pout[250] = {}; // at the top of your code, globally.

// later in your code
sprintf(pout, "Iteration %i has a value of %li (derived from %6.10f)", intValue, longValue, floatValue);
Serial.println(pout);

The length of your line is limited to the size of the pout array (250 chars).

kazzle
Автор

Floats, when used properly, are accurate!

juliocosta
Автор

good vid, coming back to the atmel 2560 for 1 project after using the esp for years forgot about the printf not being included. Ended up using sprintf as a quick fix

PetRatty
Автор

I do MOST of my YouTube viewing on my phone. Some on my laptop. And some on the 50” tv in the lounge.👍

But I must correct you on your hhgttg…. 42 is not ‘the meaning of life’. It’s only a number.

42 is the answer to the great question of life, the universe and everything. The biggest supercomputer ever know, called Deep Thought, was built to calculate the answer to that question. And it came up with the disappointing result of ‘42’. So the planet builders of Magrathea were contracted to build an even more powerful computer, called The Earth, to calculate what the actual question was. Unfortunately, moments before it completed its computation it was unexpectedly demolished to make way for a hyperspace bypass.

jeffmoye
Автор

Thanks Ralph! What program are you using for your serial monitor?

zolara