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();
    }
}

Files at this revision

API Documentation at this revision

Comitter:
sophtware
Date:
Wed Apr 02 12:59:44 2014 +0000
Parent:
2:2ebc9c0d4a54
Commit message:
Updated documentation.

Changed in this revision

Altitude.h Show annotated file Show diff for this revision Revisions of this file
MPL3115A2.h Show annotated file Show diff for this revision Revisions of this file
Pressure.h Show annotated file Show diff for this revision Revisions of this file
Temperature.h Show annotated file Show diff for this revision Revisions of this file
--- a/Altitude.h	Wed Apr 02 12:22:45 2014 +0000
+++ b/Altitude.h	Wed Apr 02 12:59:44 2014 +0000
@@ -14,8 +14,8 @@
 
 #include "mbed.h"
 
-//! Casting truncates, therefore negative numbers become positive.
-//! This will only cast properly in the range -128 to 127.
+//! Casting a float to a char truncates, therefore negative numbers become positive.
+//! This will properly cast a float in the range -128 to 127 to a char.
 #define float_to_char(x) (((x)<0)?(-(char)(x)):((char)(x)))
 
 //! Altitude provides a wrapper around altitude data coming from the sensor. The class handles
--- a/MPL3115A2.h	Wed Apr 02 12:22:45 2014 +0000
+++ b/MPL3115A2.h	Wed Apr 02 12:59:44 2014 +0000
@@ -105,19 +105,32 @@
 {
 public:
     //! Constructs an MPL3115A2 object and associates an I2C and optional Serial debug object.
+    //! @param *i2c The I2C object to use for the sensor.
+    //! @param *pc An optional serial debug connection object.
     MPL3115A2(I2C *i2c, Serial *pc = NULL);
 
-    //! Call from main to initialize the sensor, defaulting to Altitude mode.
+    //! Initializes the sensor, defaulting to Altitude mode. This should be called before using
+    //! the sensor for the first time.
     void init();
 
-    //! Returns the fixed device ID number (usually equal to 0xC4).
+    //! Queries the value from the WHO_AM_I register (usually equal to 0xC4).
+    //! @return The fixed device ID from the sensor.
     char  whoAmI() { return i2cRead(WHO_AM_I); } 
     
-    //! Returns the passed in altitude object with altitude data.
+    //! Reads Altitude data from the sensor and returns it in the Altitude object passed in. If
+    //! no data could be read, the Altitude object is left as is.
+    //! @param a A pointer to an Altitude object that will receive the sensor data.
+    //! @returns The Altitude pointer that was passed in.
     Altitude* readAltitude(Altitude* a);            
-    //! Returns the passed in pressure object with barometric pressure data.
+    //! Reads Pressure data from the sensor and returns it in the Pressure object passed in. If
+    //! no data could be read, the Pressure object is left as is.
+    //! @param a A pointer to a Pressure object that will receive the sensor data.
+    //! @returns The Pressure pointer that was passed in.
     Pressure* readPressure(Pressure* p);            
-    //! Returns the passed in temperature object with temperature data.
+    //! Reads Temperature data from the sensor and returns it in the Temperature object passed in. If
+    //! no data could be read, the Temperature object is left as is.
+    //! @param a A pointer to an Temperature object that will receive the sensor data.
+    //! @returns The Temperature pointer that was passed in.
     Temperature* readTemperature(Temperature* t);
     
     // Use these methods to set the sensor's offsets to increase its accuracy. You can generally
@@ -126,6 +139,7 @@
     // pressure from the web for your area is generally not close enough to help with calibration.
     // You may need to play with the setting to achieve good accuracy. I found the offset steps
     // were not 100% accurate to the datasheet and had to adjust accordingly. 
+    
     //! Returns the altitude offset stored in the sensor.
     char offsetAltitude() { return i2cRead(OFF_H); }
     //! Sets the altitude offset stored in the sensor. The allowed offset range is from -128 to 127 meters.
--- a/Pressure.h	Wed Apr 02 12:22:45 2014 +0000
+++ b/Pressure.h	Wed Apr 02 12:59:44 2014 +0000
@@ -14,11 +14,17 @@
 
 #include "mbed.h"
 
+//! Pressure provides a wrapper around barometric data coming from the sensor. The class handles
+//! working with compressed data from the sensor and provides convenient functions for retreiving
+//! the data in various units (with room to add more if needed).
 class Pressure
 {
 public:
 
+    //! The size of the compressed data buffer from the sensor. Used in an I2C read.
     static const int size = 3;
+
+    //! The units we support converting the sensor data to.
     enum unitsType { PASCALS, PSI, INHG, MMHG };
 
     Pressure();
@@ -26,15 +32,19 @@
     Pressure(const char* compressed);
     Pressure(const char msb, const char csb, const char lsb);
 
+    //! Allows using the object directly in an I2C read operation.
     operator char*(void) { return _compressed; }
+    //! Same as calling pressure with PASCALS as the parameter.
     operator float(void) { return _pressure; }
 
     float pressure(unitsType units = PASCALS);
+    //! Call to decompress the sensor data after an I2C read.
     void setPressure();
     void setPressure(const char* compressed);
     void setPressure(const char msb, const char csb, const char lsb);
     void setPressure(float a, unitsType units = PASCALS);
 
+    //! Returns the pressure as a string in the units specified, defaulting to PASCAL if none specified.
     const char* print(unitsType units = PASCALS);
     
 private:
--- a/Temperature.h	Wed Apr 02 12:22:45 2014 +0000
+++ b/Temperature.h	Wed Apr 02 12:59:44 2014 +0000
@@ -14,11 +14,17 @@
 
 #include "mbed.h"
 
+//! Temperature provides a wrapper around temperature data coming from the sensor. The class handles
+//! working with compressed data from the sensor and provides convenient functions for retreiving
+//! the data in various units (with room to add more if needed).
 class Temperature
 {
 public:
 
+    //! The size of the compressed data buffer from the sensor. Used in an I2C read.
     static const int size = 2;
+
+    //! The units we support converting the sensor data to.
     enum unitsType { CELSIUS, FAHRENHEIT, KELVIN };
 
     Temperature();
@@ -26,15 +32,19 @@
     Temperature(const char* compressed);
     Temperature(const char msb, const char lsb);
 
+    //! Allows using the object directly in an I2C read operation.
     operator char*(void) { return _compressed; }
+    //! Same as calling temperature with FAHRENHEIT as the parameter.
     operator float(void) { return _temperature; }
 
     float temperature(unitsType units = FAHRENHEIT);
+    //! Call to decompress the sensor data after an I2C read.
     void setTemperature();
     void setTemperature(const char* compressed);
     void setTemperature(const char msb, const char lsb);
     void setTemperature(float a, unitsType units = FAHRENHEIT);
 
+    //! Returns the temperature as a string in the units specified, defaulting to FAHRENHEIT if none specified.
     const char* print(unitsType units = FAHRENHEIT);
     
 private: