Raspberry Pi - MPL3115A2 Precision Altimeter Sensor Python Tutorial

About: We are a group of makers. We work in IoT, IOS app, android app, embedded design, sensor design, raspberry pi, arduino, beaglebone, particle electron, particle photon, Bluetooth.

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. It is capable of detecting a change in only 0.05 kPa which equates to a 0.3m change in altitude. Here is its demonstration with raspberry pi using python code.

Step 1: What You Need..!!

1. Raspberry Pi

2. MPL3115A2

LINK : https://www.controleverything.com/content/Analog-Digital-Converters?sku=MPL3115A2_I2CS

3. I²C Cable

LINK : https://www.controleverything.com/content/I2C-Cables?sku=I2CAFF3

4. I²C Shield for Raspberry Pi

LINK : https://www.controleverything.com/content/I2C-Master?sku=TOUTPI2

5. Ethernet Cable

Step 2: Connections:

Take an I2C shield for raspberry pi and gently push it over the gpio pins of raspberry pi.

Then connect the one end of I2C cable to MPL3115A2 sensor and the other end to the I2C shield.

Also connect the Ethernet cable to the pi or you can use a WiFi module.

Connections are shown in the picture above.

Step 3: Code:

The python code for MPL3115A2 can be downloaded from our github repository- ControlEverythingCommunity

Here is the link for the same :


The datasheet of MPL3115A2 can be found here:


We have used SMBus library for python code, the steps to install SMBus on raspberry pi is described here:


You can also copy the code from here, it is given as follows:

# Distributed with a free-will license.

# Use it any way you want, profit or free, provided it fits in the licenses of its associated works.

# MPL3115A2

# This code is designed to work with the MPL3115A2_I2CS I2C Mini Module available from ControlEverything.com.

# https://www.controleverything.com/products

import smbus

import time

# Get I2C bus

bus = smbus.SMBus(1)

# MPL3115A2 address, 0x60(96)

# Select control register, 0x26(38)

# 0xB9(185) Active mode, OSR = 128, Altimeter mode

bus.write_byte_data(0x60, 0x26, 0xB9)

# MPL3115A2 address, 0x60(96)

# Select data configuration register, 0x13(19)

# 0x07(07) Data ready event enabled for altitude, pressure, temperature

bus.write_byte_data(0x60, 0x13, 0x07)

# MPL3115A2 address, 0x60(96)

# Select control register, 0x26(38)

# 0xB9(185) Active mode, OSR = 128, Altimeter mode

bus.write_byte_data(0x60, 0x26, 0xB9)


# MPL3115A2 address, 0x60(96)

# Read data back from 0x00(00), 6 bytes

# status, tHeight MSB1, tHeight MSB, tHeight LSB, temp MSB, temp LSB

data = bus.read_i2c_block_data(0x60, 0x00, 6)

# Convert the data to 20-bits

tHeight = ((data[1] * 65536) + (data[2] * 256) + (data[3] & 0xF0)) / 16

temp = ((data[4] * 256) + (data[5] & 0xF0)) / 16

altitude = tHeight / 16.0

cTemp = temp / 16.0

fTemp = cTemp * 1.8 + 32

# MPL3115A2 address, 0x60(96)

# Select control register, 0x26(38)

# 0x39(57) Active mode, OSR = 128, Barometer mode

bus.write_byte_data(0x60, 0x26, 0x39)


# MPL3115A2 address, 0x60(96)

# Read data back from 0x00(00), 4 bytes

# status, pres MSB1, pres MSB, pres LSB

data = bus.read_i2c_block_data(0x60, 0x00, 4)

# Convert the data to 20-bits

pres = ((data[1] * 65536) + (data[2] * 256) + (data[3] & 0xF0)) / 16

pressure = (pres / 4.0) / 1000.0

# Output data to screen

print "Pressure : %.2f kPa" %pressure

print "Altitude : %.2f m" %altitude

print "Temperature in Celsius : %.2f C" %cTemp

print "Temperature in Fahrenheit : %.2f F" %fTemp

Step 4: Applications:

Various applications of MPL3115A2 includes High Accuracy Altimetry, Smartphones/Tablets, Personal Electronics Altimetry etc. It can also be incorporated in GPS Dead Reckoning, GPS Enhancement for Emergency Services, Map Assist, Navigation as well as Weather Station Equipment.



    • Beauty Tips Contest

      Beauty Tips Contest
    • Classroom Science Contest

      Classroom Science Contest
    • Frozen Treats Challenge

      Frozen Treats Challenge


    Nani chowdary

    Question 9 months ago on Step 4

    Does the sensor give the correct height from ground??

    I need to know as I will be using this sensor in my college project.please enlighten me.