We’ve worked on numerous embedded projects which avoid the use of printf()
due to concerns about binary size bloat. Instead, teams will create their own printf
alternative. A classic example is the Arduino SDK’s Print
class, which requires users to split print statements into multiple function calls to support different variable display formats.
We just have to say it: we don’t like the Arduino printing strategy at all. Give us our trusty printf
that we’ve known and loved for decades. So, when one of our clients asked us to implement a few libraries for Arduino, we decided to tackle the printf
problem.
The arduino-printf library adds support for the printf
family of functions to Arduino projects. This library leverages the wonderful mpaland/printf library, which is specifically designed for use in embedded systems.
The default configuration of the library uses the Serial
class for output, and no library initialization is required. Any class derived from the Print
base class can be used with the printf
library by calling printf_init()
. Users are responsible for initializing the output object.
printf_init(Serial1);
Serial1.begin(115200);
You can also override the _putchar
function provided by the library with your own implementation if you need more complex printing behavior (such as output to multiple sources). All that is required is to define _putchar
in your Sketch:
void _putchar(char character)
{
// For some reason, I want spaces in between every character
Serial.print(character);
Serial.print(' ');
}
Example sketches showing use of the library are provided within the repository.
embeddedartistry/arduino-printf
is released under the MIT license.