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.
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
int raw = analogRead(A0);
// 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;