Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Pressure Sensor code (formula) help please
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Pressure Sensor code (formula) help please

by mjpcarbon on Sat Mar 21, 2020 4:47 pm

Some pressure sensor code help please. My project is to read pressure via a 5v transducer. Can anyone help with a formula that will convert the reading(I assume 0-5v) to a pressure reading. PSI

mjpcarbon
 
Posts: 435
Joined: Fri Nov 29, 2013 8:57 pm

Re: Pressure Sensor code (formula) help please

by adafruit_support_bill on Sat Mar 21, 2020 4:51 pm

What transducer are you using? The spec sheet should tell you how to map the output voltage to a pressure.

adafruit_support_bill
 
Posts: 78659
Joined: Sat Feb 07, 2009 10:11 am

Re: Pressure Sensor code (formula) help please

by mjpcarbon on Sat Mar 21, 2020 4:56 pm

This is what is given but how do I map it ?

Output: 0.5-4.5V linear voltage output. 0 psi outputs 0.5V, 800 psi outputs 2.5V, 1600 psi outputs 4.5V.

mjpcarbon
 
Posts: 435
Joined: Fri Nov 29, 2013 8:57 pm

Re: Pressure Sensor code (formula) help please

by adafruit_support_bill on Sat Mar 21, 2020 7:25 pm

0 psi outputs 0.5V, . . . 1600 psi outputs 4.5V.


So you have the 2 data points needed for a two-point calibration. Just use the two-point calibration calculation from the guide:

https://learn.adafruit.com/calibrating- ... -do-it-5-3

adafruit_support_bill
 
Posts: 78659
Joined: Sat Feb 07, 2009 10:11 am

Re: Pressure Sensor code (formula) help please

by jim_lee on Sat Mar 21, 2020 7:39 pm

map() is your friend here.

volts = map(analogVal, analogIn1, analogIn2, volts1,volts2); // what volts are we reading? (From Arduino specs.)
psi = map(volts, volts1, volts2, psi1,psi2); // And the pressure that gives us? (From the pressure sensor specs.)

See? No fancy math. Just mappings.

-jim lee

jim_lee
 
Posts: 654
Joined: Thu May 24, 2012 8:24 pm

Re: Pressure Sensor code (formula) help please

by adafruit_support_bill on Sat Mar 21, 2020 7:47 pm

Unfortunately, the Map function only works with integers. The equation in the calibration guide is essentially the same - but works with floating point values too.

adafruit_support_bill
 
Posts: 78659
Joined: Sat Feb 07, 2009 10:11 am

Re: Pressure Sensor code (formula) help please

by jim_lee on Sat Mar 21, 2020 8:38 pm

Yes, that can be a, thing. You could just multiply everything by 100 or 1000 do the math then divid it out. Or..

Use mine : https://github.com/leftCoast/Arduino/wiki/mapper

-jim lee

jim_lee
 
Posts: 654
Joined: Thu May 24, 2012 8:24 pm

Re: Pressure Sensor code (formula) help please

by mjpcarbon on Sat Mar 21, 2020 9:10 pm

Excellent, thanks for the help

mjpcarbon
 
Posts: 435
Joined: Fri Nov 29, 2013 8:57 pm

Re: Pressure Sensor code (formula) help please

by mjpcarbon on Sun Mar 22, 2020 11:35 am

Jim, I don't fully understand the example you provided

volts = map(analogVal, analogIn1, analogIn2, volts1,volts2); // what volts are we reading? (From Arduino specs.)
psi = map(volts, volts1, volts2, psi1,psi2); // And the pressure that gives us? (From the pressure sensor specs.)

I expected only to see high and low ranges in the map, don't quite grasp why there are 3 values in the Volts map and the same in the psi map

I realize I am in over my head here and apologize for not understanding the help you have provided

mjpcarbon
 
Posts: 435
Joined: Fri Nov 29, 2013 8:57 pm

Re: Pressure Sensor code (formula) help please

by adafruit_support_bill on Sun Mar 22, 2020 12:50 pm

volts1 is the minimum output of the sensor - 0.5v in your case
psi1 is the pressure value that corresponds to volts1 - or 800 psi.

volts2 is the maximum output of the sensor - 4.5v.
psi2 is the pressure that corresponds to volts2 - or 1600 psi.

The first parameter 'volts' is the measured value from your sensor.

adafruit_support_bill
 
Posts: 78659
Joined: Sat Feb 07, 2009 10:11 am

Re: Pressure Sensor code (formula) help please

by mjpcarbon on Sun Mar 22, 2020 7:34 pm

This is what I have so far, not quite there yet though. The voltage being reported is accurate however the PSI does go up with the voltage but in 400 increments. Seems as thought the PSI is only following the first digit of the voltage * 400.
[code][
int psi = 0;
int volts = 0;

void setup() {
Serial.begin(9600);
}

void loop() {
int sensorValue = analogRead(A0);
float volts = sensorValue * (5.0 / 1023.0);
//volts = map(volts, 0, 5, .5, 4.5);
psi = map(volts, .5, 4.5, 0, 1600);




Serial.print("PSI = ");
Serial.print(psi);
Serial.print(" Volts = ");
Serial.println(volts);
delay(2000);
}


/code]

mjpcarbon
 
Posts: 435
Joined: Fri Nov 29, 2013 8:57 pm

Re: Pressure Sensor code (formula) help please

by adafruit_support_bill on Sun Mar 22, 2020 8:02 pm

As I mentioned in an earlier post, the Arduino version of map() only works with integer values. You can implement the equation posted above, or try jim_lee's version here: https://github.com/leftCoast/Arduino/wiki/mapper

adafruit_support_bill
 
Posts: 78659
Joined: Sat Feb 07, 2009 10:11 am

Re: Pressure Sensor code (formula) help please

by jim_lee on Sun Mar 22, 2020 10:03 pm

Here's the actual folder that the mapper stuff is in..

https://github.com/leftCoast/Arduino/tree/master/libraries/LC_baseTools

LC_baseTools is full of very handy & delightful tools, but probably just confusing at this point.

You want mapper.h & mapper.cpp. Luckily they predate most everything in there so they depend on nothing and can "live" on their own.

-jim lee

jim_lee
 
Posts: 654
Joined: Thu May 24, 2012 8:24 pm

Please be positive and constructive with your questions and comments.