Rebuilding the Arduino IDE to Support Sprintf with Floats

One of the biggest limitations within the Arduino IDE is the inability to render float types using sprintf() or some other print() family function. For the sake of sketch size, these functions were largely stripped down of their ability to handle float types in favour of a float-to-string conversion function for those who needed it. Anyone who does use sprintf() with a float type will be met with a '?' in place of their float. However this is not to say that the Arduino IDE cannot generate sketches capable of handling floats! In fact, the AVR compiler that the Arduino IDE uses supports floats in sprintf() – just not by default. So we need to instruct it to do so.

I’ve written up some (pretty simple) instructions that you can follow to prepare your own Arduino IDE that will support float types with sprintf():

  1. Navigate to: http://code.google.com/p/arduino/wiki/BuildingArduino and prepare your build environment so that you’ll be able to compile everything okay. Proceed with the build instructions up until Step 3.
  2. Navigate to the following file and open it using your favourite text editor (you might have a slightly different file structure depending on your operating system): /app/app/src/processing/app/debug/Compiler.java
  3. Search for the following line of code: "-Wl, --gc-sections"+optRelax,
  4. Replace that line of code with: "-Wl,-u,vfscanf,-lscanf_flt,-u,vfprintf,-lprintf_flt,--gc-sections"+optRelax,
  5. Save the file and continue with the rest of the build process.

If all goes well, you should now have a fresh Arduino IDE built from the latest source code that supports floats in sprintf(). No more nasty ‘?’ :). You’ll notice however that even basic sketches are rather large in size (~10kb). However this is a tradeoff that you must be willing to make if you want sprintf() with float support.

I hope this helps everyone – I know I certainly appreciate having float support.

6 thoughts on “Rebuilding the Arduino IDE to Support Sprintf with Floats

  1. Pingback: CREMA Firmware | CREMA: ControlleR for Espresso MAchines

  2. Hi,

    great instructions i’ve followed it on windows but I’m having a problem, i complete the process and get a fresh new built of the IDE but, when i use it to compile my sketch I’m still getting the ‘?’ sign instead of the float numbers when using *printf functions.

    Do you know what could be happening?

    Thanks in advanced.

    • Hey! Can you confirm that you are using the newly generated EXE file (and libraries) that you made during the compile process? Sometimes an old copy of the IDE remains and gets used accidentally.

      • Yes, I’m using the new generated EXE and all other files excetp for the “libraries” folder (my project depends on several libraries i was already using) the IDE version is 1.0.4.

        Thanks for your help!

  3. Hi Josh.
    thanks for your information. But I would rather use function “dtostrf” converting to string before sprintf , just avoding convert float in sprintf function. And I think there should be a new version IDE with the compiler which as your cured.

    • Thanks for this – I initially considered this approach, however it adds a layer of complexity to a substantial portion of the coding process. As the entire menu and its content is stored in PROGMEM, it would be near impossible to cleanly code a dynamic menu structure if I had to depend on dstrtof() to render floats. In this case, the flexibility that sprintf() affords me by including native float support far outweighs the difference in compiled program size.

      I’m curious about the new IDE and what features it will bring to the compile process.

Leave a Reply