Analog in discretization

Hi there, For any sensor outputting in the range 0 - 5V can be simply read by analog In pins by:

int sensorValue = analogRead(A0);
    float voltage = sensorValue * (5.0 / 1023.0);

But what if the sensor outputs in the range 0.25 to 4.5V or 1 - 5V?

Will it be better if sensor value is remapped for the given range? Or above mapping of 0-5V is just fine?

Thanks.

Z

  float voltage = sensorValue * (5.0 / 1024.0);

AWOL:   float voltage = sensorValue * (5.0 / 1024.0);

Yes 1024. My bad. Should I use the same factor with any sensor uptutting a different range but below 5V?

I prefer to use either the internal 1.1 V reference, or a precision external reference, and a voltage divider on the inputs.

But yes, you use the same factor.

AWOL: I prefer to use either the internal 1.1 V reference, or a precision external reference, and a voltage divider on the inputs.

Thanks. Do you have an example showing circuit and code?

Z

Unless you are going to display the voltage, you do not need to convert it. Instead of converting the ADC result to a voltage and comparing it against other voltages, convert the voltages to the equivalent ADC counts and compare those to the ADC result.

For example, instead of testing if the sensor voltage is higher than 3V (or something like that), test if the analogRead result is higher than (3*1024)/5. The compiler can do that math up front, so you don't have to waste time pointlessly converting every analogRead into real units.

Jiggy-Ninja: Unless you are going to display the voltage, you do not need to convert it. Instead of converting the ADC result to a voltage and comparing it against other voltages, convert the voltages to the equivalent ADC counts and compare those to the ADC result.

For example, instead of testing if the sensor voltage is higher than 3V (or something like that), test if the analogRead result is higher than (3*1024)/5. The compiler can do that math up front, so you don't have to waste time pointlessly converting every analogRead into real units.

Alright, I see what you mean. Thanks.