 # Sensor input conversion

Hello :)

I have a simple question but I'm fairly new with programming.

I need to convert the input from a pressure sensor to a physical value. But the sensor output is linear between 0.5V and 4.5V, not 0-5V. 0 PSI = 0.5 volt 100 PSI = 4.5V

It's confused me on how to convert it to eliminate the 0.5V gap at each end.

I guess it's something very common but I can't find any example.

You won’t find an example, but you should be able to figure it from a little geometry.

Slope of psi vs volts graph = (100-0) / (4.5 -0.5) = 100/4 = 25

Y-intercept = -12.5

so PSI = 25V - 12.5

where V is from the analogRead:

so PSI = (25x (5/1023) x analogRead) -12.5

25x5/1023 = 0.1222

so PSI = 0.1222analogRead - 12.5

Let's say that Vcc is exactly 5V and that you are using it for Aref. 10 bits is 0 to 1023.

0.5V = 1023*0.5/5 = 102.3 4.5V = 1023*4.5/5 = 920.7 The range is therefore 920.7 - 102.3 = 818.4 rounded to 818

So... 100psi full scale times the raw reading minus the 0psi reading divided by the full scale 818 present with 100psi. psi = 100(raw-102)/818

At 0psi, 0.5V therefore 102 raw 100(102 - 102)/818 = 0psi 50psi, 2.5V therefore 511 100(511 - 102)/818 = 50psi 75psi, 3.5V therefore 716 100(716 - 102)/818 = 75.06psi

You could simplify the equation but then it would no longer be integer math. 0.122249(raw - 102)

I like to calculate the voltage first, that makes it possible to check it with a voltmeter.
After that, adjust the range and offset for the psi.

It is possible to make calculations and put that in code. I think, that the compiler should do the calculations, and the code should make visible what is going on.

I’m using floating point for the calculation.

``````// raw = is the raw ADC value

// voltage is the voltage at the pin
// assuming the reference is default (5V).
float voltage = (float) raw / 1023.0 * 5.0;

// psi is the pressure 0...100 for 0.5...4.5V
// substract the offset, and adjust the range.
// The range is the resulting range divided by the original range.
float psi = (voltage - 0.5) * (100.0) / (4.5 - 0.5);
``````

When you want to do something really ugly, you can also do this: int pressure = (analogRead(A0) - 102) * 5 / 41;

that's great :D

Many thanks to all, it's a full reply !

M4vrick: that's great :D

Many thanks to all, it's a full reply !

Three different looking ones... but all essentially the same.