# More Precision With Numbers

I could do with a little more precision from the output of my analog light sensor. At the moment I am using an integer, but would like to see it as a float e.g. 87.8, etc. I'm not 100% sure if this is possible? I did try changing the type int to float, but all this done was transform e.g. 87 to 87.0

Here is my code:

``````int val = analogRead(light_pin);
val = map(constrain(val, 0, 614), 0, 614, 0, 100);
``````

Its constrained as the maximum output for the sensor is 3v.

analogRead returns integers. You can't change that.

If the sensor maximum output is 3V you could set up to use an external reference on Vref, and supply 3V to Vref. (An obvious way of doing (almost) that is to run a wire from the 3.3V pin on the Arduino).

Then you are using most of the range of the light sensor.

The map() reference page gives you the formula it uses for the command so maybe you could do a floating point version of this.

``````long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
``````

[quote author=Nick Gammon link=topic=181370.msg1344260#msg1344260 date=1375788706] If the sensor maximum output is 3V you could set up to use an external reference on Vref, and supply 3V to Vref. (An obvious way of doing (almost) that is to run a wire from the 3.3V pin on the Arduino). [/quote]

Sadly I'm using the Arduino Pro Mini and theres no Aref :(

I've got one sensor that uses the Aref that outputs voltage between 0 and 3, but I've never quite understood the concept as the Aref is 3.3v. So how exactly does it make it better if you don't mind explaining? I'm sure it will be obvious once explained.

Thanks

I've never quite understood the concept as the Aref is 3.3v. So how exactly does it make it better

If Aref is 5V, you're wasting 2V of your range, if your sensor can never output more than 3V.

is it just a case of waste, or do I actually lose actual precision also?

You have a total of 1023 steps. If you never get above 600 or so, you are throwing away precision.

So basically if I used the Aref I'd have from 0 to 1023 steps and more precision?

If so I may have to change boards from the pro to the nano if it makes a big difference.

KeithRB: You have a total of 1023 steps.

1024.

oops, lost a little bit of presicison there… 8^)

Be sure, however, not to confuse precision with accuracy. They are entirely different things.

Sorry thats what I meant, will the accuracy be diminished drastically running the sensor as I am? If so I may have to consider using another board that supports aref. Other than that is maybe looking into increasing the 3v output to 5v of that's at all applicable with the GA1A12S202 analog light sensor in question.

If so I may have to consider using another board that supports aref.
Or, you could de-solder C1, and wire in your own reference