inf nan errors reading sensor data


Ive building up a block of code based on others work, to read a pressure sensor and apply a basic Kalman filter. So far so good. All works nicely ( although I’d love to find the tuning guide for the Kalman variables!).

To improve accuracy i want to take an average of n initial pressure readings, which is then used later on in the code to calculate altitude. Eventually this code will be use to trigger states based on altitude.

My question is, once i include the averaging code, it all collapses. see Lines 42-58. It looks like a math problem, but not sure what or how to fix it. All help greatly appreciated!

// this works- with 1D Kalman filter!

  MS5611 Barometric Pressure & Temperature Sensor. Simple Example
  Read more:
  (c) 2014 by Korneliusz Jarzebski

  Kalman from:

#include <Wire.h>
#include <MS5611.h>
#include <Kalman.h>

float filteredAltitude;
// initial values Kalman myfilter(1.025,32,1023,0);
Kalman myFilter(1.0125,20,1023,0); //suggested initial values for high noise filtering

MS5611 ms5611;

double referencePressure;
unsigned long prevMillis;
unsigned long currentMillis;
const int pause = 10;

void setup() 

  // Initialize MS5611 sensor
  Serial.println("Initialize MS5611 Sensor");

    Serial.println("Could not find a valid MS5611 sensor, check wiring!");

  // Get reference pressure for relative altitude

  // this bit added to average n readings for initial pressure reference reading
   float AveragePressure = 0;
    const int NumMeasurements = 10 ;
  for(int i = 0; i < NumMeasurements ; ++i)
    AveragePressure += ms5611.readPressure();
    delay(1); // this needs to be removed for delay free code!
  //AverageTemperature /= MeasurementsToAverage;  old code- example was temp
  referencePressure /= NumMeasurements;

  // origional code took one reading only:
  //referencePressure = ms5611.readPressure();
  // Check settings

void checkSettings()
  Serial.print("Oversampling: ");

void loop()
  currentMillis= millis();

  if (currentMillis-prevMillis >= pause) {
  // Read raw values
  uint32_t rawTemp = ms5611.readRawTemperature();
  uint32_t rawPressure = ms5611.readRawPressure();

  // Read true temperature & Pressure
  double realTemperature = ms5611.readTemperature();
  long realPressure = ms5611.readPressure();

  // Calculate altitude
  float absoluteAltitude = ms5611.getAltitude(realPressure);
  float relativeAltitude = ms5611.getAltitude(realPressure, referencePressure);

  filteredAltitude = myFilter.getFilteredValue(relativeAltitude);


  //Serial.print(" rawTemp = ");
  //Serial.print(", realTemp = ");
  //Serial.println(" *C");

  //Serial.print(" rawPressure = ");
  //Serial.print(", realPressure = ");
  //Serial.println(" Pa");

  //Serial.print(" absoluteAltitude = ");
  //Serial.print(" m, relativeAltitude = ");
  //Serial.println(" m");
  //Serial.print( "m, filtered relative alt");

  Serial.print(" ");

Instead of just “it all collapses”, can you please tell us what is actually wrong? What actually happens that shouldn’t or what doesn’t happen that should happen?

withthe averaging code included, the serial monitor reports:
Oversampling: 6
inf inf
inf nan
inf nan
inf nan
inf nan
inf nan
inf nan
inf nan
inf nan
inf nan
inf nan
inf nan
inf nan
inf nan
inf nan
inf nan
inf nan

Without the averaging code and just readign a single value, everything works fine

You forgot to initialize the referencePressure variable.


  referencePressure /= NumMeasurements;

I think this was meant to be

 referencePressure = AveragePressure / NumMeasurements;

I see my folleys-you are correct!

I could have also set my variable names better if i wanted to use /=…

Thanks heaps!