Library to wrapper most of the functions on the MPL3115A2 pressure and temperature sensor.

Dependents:   WeatherBalloon4180 WeatherBalloon4180 mbed_rifletool Smart_Watch_4180_Final_Design ... more

MPL3115A2 Precision Altimeter

This class wraps the functions of the MPL3115A2 sensor into a usable class that exposes most functions to your project. Functions that are not exposed can easily be added using the existing functions in this library. Specifically, I did not add any functions to handle FIFO logging or the use of either of the pin interrupts. This should not be too difficult to add if you need those features.

The motivation here was to get a set of support classes together that supported the chip and could be expanded on. With this library you can extract all relevant data from the sensor.

Be sure to download the DATASHEET and the App Note AN4519.

This library was created using the mbed NXP LPC11U24. Pins p27 and p28 were used for the I2C functions. Be sure to install 1K pull-up resistors on both lines. Also, if you're not using the SparkFun breakout board, be sure to use the right caps on the power pin. If you don't, the jitter can cause problems.

This library was inspired by the similar library available for the Arduino written by Nathan Seidle at SparkFun. I copied some of the number crunching routines and tried to follow his style of coding for the library. That way users of Arduinos could step into this library a little easier.

Below is some sample code that outputs the sensor data to a serial debug terminal window. If you have a logic analyzer, like the Saleae Logic, then you might not need the debug terminal window. If you still need the serial driver for your mbed, you can get it here.

Sample Code

#include "mbed.h"
#include "MPL3115A2.h"

I2C i2c(p28, p27);       // sda, scl

// Comment out all of the references to 'pc' on this page if you don't have the 
// serial debug driver for your mbed board installed on your computer. If you do,
// I personally like to use Putty as the terminal window to capture debug messages.
Serial pc(USBTX, USBRX); // tx, rx

// Again, remove the '&pc' parameter is you're not debugging.
MPL3115A2 sensor(&i2c, &pc);

DigitalOut myled(LED1);     // Sanity check to make sure the program is working.
DigitalOut powerPin(p21);   // <-- I powered the sensor from a pin. You don't have to.

int main() {
    powerPin = 1;

    pc.printf("** MPL3115A2 SENSOR **\r\n");


    pc.printf("Who Am I: 0x%X\r\n", sensor.whoAmI());

    Altitude a;
    Temperature t;
    Pressure p;
    // Offsets for Dacula, GA
        pc.printf("Altitude: %sft, Temp: %sºF, Pressure: %sPa\r\n", a.print(), t.print(), p.print());
        pc.printf("OFF_H: 0x%X, OFF_T: 0x%X, OFF_P: 0x%X\r\n", sensor.offsetAltitude(), sensor.offsetTemperature(), sensor.offsetPressure());
        myled = 1;
        myled = 0;

Wed Apr 02 12:59:44 2014 +0000
Updated documentation.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sophtware 0:beb43bc3d6d4 1 /*
sophtware 0:beb43bc3d6d4 2 MPL3115A2 Barometric Pressure and Tempurature Sensor Library
sophtware 0:beb43bc3d6d4 3 By: Michael Lange
sophtware 0:beb43bc3d6d4 4 Date: March 31, 2014
sophtware 0:beb43bc3d6d4 5 License: This code is public domain.
sophtware 0:beb43bc3d6d4 6
sophtware 0:beb43bc3d6d4 7 This class encapsulates a pressure reading from the sensor.
sophtware 0:beb43bc3d6d4 8
sophtware 0:beb43bc3d6d4 9 */
sophtware 0:beb43bc3d6d4 10
sophtware 0:beb43bc3d6d4 11
sophtware 0:beb43bc3d6d4 12 #ifndef PRESSURE_H
sophtware 0:beb43bc3d6d4 13 #define PRESSURE_H
sophtware 0:beb43bc3d6d4 14
sophtware 0:beb43bc3d6d4 15 #include "mbed.h"
sophtware 0:beb43bc3d6d4 16
sophtware 3:7c7c1ea6fc33 17 //! Pressure provides a wrapper around barometric data coming from the sensor. The class handles
sophtware 3:7c7c1ea6fc33 18 //! working with compressed data from the sensor and provides convenient functions for retreiving
sophtware 3:7c7c1ea6fc33 19 //! the data in various units (with room to add more if needed).
sophtware 0:beb43bc3d6d4 20 class Pressure
sophtware 0:beb43bc3d6d4 21 {
sophtware 0:beb43bc3d6d4 22 public:
sophtware 0:beb43bc3d6d4 23
sophtware 3:7c7c1ea6fc33 24 //! The size of the compressed data buffer from the sensor. Used in an I2C read.
sophtware 0:beb43bc3d6d4 25 static const int size = 3;
sophtware 3:7c7c1ea6fc33 26
sophtware 3:7c7c1ea6fc33 27 //! The units we support converting the sensor data to.
sophtware 0:beb43bc3d6d4 28 enum unitsType { PASCALS, PSI, INHG, MMHG };
sophtware 0:beb43bc3d6d4 29
sophtware 0:beb43bc3d6d4 30 Pressure();
sophtware 0:beb43bc3d6d4 31 Pressure(float a, unitsType units = PASCALS);
sophtware 0:beb43bc3d6d4 32 Pressure(const char* compressed);
sophtware 0:beb43bc3d6d4 33 Pressure(const char msb, const char csb, const char lsb);
sophtware 0:beb43bc3d6d4 34
sophtware 3:7c7c1ea6fc33 35 //! Allows using the object directly in an I2C read operation.
sophtware 0:beb43bc3d6d4 36 operator char*(void) { return _compressed; }
sophtware 3:7c7c1ea6fc33 37 //! Same as calling pressure with PASCALS as the parameter.
sophtware 0:beb43bc3d6d4 38 operator float(void) { return _pressure; }
sophtware 0:beb43bc3d6d4 39
sophtware 0:beb43bc3d6d4 40 float pressure(unitsType units = PASCALS);
sophtware 3:7c7c1ea6fc33 41 //! Call to decompress the sensor data after an I2C read.
sophtware 0:beb43bc3d6d4 42 void setPressure();
sophtware 0:beb43bc3d6d4 43 void setPressure(const char* compressed);
sophtware 0:beb43bc3d6d4 44 void setPressure(const char msb, const char csb, const char lsb);
sophtware 0:beb43bc3d6d4 45 void setPressure(float a, unitsType units = PASCALS);
sophtware 0:beb43bc3d6d4 46
sophtware 3:7c7c1ea6fc33 47 //! Returns the pressure as a string in the units specified, defaulting to PASCAL if none specified.
sophtware 0:beb43bc3d6d4 48 const char* print(unitsType units = PASCALS);
sophtware 0:beb43bc3d6d4 49
sophtware 0:beb43bc3d6d4 50 private:
sophtware 0:beb43bc3d6d4 51 float _pressure;
sophtware 0:beb43bc3d6d4 52 char _compressed[3];
sophtware 0:beb43bc3d6d4 53 char _printBuffer[9];
sophtware 0:beb43bc3d6d4 54 };
sophtware 0:beb43bc3d6d4 55
sophtware 0:beb43bc3d6d4 56 #endif // PRESSURE_H