Go Down

Topic: MPL3115A2 Pressure Sensor Lag (Read 508 times) previous topic - next topic

filbertwatson

I have a simple Arduino Mega 2560 and MPL3115A2 sensor setup. I am using the example sketch provided by the Adafruit people. I have the Vin, Gnd, Scl, and Sda pins all wired to the correct places. I notice that in the example sketch it reads the altitude, pressure, and temperature. I recall hearing that the sensor has an altitude mode and a pressure mode, it laggs in between the two, it outputs pressure, waits about a second, then outputs altitude and temperature. Is this caused by it switch modes? I would like all 3 data values but I can make do with just altitude and temperature if need be. I am just wondering why it is lagging, thanks.

756E6C

#1
Mar 07, 2017, 09:43 am Last Edit: Mar 07, 2017, 09:50 am by 756E6C
Well, looking at the conversion formula on page 5 of the datasheet, I can see it might take some time.
http://cache.freescale.com/files/sensors/doc/data_sheet/MPL3115A2.pdf

Try putting some debug prints in the code to see how many millis it takes when switching modes.

Code: [Select]

/*!
/**************************************************************************/
    @file     Adafruit_MPL3115A2.cpp
    @author   K.Townsend (Adafruit Industries)
    @license  BSD (see license.txt)

    Example for the MPL3115A2 barometric pressure sensor

    This is a library for the Adafruit MPL3115A2 breakout
    ----> https://www.adafruit.com/products/1893

    Adafruit invests time and resources providing this open source code,
    please support Adafruit and open-source hardware by purchasing
    products from Adafruit!

    @section  HISTORY

    v1.0 - First release
*/
/**************************************************************************/

#include <Wire.h>
#include <Adafruit_MPL3115A2.h>
uint32_t timerStart;

// Power by connecting Vin to 3-5V, GND to GND
// Uses I2C - connect SCL to the SCL pin, SDA to SDA pin
// See the Wire tutorial for pinouts for each Arduino
// http://arduino.cc/en/reference/wire
Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();

void setup() {
  Serial.begin(9600);
  Serial.println("Adafruit_MPL3115A2 test!");
}

void loop() {
  if (! baro.begin()) {
    Serial.println("Couldnt find sensor");
    return;
  }
  timerStart = millis();
  float pascals = baro.getPressure();
  // Our weather page presents pressure in Inches (Hg)
  // Use http://www.onlineconversion.com/pressure.htm for other units
  Serial.println(millis() - timerStart;
  Serial.print(pascals/3377); Serial.println(" Inches (Hg)");
 
  timerStart = millis();
  float altm = baro.getAltitude();
  Serial.println(millis() - timerStart;
  Serial.print(altm); Serial.println(" meters");

  timerStart = millis();
  float tempC = baro.getTemperature();
  Serial.println(millis() - timerStart;
  Serial.print(tempC); Serial.println("*C");

  delay(250);
}

knut_ny

rearrange. Read all values - then print (removed the timerelated sentences)
Ny

filbertwatson

Okay so I finally got home and tested it with that code, but printing it all at the end of the loop. The FIRST time the Arduino writes to serial the output is 11 (pressure), 231 (altitude), and 11 (temperature). After the first time it goes a steady average of 983 (pressure), 1000 (altitude), 11 (temperature). It seems to be the problem with switching modes is causing all the lag, considering the first output. Why would this be do you think? Can it be fixed? I need this to be as fast as possible.

knut_ny

#4
Mar 08, 2017, 08:53 am Last Edit: Mar 08, 2017, 09:32 am by knut_ny
There is no "modes". (ref datasheet).
Just throw the first reading..
Ny

filbertwatson

Hm. Well I guess that makes a whole lot of sense, I guess I don't remember but one sensor I had switched between altitude and pressuse modes... sorry. It still takes 1 second to log the altitude though, which is still way slower than I need it to be. There are two interrupt pins on the breakout board, if I connect those to something can I make it faster by making it interrupt driven?

knut_ny

#6
Mar 08, 2017, 05:36 pm Last Edit: Mar 08, 2017, 05:59 pm by knut_ny
store pressure only.  The altitide can be calculated 'anytime' when needed.

For low altitudes (to 1000m)  U can use a linear formula for the convesion
(the barometric formula is more math.. and slower)

H = 8716 - 8.6327*(P1/P0);    is within 3m for heights 0..700m
Ny

filbertwatson

That is not the problem, storing the altitude and storing the pressure take about the same time, 1 second.

filbertwatson

Alright! Figured it out, wrote my own library for it, the pressure and altitude after they were read had 1 seconds delays each, I feel dumb. I'm sorry for all you your time although I have learnt a lot from this, and thank you.

davidwetzel

Filbertwatson,

How did you modify the library to speed up the sensor?  I took a look at the Adafruit_MPL3115A2.cpp library file and found 3 instances where delay(10) was used in the code.  Unfortunately, removing the delay statements does not speed up the sensor when getting temp, pressure and altitude.  It still takes 970 milliseconds for each reading even though I have removed all three delay statements. 

How did you fix the library to speed things up?

-David

Go Up