## Adafruit MPL115A2 Temp/Pressure sensor - Python

pleriche
Posts: 80
Joined: Mon Oct 14, 2013 8:44 am

### Adafruit MPL115A2 Temp/Pressure sensor - Python

The documentation that comes with the MPL115A2 temperature and pressure sensor from Adafruit only gives C code for Arduino, which is less useful than might have been hoped. I've searched in vain for Python code for the Raspberry Pi.

After quite a long slog, I think I have it working. Gotchas along the way were the fact that SMBus function read_word_data() appears to assume little-endian data whereas the device presents it big-endian (SMBus documentation leaves quite a lot to be desired), and the knotty little problem of how to convert 16 bits 2's complement presented as a long into a float. The MPL115A2 datasheet was not exactly written for dummies, and I still can't figure how the definition of coefficient c12 corresponds with how you in fact have to interpret it.

Anyway here's my code. This post is not really asking a question (unless anyone can suggest any improvements or bug fixes), but rather in the hope that the next person trying to do it might have more success in a Google search than I did.

Code: Select all

``````#!/usr/bin/python
import time
import smbus

bus = smbus.SMBus(1)

# a0: 16 bits - 1 sign, 12 int, 3 frac
if a0 & 0x8000:
a0d = -((~a0 & 0xffff) + 1)
else:
a0d = a0
a0f = float(a0d) / 8.0
print("a0 = 0x%4x %5d %4.3f" % (a0, a0d, a0f))

# b1: 16 bits - 1 sign, 2 int, 13 frac
if b1 & 0x8000:
b1d = -((~b1 & 0xffff) + 1)
else:
b1d = b1
b1f = float(b1d) / 8192.0
print("b1 = 0x%4x %5d %1.5f" % (b1, b1d, b1f))

# b2: 16 bits - 1 sign, 1 int, 14 frac
if b2 & 0x8000:
b2d = -((~b2 & 0xffff) + 1)
else:
b2d = b2
b2f = float(b2d) / 16384.0
print("b2 = 0x%4x %5d %1.5f" % (b2, b2d, b2f))

# c12: 14 bits - 1 sign, 0 int, 13 frac
# (Documentation in the datasheet is poor on this.)
if c12 & 0x8000:
c12d = -((~c12 & 0xffff) + 1)
else:
c12d = c12
c12f = float(c12d) / 16777216.0
print("c12 = 0x%4x %5d %1.5f" % (c12, c12d, c12f))

# Start conversion and wait 3mS
time.sleep(0.003)

print("\nRaw pres = 0x%3x %4d" % (rawpres, rawpres))
print("Raw temp = 0x%3x %4d" % (rawtemp, rawtemp))

pcomp = a0f + (b1f + c12f * rawtemp) * rawpres + b2f * rawtemp
pkpa = pcomp / 15.737 + 50
print("Pres = %3.2f kPa" % pkpa)

temp = 25.0 - (rawtemp - 498.0) / 5.35
print("Temp = %3.2f" % temp)
``````
Regards - Philip
PS I think the weather is going to be fine tomorrow.

mascal
Posts: 17
Joined: Thu Feb 21, 2013 8:09 am
Contact: Website

### Re: Adafruit MPL115A2 Temp/Pressure sensor - Python

I already saw the lack of information for successfully using this sensor with RPi and I was really happy to finally see a useful post.

Thank You really much!
Mascal
http://rpihome.blogspot.com

jgoat
Posts: 24
Joined: Wed Oct 02, 2013 12:11 pm

### Re: Adafruit MPL115A2 Temp/Pressure sensor - Python

May I also add my voice of praise to your rather excellent bit of heavy lifting.

Adafruit ought to link to your code, or perhaps even hire you to write a tutorial for their site.

Whatever the case, you made today easy, and I also thank you.

Nev
Posts: 26
Joined: Fri Jan 16, 2015 7:09 pm

### Re: Adafruit MPL115A2 Temp/Pressure sensor - Python

Hi Philip,
Just discovered your solution for the above. It works perfectly on my RPi b+ And prints out to my 16 x 2 i2c lcd display. Thanks ver much for all the hard work.
Nev

pleriche
Posts: 80
Joined: Mon Oct 14, 2013 8:44 am

### Re: Adafruit MPL115A2 Temp/Pressure sensor - Python

Glad it's still useful, nearly 3 years later!

Regards - Philip