can't print a float value???

I'm getting a problem trying to Serial.print a float value
(I've read, but he is trying to cast it to int which I don't want)
I need to read the floating point numbers

is it possible at all to do Serial.print(myFloatVar) ???

this is the debugger warning

error: call of overloaded 'print(float&)' is ambiguous
C:/DEV/_ARDUINO/arduino-0007/lib/targets/arduino/HardwareSerial.h:42: note: candidates are: void HardwareSerial::print(char)

C:/DEV/_ARDUINO/arduino-0007/lib/targets/arduino/HardwareSerial.h:44: note: void HardwareSerial::print(uint8_t)

C:/DEV/_ARDUINO/arduino-0007/lib/targets/arduino/HardwareSerial.h:45: note: void HardwareSerial::print(int)

C:/DEV/_ARDUINO/arduino-0007/lib/targets/arduino/HardwareSerial.h:46: note: void HardwareSerial::print(unsigned int)

C:/DEV/_ARDUINO/arduino-0007/lib/targets/arduino/HardwareSerial.h:47: note: void HardwareSerial::print(long int)

C:/DEV/_ARDUINO/arduino-0007/lib/targets/arduino/HardwareSerial.h:48: note: void HardwareSerial::print(long unsigned int)

do I have to modify the Serial lib to accept floating point values on the serial port?

At the moment, there's no real way to print a float. Including the system routine to do so adds a large amount of size to the Arduino core. It used to be that using floats at all meant that your sketch wouldn't fit on the chip. Now that floats fit, I imagine that the float printing routine would still take too much space. Although maybe now with lots of people using ATmega168's, it's worth investigating how to enable float printing.

May I ask why you need to print a float and why casting it (or multiplying by a 1000 or so and casting it) won't work?

thanks mellis,
the reason is I'm playing around with an accelerometer that is giving me very slight values
and I'm checking with the reference voltage that is going into the accelerometer.

I've seen that on some tutorials tom igoe is using digital pins with his pulseIn function to detect the data from the accelerometer...however i've hooked my dual axis to 2 arduino analog pins
and it's voltage reference to another arduino analog pin so I can check what Vin really is.

I know I could do that with a multimeter but I'm doing so that I can have a double reference

now for your solutions in fact does seem mathematically interesting :wink:

are you suggesting this?

float Vfloat;
int Vint
Vfloat = analogRead (VinPin); //reads 665.6
Vint = (int)(Vfloat);
Serial.print(Vint); //prints 665

or this?

float Vfloat;
int Vint
Vfloat = analogRead (VinPin); //reads 665.65
Vint = (int)(Vfloat*1000);
Serial.print(Vint); //prints 665650

another doubt is: to convert it to the 5 volt range I will have to multiply that to (5/1024)
is that correct?

Vint = (int)(Vfloat1000(5/1024));
Serial.print(Vint); //prints 3250 which means 3,2 V

that would be what my multimeter displays.
thanks as usual :slight_smile:

Your conversion (to a 5 volt range) looks right. But it might be easier to just send the value returned by analogRead() (e.g. 0 to 1023) and just convert it on the computer.

I've seen a the same and similar problems, trying to use sprintf(dest,"%f %s",floatvalue,stringvalue);

Thinking that underneath the Arduino extensions there is C, I thought this would work, but does not?

Am I missing something simple (or being stupid) here?


Arduino is C underneath.

A very common mistake is thinking that functions from the standard libraries on unix (e.g. printf() and friends, or math functions like pow()) are part of the C language. They are not - they are add-ons that come from libraries or the operating system.

Another hurdle for folks who have programmed on larger systems but are programming a microcontroller for the first time is the size of available resources: The Arduino has 8k of program space (1k of which is occupied by the bootloader) and 1k of RAM, and it doesn't have things like an operating system, virtual memory, etc. Compare this to a typical desktop computer that has RAM measured in Gb and a few hundred Gb of disk space (which can be combined to make a virtual memory system limited only by disk space), has an OS with many megabytes of library and kernel functions to call upon, etc. On a desktop box the printf() function occupies a few k of memory - you don't even notice a chunk of memory that small. On the ATmega8, however, that "few k" is 50% of your available program space.

Of course, there are also things you can do with the Arduino that you can't do with your desktop computer, or that you can do with a US$32 Arduino instead of using a US$500 desktop computer. Different tools for different jobs.


Thanks, but this is still a little confusing, so you are saying that printf is not available - although I have used it with integers?

But floating point is not available either? - but it is listed as an available data type.

I would really like to be able to use sprintf with strings at least. I want to do some base64 encoding in the module to be able to send emails, so some string manipulation will be important!

Is there any documentation of what is and is not available from standard 'C'?

You can do floating point, but the ATmega8 doesn't have any floating point hardware so it is all done in software and that eats up the available 8k program space. For example, I had a program that was under 4k; I added one float variable and performed one multiplication on that variable and the resulting program would not fit on the ATmega8. The size of the code to get the printf() function is around 4k, so if you want to printf() a float variable you need 4k for the printf() library, 4k for the floating point emulation library, and don't forget 1k for the Arduino bootloader (that's the part that keeps you from needing to buy a programmer). That's a total of 9k of code to fit on the 8k ATmega8, and we haven't included any of your code yet.

Technically, yes: floating point variables and floating point printf() are available. Practically, however, their uses are limited.

Work is being done to link against libraries instead of chunks of object code, so future versions of the Arduino environment may be able to make effective use of floats and printf() withing the available limits.

Arduino relies on AVRlib. I'm not sure where the official documentation lives, but you can always go and poke through the code in the directory "tools/avr" under the Arduino install directory and see the source code and man pages.

Again, the standard C language is there, but what's generally referred to as the "standard C library" (or libc on unix) has only portions implemented in Arduino and AVRlib.


AVR Lib can be found here

as suggested in the playground

I’ve also found this other link but it’s not official