In this example we connect a MPL3115A2 sensor to a STM32 Nucleo and the example will use the MBed compiler
The MPL3115A2 is a compact, piezoresistive, absolute pressure sensor with an I2C digital interface. MPL3115A2 has a wide operating range of 20 kPa to 110 kPa, a range that covers all surface elevations on earth. The MEMS is temperature compensated utilizing an on-chip temperature sensor. The pressure and temperature data is fed into a high resolution ADC to provide fully compensated and digitized outputs for pressure in Pascals and temperature in °C.
The compensated pressure output can then be converted to altitude, utilizing the formula stated in Section 9.1.3 “Pressure/altitude” provided in meters.The internal processing in MPL3115A2 removes compensation and unit conversion load from the system MCU, simplifying system design
Parts List
Here are the parts that I used
Schematics/Layout


Code
I imported the following example and library into the MBed compiler – https://os.mbed.com/teams/MSS/code/test_MPL3115A2/
#include "mbed.h"
#include "MSS.h"
#include "MPL3115A2.h"
#define MPL3115A2_I2C_ADDRESS (0x60)
int main() {
double alt, bar, temp ;
MPL3115A2 mpl(PIN_SDA, PIN_SCL, MPL3115A2_I2C_ADDRESS) ;
printf("=== test MPL3115A2 for %s (%s) ===\n") ;
printf("Altitude(m), Pressure(hPa), Temperature(c)\n") ;
while(1)
{
alt = mpl.getAltitude() ;
bar = mpl.getPressure() ;
temp = mpl.getTemperature() ;
printf("%6.2f, %6.2f, %6.2f\n",alt, bar, temp) ;
wait(1) ;
}
}
Output
Using a serial monitor program like Teraterm connect to the serial port of your STM32 nucleo


Links
https://www.nxp.com/docs/en/data-sheet/MPL3115A2.pdf