Using an MPXV5050DP

Hi All - I'm trying to use a differential pressure sensor (MPXV5050DP) to measure water levels in a water tank. I've wired it up to A0 on a MEGA, and using the code below. Both ports are open, so I'm expecting the a value of 0, but getting back 51-52. Is this normal? Do I just account for this as the offset in the code? It seems high to me. I've wired GND to A0 and get a consistent 0 value, and also 5V to A0 and get 1023 which is expected. Should I just read the sensorValue and remove 51 as the offset?

Any advice would be greatly appreciated :slight_smile:

Output:

sensorValue: 52.0
voltage: 0.3
kpa: 0.6

Code:

// Pin 1 (notch) - Analog output
// Pin 2 - Gnd
// Pin 3 - 5V


int sensorPin = A0;    // Select input pin for the potentiometer
// int sensorValue = 0;   // Variable stores value coming from the sensor
double sensorValue = 0;
double voltage = 0;
double kpa = 0;

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

void loop() {
//  sensorValue = analogRead(sensorPin);  // Read sensor
  sensorValue = (double) analogRead(sensorPin);  
  Serial.print("sensorValue: ");
  Serial.println(sensorValue, 1);     // Display result
  
  voltage = sensorValue * (5.0 / 1023.0);
  Serial.print("voltage: ");
  Serial.println(voltage, 1);     // Display result
  
  kpa=((voltage/5.0)-0.04)/0.018;
  Serial.print("kpa: ");
  Serial.println(kpa, 1);     // Display result

  Serial.println("");
  delay(1000);         // Wait 400 milliseconds
}

The 'double' is actually a 'float'. So you can use 'float' in your sketch.
http://arduino.cc/en/Reference/Double

The unused pins of the sensor should not be connected. I made that mistake myself with such an sensor by soldering them to ground. But they should be left open.

As you can see in the datasheet, the pressure range is 50kPa, and the output range is 4.5V (with 5V power).
With 0 kPa differential pressure, the typical output is 0.2V.
That 0.2V results in a value for analogRead() of a value of 40.
Your offset of 51 is well within the specifications.

So you have to adjust your formula for offset and range.
Calculate the voltage with the range of 5.0V.
Calculate the pressure with the range of 4.5V and with the offset.

Awesome, thanks for the feedback Caltoa! I've made some updates to the code based on your feedback and added some notes for reference. I've included some outputs below, but looks like we are heading in the right direction :slight_smile:

Code

// NOTES
// 50kPa = 5.10 mH20 max water column depth (http://www.convert-measurement-units.com/conversion-calculator.php?type=pressure)
// If max v of 4.5v = 50kPa, then sensorValue of 921 (920.7) = 50kPa

int sensorPin = A0;
int sensorValue = 0, sensorMax = 1023, sensorOffset = 52;
float voltage = 0, kpa = 0, voltageMax = 5.0, kpaRangeTopVoltage = 4.5;

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

void loop() {
  sensorValue = analogRead(sensorPin) - sensorOffset;  // Read sensor & adjust with offset
  Serial.print("sensorValue (offset ");
  Serial.print(sensorOffset, DEC);
  Serial.print("): ");
  Serial.println(sensorValue, DEC);
    
  if (sensorValue == 0) {
    kpa = 0;
    voltage = 0;
  } else {
    voltage = sensorValue * (voltageMax / sensorMax);
    kpa=((voltage/kpaRangeTopVoltage)-0.04)/0.018;
  }

  Serial.print("voltage: ");
  Serial.println(voltage, 3);

  Serial.print("kpa: ");
  Serial.println(kpa, 1);

  Serial.println("");
  delay(1000); 
}

Output example as I blow into one port:

sensorValue (offset 52): 0
voltage: 0.000
kpa: 0.0

sensorValue (offset 52): 101
voltage: 0.494
kpa: 3.9

sensorValue (offset 52): 101
voltage: 0.494
kpa: 3.9

sensorValue (offset 52): 112
voltage: 0.547
kpa: 4.5

sensorValue (offset 52): 181
voltage: 0.885
kpa: 8.7