MPL3115A2
I2C Precision Altimeter¶
The MPL3115A2 employs a MEMS pressure sensor with an I2C interface to provide accurate Pressure/Altitude and Temperature data. The sensor outputs are digitized by a high resolution 24-bit ADC. Internal processing removes compensation tasks from the host MCU system. Multiple user-programmable, power saving, interrupt and autonomous data acquisition modes are available, including programmed acquisition cycle timing, and poll-only modes. Typical active supply current is 40 uA per measurement-second for a stable 30 cm output resolution. Pressure output can be resolved with output in fractions of a Pascal, and Altitude can be resolved in fractions of a meter.
Features¶
- 1.95V to 3.6V Supply Voltage, internally regulated by LDO
- 1.6V to 3.6V Digital Interface Supply Voltage
- Fully Compensated internally
- Direct Reading, Compensated
- - - Pressure: 20-bit measurement (Pascals)
- - - Altitude: 20-bit measurement (meters)
- - - Temperature: 12-bit measurement (degrees Celsius)
- Programmable Events
- Autonomous Data Acquisition
- Resolution down to 1 ft. / 30 cm
- 32 Sample FIFO
- Ability to log data up to 12 days using the FIFO
- 1 second to 9 hour data acquisition rate
- I2C digital output interface (operates up to 400 kHz)
Motivation¶
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. Currently the classes do not support using the FIFO or programmable interrupts. Although methods in the class are there to support adding those features.
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. Arduino users should find it easy to switch to an mbed device and port their code using this library.
Interface¶
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. I haven't found a way on the mbed to enable the internal pull-up resistors. 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.
Reference¶
API¶
Import library
Public Member Functions |
|
MPL3115A2 (I2C *i2c, Serial *pc=NULL) | |
Constructs an
MPL3115A2
object and associates an I2C and optional Serial debug object.
|
|
void | init () |
Initializes the sensor, defaulting to
Altitude
mode.
|
|
char | whoAmI () |
Queries the value from the WHO_AM_I register (usually equal to 0xC4).
|
|
Altitude * | readAltitude ( Altitude *a) |
Reads
Altitude
data from the sensor and returns it in the
Altitude
object passed in.
|
|
Pressure * | readPressure ( Pressure *p) |
Reads
Pressure
data from the sensor and returns it in the
Pressure
object passed in.
|
|
Temperature * | readTemperature ( Temperature *t) |
Reads
Temperature
data from the sensor and returns it in the
Temperature
object passed in.
|
|
char | offsetAltitude () |
Returns the altitude offset stored in the sensor.
|
|
void | setOffsetAltitude (const char offset) |
Sets the altitude offset stored in the sensor. The allowed offset range is from -128 to 127 meters.
|
|
char | offsetPressure () |
Returns the pressure offset stored in the sensor.
|
|
void | setOffsetPressure (const char offset) |
Sets the pressure offset stored in the sensor. The allowed offset range is from -128 to 127 where each LSB represents 4 Pa.
|
|
char | offsetTemperature () |
Returns the temperature offset stored in the sensor.
|
|
void | setOffsetTemperature (const char offset) |
Sets the temperature offset stored in the sensor. The allowed offset range is from -128 to 127 where each LSB represents 0.0625ºC.
|
|
void | setModeStandby () |
Puts the sensor into Standby mode. Required when using methods below.
|
|
void | setModeActive () |
Activates the sensor to start taking measurements.
|
|
void | setModeBarometer () |
Puts the sensor into barometric mode, be sure to put the sensor in standby mode first.
|
|
void | setModeAltimeter () |
Puts the sensor into altimeter mode, be sure to put the sensor in standby mode first.
|
|
void | setOversampleRate (char rate) |
Sets the number of samples from 1 to 128, be sure to put the sensor in standby mode first.
|
|
void | enableEventFlags () |
Sets all the event flags, be sure to put the sensor in standby mode first.
|
Example¶
Sample Code
#include "mbed.h" #include "MPL3115A2.h" I2C i2c(p28, p27); // sda, scl Serial pc(USBTX, USBRX); // tx, rx 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(); } }
Debugging¶
Tips for Debugging
If you want to use the debug features in this library to send data to a host PC, take a look at:
- SerialPC - Communicating between mbed and a host PC
On a windows machine, you will need to install a USB Serial driver, see:
An even better way to debug I2C communications with this chip would be to use a Saleae Logic Analyzer or equivalent. For the small investment in a logic analyzer, you will save yourself countless hours of frustration and writing printf commands.
See Also¶
- MPL115A2 I2C MEMS barometric and temperature sensor