# MPX5100: taking longer for the last linear change in level measured?

Question asked by Max G on Mar 11, 2017
Latest reply on Mar 14, 2017 by Dario Arias

I am using a MPX5100P to measure a water tank level.

Based on the tank dimensions it takes some 5 minutes for 1% of the tank being filled.
However, the last percent (from 99-100) usually takes double the time, and sometimes it takes 15 minutes, by which the tank over flows (eventually reacing 100% and the pump switching off).

where would I have to look to troubleshoot this issue?
Any hints appreciated...

void measureWaterLevel() {
// measure water level by taking 64 samples and averaging them

unsigned int pressureSensorSamples = 0;         // holds the accummulated / added-up analog read values
float pressureSensorAvgValue       = 0;         // averaged sensor value
float pressureSensorAvgValueOld    = 0;         // previous averaged sensor value
const float VOLTAGE_IN = 5.18;                  // Vin; needs to be measured with scope once the circuitry is built

for (int i = 0; i < 64; i++) {                  // sample 64 analogue values for averaging
}

// get the average value by dividing by the number of samples
pressureSensorAvgValue = pressureSensorSamples / 64.0;

// if the pressure value has changed calculate values
if (pressureSensorAvgValue != pressureSensorAvgValueOld) {
pressureSensorAvgValueOld = pressureSensorAvgValue;

// 5.0V for the ratio-metric sensor sensitivity
float sensorVoltage = pressureSensorAvgValue / 1024.0 * VOLTAGE_IN;

// a reading of zero kPa = 36 steps or 180mV -- either zero out or subtract from sensor value
// test sensor 0.185
const float SENSOR_ZERO_CORRECTION = 5.0 / 1024 * 36;  // 0.0048828125 * 36 = 0.17578125

if (sensorVoltage < SENSOR_ZERO_CORRECTION) {
sensorVoltage = 0;
}
else {
sensorVoltage -= SENSOR_ZERO_CORRECTION;
}

// kPa = sensor V divided by voltage span, multiplied by sensor range
float kPa                 = sensorVoltage / 4.5 * 100.0;
float tmpLevelPercent     = kPa / 17.5 * 100.0;        // 17.5 == 100%
tankLevelPercent          = (byte)tmpLevelPercent;

#ifdef DEBUG
unsigned long levelVolume = TANK_VOLUME * tmpLevelPercent / 100;

Serial.print(F("sum pressureSensorValues ....: "));
Serial.println(pressureSensorSamples);
Serial.print(F("pressureSensorAvgValue / 64 .: "));
Serial.println(pressureSensorAvgValue, 4);
Serial.print(F("SENSOR_ZERO_CORRECTION ......: "));
Serial.println(SENSOR_ZERO_CORRECTION, 4);
Serial.print(F("tmpLevelPercent .............: "));
Serial.println(tmpLevelPercent);
Serial.print(F("TANK_VOLUME_MAX .............: "));
Serial.println(TANK_VOLUME_MAX);

Serial.print(F("levelVolume .................: "));
Serial.println(levelVolume);

Serial.print(F("Sum(A0)/64 | Vs | kPa | % | l: "));
Serial.print(pressureSensorAvgValue, 4);
Serial.print(" | ");
Serial.print(sensorVoltage, 5);
Serial.print(" | ");
Serial.print(kPa);
Serial.print(" | ");
Serial.print(tankLevelPercent);
Serial.print(" | ");
Serial.println(levelVolume);
#endif
}
}