This library adds support for the printf()
function to Arduino projects. This code leverages the embeddedartistry/printf library (a fork of eyalroz/printf, which is designed for use in embedded systems. For more information about what is available, please refer to the parent library documentation.
This library provides a standalone implementation for the following functions:
printf()
sprintf()
and snprintf()
vprintf()
and vsnprintf()
This library aims to offer a complete printf()
solution while maintaining low storage and RAM requirements.
This is critical for MCUs with limited storage and RAM. This project is ideal for AVR based MCUs like the
Arduino Uno and it's siblings.
The Arduino implementations for the ESP8266 and
ESP32 already include a printf()
implementation
as part of the base library. You do not need this library for those platforms.
To use this library in your Arduino project, you need to include the header:
#include <LibPrintf.h>
void setup() {
Serial.begin(115200);
}
By default, the library can be used without any special initialization. Any printf()
calls will be output using
the Arduino Serial
interface. If you need to use a different interface, call printf_init
.
If you only want to use s[n]printf
, then you do not need to initialize the library.
See advanced_usage.md.
If memory footprint is critical, you can disable library features using compiler definitions. Available controls are:
PRINTF_DISABLE_ALL
printf
calls from the programPRINTF_NTOA_BUFFER_SIZE
(unsigned integer)
PRINTF_FTOA_BUFFER_SIZE
(unsigned integer)
PRINTF_DISABLE_SUPPORT_FLOAT
PRINTF_DISABLE_SUPPORT_EXPONENTIAL
PRINTF_DEFAULT_FLOAT_PRECISION
(unsigned integer)PRINTF_MAX_FLOAT
(float value)
PRINTF_DISABLE_SUPPORT_LONG_LONG
PRINTF_DISABLE_SUPPORT_PTRDIFF_T
For AVR chips, the library will automatically set PRINTF_DISABLE_SUPPORT_EXPONENTIAL
and PRINTF_DISABLE_SUPPORT_LONG_LONG
. You can re-enable these settings by defining PRINTF_PREVENT_DEFAULT_AVR_SETTINGS
.
Because these settings control behavior in the source file, they cannot be defined in the sketch. You must adjust the compilation commands for your project in order for the changes to take effect.
If you're using a Makefile or other build system, you'd use the -D
flag (e.g., -DPRINTF_DISABLE_SUPPORT_EXPONENTIAL
) to the library build target. For Arduino IDE, the flags need to be added to the compiler.extra_flags property in platform.txt or platform.local.txt. You would need to restart the IDE for the changes to take effect.
Here are comparisons for a simple test sketch showing the overall sketch size for different configurations:
Type | Bytes |
---|---|
No Serial | 1606 |
All options enabled | 9476 |
Disable long long and exponential | 6328 |
Disable long long, float, and exponential | 4256 |
Multiple examples are provided with this library in the examples/ folder.
Serial
will be the default output for printf()
Serial
class and prints in a loopPrint
base class can be used with the Arduino Printf libraryprintf
with Serial1
instead of Serial
putchar_()
and adds a space in between every letterputchar_()
that you like, such as outputting information to multiple ports