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():
- 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.
- 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):
- Search for the following line of code:
- Replace that line of code with:
- 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.