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;
    wait_ms(300);

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

    sensor.init();

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

    Altitude a;
    Temperature t;
    Pressure p;
    
    // Offsets for Dacula, GA
    sensor.setOffsetAltitude(83);
    sensor.setOffsetTemperature(20);
    sensor.setOffsetPressure(-32);
    
    while(1) 
    {
        sensor.readAltitude(&a);
        sensor.readTemperature(&t);
        
        sensor.setModeStandby();
        sensor.setModeBarometer();
        sensor.setModeActive();
        sensor.readPressure(&p);
        
        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;
        wait(5);
        myled = 0;
        wait(5);

        sensor.setModeStandby();
        sensor.setModeAltimeter();
        sensor.setModeActive();
    }
}
Committer:
sophtware
Date:
Wed Apr 02 12:59:44 2014 +0000
Revision:
3:7c7c1ea6fc33
Parent:
0:beb43bc3d6d4
Updated documentation.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sophtware 0:beb43bc3d6d4 1 #include "Pressure.h"
sophtware 0:beb43bc3d6d4 2 #include "mbed.h"
sophtware 0:beb43bc3d6d4 3
sophtware 0:beb43bc3d6d4 4 Pressure::Pressure()
sophtware 0:beb43bc3d6d4 5 {
sophtware 0:beb43bc3d6d4 6 _pressure = 0.0;
sophtware 0:beb43bc3d6d4 7
sophtware 0:beb43bc3d6d4 8 _compressed[0] = 0;
sophtware 0:beb43bc3d6d4 9 _compressed[1] = 0;
sophtware 0:beb43bc3d6d4 10 _compressed[2] = 0;
sophtware 0:beb43bc3d6d4 11 }
sophtware 0:beb43bc3d6d4 12
sophtware 0:beb43bc3d6d4 13 Pressure::Pressure(float a, unitsType units)
sophtware 0:beb43bc3d6d4 14 {
sophtware 0:beb43bc3d6d4 15 setPressure(a, units);
sophtware 0:beb43bc3d6d4 16 }
sophtware 0:beb43bc3d6d4 17
sophtware 0:beb43bc3d6d4 18 Pressure::Pressure(const char* compressed)
sophtware 0:beb43bc3d6d4 19 {
sophtware 0:beb43bc3d6d4 20 setPressure(compressed);
sophtware 0:beb43bc3d6d4 21 }
sophtware 0:beb43bc3d6d4 22
sophtware 0:beb43bc3d6d4 23 Pressure::Pressure(const char msb, const char csb, const char lsb)
sophtware 0:beb43bc3d6d4 24 {
sophtware 0:beb43bc3d6d4 25 setPressure(msb, csb, lsb);
sophtware 0:beb43bc3d6d4 26 }
sophtware 0:beb43bc3d6d4 27
sophtware 0:beb43bc3d6d4 28 void Pressure::setPressure()
sophtware 0:beb43bc3d6d4 29 {
sophtware 0:beb43bc3d6d4 30 setPressure(_compressed[0], _compressed[1], _compressed[2]);
sophtware 0:beb43bc3d6d4 31 }
sophtware 0:beb43bc3d6d4 32
sophtware 0:beb43bc3d6d4 33 void Pressure::setPressure(float a, unitsType units)
sophtware 0:beb43bc3d6d4 34 {
sophtware 0:beb43bc3d6d4 35 // TODO:
sophtware 0:beb43bc3d6d4 36 }
sophtware 0:beb43bc3d6d4 37
sophtware 0:beb43bc3d6d4 38 void Pressure::setPressure(const char* compressed)
sophtware 0:beb43bc3d6d4 39 {
sophtware 0:beb43bc3d6d4 40 setPressure(compressed[0], compressed[1], compressed[2]);
sophtware 0:beb43bc3d6d4 41 }
sophtware 0:beb43bc3d6d4 42
sophtware 0:beb43bc3d6d4 43 void Pressure::setPressure(const char msb, const char csb, const char lsb)
sophtware 0:beb43bc3d6d4 44 {
sophtware 0:beb43bc3d6d4 45 long pressure_whole = (long)msb<<16 | (long)csb<<8 | (long)lsb;
sophtware 0:beb43bc3d6d4 46 pressure_whole >>= 6; // Pressure is an 18 bit number with 2 bits of decimal. Get rid of decimal portion.
sophtware 0:beb43bc3d6d4 47
sophtware 0:beb43bc3d6d4 48 float pressure_decimal = (float)((lsb&0x30)>>4)/4.0; // Turn it into fraction
sophtware 0:beb43bc3d6d4 49
sophtware 0:beb43bc3d6d4 50 _pressure = (float)pressure_whole + pressure_decimal;
sophtware 0:beb43bc3d6d4 51 }
sophtware 0:beb43bc3d6d4 52
sophtware 0:beb43bc3d6d4 53 float Pressure::pressure(unitsType units)
sophtware 0:beb43bc3d6d4 54 {
sophtware 0:beb43bc3d6d4 55 // http://www.asknumbers.com/
sophtware 0:beb43bc3d6d4 56 switch (units)
sophtware 0:beb43bc3d6d4 57 {
sophtware 0:beb43bc3d6d4 58 case PSI:
sophtware 0:beb43bc3d6d4 59 return _pressure * 0.000145037738;
sophtware 0:beb43bc3d6d4 60 case INHG:
sophtware 0:beb43bc3d6d4 61 return _pressure * 0.00029529983071;
sophtware 0:beb43bc3d6d4 62 case MMHG:
sophtware 0:beb43bc3d6d4 63 return _pressure * 0.007500615613;
sophtware 0:beb43bc3d6d4 64 }
sophtware 0:beb43bc3d6d4 65
sophtware 0:beb43bc3d6d4 66 return _pressure;
sophtware 0:beb43bc3d6d4 67 }
sophtware 0:beb43bc3d6d4 68
sophtware 0:beb43bc3d6d4 69 const char* Pressure::print(unitsType units)
sophtware 0:beb43bc3d6d4 70 {
sophtware 0:beb43bc3d6d4 71 sprintf(_printBuffer, "%.0f", pressure(units));
sophtware 0:beb43bc3d6d4 72 return _printBuffer;
sophtware 0:beb43bc3d6d4 73 }