Home Code Raspberry PI and MPL3115A2 pressure sensor

Raspberry PI and MPL3115A2 pressure sensor

by shedboy71

In this article we will take a look at the MPL3115A2 pressure sensor and connect it to our Raspberry PI. lets take a look at the MPL3115A2 first

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

 

Layout

Lets look at how you connect the sensor to your Raspberry Pi

An I2C device that just needs 3.3v and GND as well, so its pretty simple to connect to your Raspberry PI, the layout below shows a Raspberry PI 3 but you can use others as well

pi and MPL3115A2 layout

pi and MPL3115A2 layout

Parts List

Here is a parts list generated from the fritzing layout above. The sensor costs about

Amount Part Type Properties
1 MPL3115A2 I2C Barometric Pressure, Altitude, Temperature Sensor MPL3115A2 I2C Intelligent Temperature Pressure Altitude Sensor V2.0
1 Raspberry Pi 3 Original 2018 New Raspberry Pi 3 Model B+plus Board 1GB LPDDR2 BCM2837B0 Quad-Core Ras PI3


This is a python example, the code comes from CodeEverything as you can see in the comments

 

Code Example

[codesyntax lang=”python”]

# 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://shop.controleverything.com/products/precision-altimeter-500-to-1100-mbar#tabs-0-product_tabset-2

import smbus
import time

# Get I2C bus
bus = smbus.SMBus(1)

# I2C address of the device
MPL3115A2_DEFAULT_ADDRESS			= 0x60

# MPL3115A2 Regster Map
MPL3115A2_REG_STATUS				= 0x00 # Sensor status Register
MPL3115A2_REG_PRESSURE_MSB			= 0x01 # Pressure data out MSB
MPL3115A2_REG_PRESSURE_CSB			= 0x02 # Pressure data out CSB
MPL3115A2_REG_PRESSURE_LSB			= 0x03 # Pressure data out LSB
MPL3115A2_REG_TEMP_MSB				= 0x04 # Temperature data out MSB
MPL3115A2_REG_TEMP_LSB				= 0x05 # Temperature data out LSB
MPL3115A2_REG_DR_STATUS				= 0x06 # Data Ready status registe
MPL3115A2_OUT_P_DELTA_MSB			= 0x07 # Pressure data out delta MSB
MPL3115A2_OUT_P_DELTA_CSB			= 0x08 # Pressure data out delta CSB
MPL3115A2_OUT_P_DELTA_LSB			= 0x09 # Pressure data out delta LSB
MPL3115A2_OUT_T_DELTA_MSB			= 0x0A # Temperature data out delta MSB
MPL3115A2_OUT_T_DELTA_LSB			= 0x0B # Temperature data out delta LSB
MPL3115A2_REG_WHO_AM_I				= 0x0C # Device Identification Register
MPL3115A2_PT_DATA_CFG				= 0x13 # PT Data Configuration Register
MPL3115A2_CTRL_REG1					= 0x26 # Control Register-1
MPL3115A2_CTRL_REG2					= 0x27 # Control Register-2
MPL3115A2_CTRL_REG3					= 0x28 # Control Register-3
MPL3115A2_CTRL_REG4					= 0x29 # Control Register-4
MPL3115A2_CTRL_REG5					= 0x2A # Control Register-5

# MPL3115A2 PT Data Configuration Register
MPL3115A2_PT_DATA_CFG_TDEFE			= 0x01 # Raise event flag on new temperature data
MPL3115A2_PT_DATA_CFG_PDEFE			= 0x02 # Raise event flag on new pressure/altitude data
MPL3115A2_PT_DATA_CFG_DREM			= 0x04 # Generate data ready event flag on new pressure/altitude or temperature data

# MPL3115A2 Control Register-1 Configuration
MPL3115A2_CTRL_REG1_SBYB			= 0x01 # Part is ACTIVE
MPL3115A2_CTRL_REG1_OST				= 0x02 # OST Bit ACTIVE
MPL3115A2_CTRL_REG1_RST				= 0x04 # Device reset enabled
MPL3115A2_CTRL_REG1_OS1				= 0x00 # Oversample ratio = 1
MPL3115A2_CTRL_REG1_OS2				= 0x08 # Oversample ratio = 2
MPL3115A2_CTRL_REG1_OS4				= 0x10 # Oversample ratio = 4
MPL3115A2_CTRL_REG1_OS8				= 0x18 # Oversample ratio = 8
MPL3115A2_CTRL_REG1_OS16			= 0x20 # Oversample ratio = 16
MPL3115A2_CTRL_REG1_OS32			= 0x28 # Oversample ratio = 32
MPL3115A2_CTRL_REG1_OS64			= 0x30 # Oversample ratio = 64
MPL3115A2_CTRL_REG1_OS128			= 0x38 # Oversample ratio = 128
MPL3115A2_CTRL_REG1_RAW				= 0x40 # RAW output mode
MPL3115A2_CTRL_REG1_ALT				= 0x80 # Part is in altimeter mod
MPL3115A2_CTRL_REG1_BAR				= 0x00 # Part is in barometer mode

class MPL3115A2():
	def control_alt_config(self):
		"""Select the Control Register-1 Configuration from the given provided value"""
		CONTROL_CONFIG = (MPL3115A2_CTRL_REG1_SBYB | MPL3115A2_CTRL_REG1_OS128 | MPL3115A2_CTRL_REG1_ALT)
		bus.write_byte_data(MPL3115A2_DEFAULT_ADDRESS, MPL3115A2_CTRL_REG1, CONTROL_CONFIG)
	
	def data_config(self):
		"""Select the PT Data Configuration Register from the given provided value"""
		DATA_CONFIG = (MPL3115A2_PT_DATA_CFG_TDEFE | MPL3115A2_PT_DATA_CFG_PDEFE | MPL3115A2_PT_DATA_CFG_DREM)
		bus.write_byte_data(MPL3115A2_DEFAULT_ADDRESS, MPL3115A2_PT_DATA_CFG, DATA_CONFIG)
	
	def read_alt_temp(self):
		"""Read data back from MPL3115A2_REG_STATUS(0x00), 6 bytes
		status, tHeight MSB, tHeight CSB, tHeight LSB, temp MSB, temp LSB"""
		data = bus.read_i2c_block_data(MPL3115A2_DEFAULT_ADDRESS, MPL3115A2_REG_STATUS, 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
		
		return {'a' : altitude, 'c' : cTemp, 'f' : fTemp}
	
	def control_pres_config(self):
		"""Select the Control Register-1 Configuration from the given provided value"""
		CONTROL_CONFIG = (MPL3115A2_CTRL_REG1_SBYB | MPL3115A2_CTRL_REG1_OS128)
		bus.write_byte_data(MPL3115A2_DEFAULT_ADDRESS, MPL3115A2_CTRL_REG1, CONTROL_CONFIG)
	
	def read_pres(self):
		"""Read data back from MPL3115A2_REG_STATUS(0x00), 4 bytes
		status, pres MSB, pres CSB, pres LSB"""
		data = bus.read_i2c_block_data(MPL3115A2_DEFAULT_ADDRESS, MPL3115A2_REG_STATUS, 4)
		
		# Convert the data to 20-bits
		pres = ((data[1] * 65536) + (data[2] * 256) + (data[3] & 0xF0)) / 16
		pressure = (pres / 4.0) / 1000.0
		
		return {'p' : pressure}

from MPL3115A2 import MPL3115A2
mpl3115a2 = MPL3115A2()

while True :
	mpl3115a2.control_alt_config()
	mpl3115a2.data_config()
	time.sleep(1)
	alt = mpl3115a2.read_alt_temp()
	print "Altitude : %.2f m"%(alt['a'])
	print "Temperature in Celsius : %.2f C"%(alt['c'])
	print "Temperature in Fahrenheit : %.2f F"%(alt['f'])
	mpl3115a2.control_pres_config()
	time.sleep(1)
	pres = mpl3115a2.read_pres()
	print "Pressure : %.2f kPa"%(pres['p'])
	print " ************************************* "

[/codesyntax]

copy it to your Raspbery PI, open a terminal and run

sudo python MPL3115A2.py

Output

You should see something like this in the terminal window

MPL3115A2 output

MPL3115A2 output

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More