Created
June 8, 2015 09:55
-
-
Save pepijndevos/c3646ef6652e0f0342dd to your computer and use it in GitHub Desktop.
A Python lib for reading the MPL3115A2 over the Raspberry Pi I2C bus.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from smbus import SMBus | |
from sys import exit | |
#I2C ADDRESS/BITS | |
MPL3115A2_ADDRESS = (0x60) | |
#REGISTERS | |
MPL3115A2_REGISTER_STATUS = (0x00) | |
MPL3115A2_REGISTER_STATUS_TDR = 0x02 | |
MPL3115A2_REGISTER_STATUS_PDR = 0x04 | |
MPL3115A2_REGISTER_STATUS_PTDR = 0x08 | |
MPL3115A2_REGISTER_PRESSURE_MSB = (0x01) | |
MPL3115A2_REGISTER_PRESSURE_CSB = (0x02) | |
MPL3115A2_REGISTER_PRESSURE_LSB = (0x03) | |
MPL3115A2_REGISTER_TEMP_MSB = (0x04) | |
MPL3115A2_REGISTER_TEMP_LSB = (0x05) | |
MPL3115A2_REGISTER_DR_STATUS = (0x06) | |
MPL3115A2_OUT_P_DELTA_MSB = (0x07) | |
MPL3115A2_OUT_P_DELTA_CSB = (0x08) | |
MPL3115A2_OUT_P_DELTA_LSB = (0x09) | |
MPL3115A2_OUT_T_DELTA_MSB = (0x0A) | |
MPL3115A2_OUT_T_DELTA_LSB = (0x0B) | |
MPL3115A2_BAR_IN_MSB = (0x14) | |
MPL3115A2_WHOAMI = (0x0C) | |
#BITS | |
MPL3115A2_PT_DATA_CFG = 0x13 | |
MPL3115A2_PT_DATA_CFG_TDEFE = 0x01 | |
MPL3115A2_PT_DATA_CFG_PDEFE = 0x02 | |
MPL3115A2_PT_DATA_CFG_DREM = 0x04 | |
MPL3115A2_CTRL_REG1 = (0x26) | |
MPL3115A2_CTRL_REG1_SBYB = 0x01 | |
MPL3115A2_CTRL_REG1_OST = 0x02 | |
MPL3115A2_CTRL_REG1_RST = 0x04 | |
MPL3115A2_CTRL_REG1_OS1 = 0x00 | |
MPL3115A2_CTRL_REG1_OS2 = 0x08 | |
MPL3115A2_CTRL_REG1_OS4 = 0x10 | |
MPL3115A2_CTRL_REG1_OS8 = 0x18 | |
MPL3115A2_CTRL_REG1_OS16 = 0x20 | |
MPL3115A2_CTRL_REG1_OS32 = 0x28 | |
MPL3115A2_CTRL_REG1_OS64 = 0x30 | |
MPL3115A2_CTRL_REG1_OS128 = 0x38 | |
MPL3115A2_CTRL_REG1_RAW = 0x40 | |
MPL3115A2_CTRL_REG1_ALT = 0x80 | |
MPL3115A2_CTRL_REG1_BAR = 0x00 | |
MPL3115A2_CTRL_REG2 = (0x27) | |
MPL3115A2_CTRL_REG3 = (0x28) | |
MPL3115A2_CTRL_REG4 = (0x29) | |
MPL3115A2_CTRL_REG5 = (0x2A) | |
MPL3115A2_REGISTER_STARTCONVERSION = (0x12) | |
bus = SMBus(1) | |
whoami = bus.read_byte_data(MPL3115A2_ADDRESS, MPL3115A2_WHOAMI) | |
if whoami != 0xc4: | |
print "Device not active." | |
exit(1) | |
bus.write_byte_data( | |
MPL3115A2_ADDRESS, | |
MPL3115A2_CTRL_REG1, | |
MPL3115A2_CTRL_REG1_SBYB | | |
MPL3115A2_CTRL_REG1_OS128 | | |
MPL3115A2_CTRL_REG1_ALT) | |
bus.write_byte_data( | |
MPL3115A2_ADDRESS, | |
MPL3115A2_PT_DATA_CFG, | |
MPL3115A2_PT_DATA_CFG_TDEFE | | |
MPL3115A2_PT_DATA_CFG_PDEFE | | |
MPL3115A2_PT_DATA_CFG_DREM) | |
def poll(): | |
sta = 0 | |
while not (sta & MPL3115A2_REGISTER_STATUS_PDR): | |
sta = bus.read_byte_data(MPL3115A2_ADDRESS, MPL3115A2_REGISTER_STATUS) | |
def altitude(): | |
bus.write_byte_data( | |
MPL3115A2_ADDRESS, | |
MPL3115A2_CTRL_REG1, | |
MPL3115A2_CTRL_REG1_SBYB | | |
MPL3115A2_CTRL_REG1_OS128 | | |
MPL3115A2_CTRL_REG1_ALT) | |
poll() | |
msb, csb, lsb = bus.read_i2c_block_data(MPL3115A2_ADDRESS,MPL3115A2_REGISTER_PRESSURE_MSB,3) | |
print msb, csb, lsb | |
alt = ((msb<<24) | (csb<<16) | (lsb<<8)) / 65536. | |
# correct sign | |
if alt > (1<<15): | |
alt -= 1<<16 | |
return alt | |
def pressure(): | |
bus.write_byte_data( | |
MPL3115A2_ADDRESS, | |
MPL3115A2_CTRL_REG1, | |
MPL3115A2_CTRL_REG1_SBYB | | |
MPL3115A2_CTRL_REG1_OS128 | | |
MPL3115A2_CTRL_REG1_BAR) | |
poll() | |
msb, csb, lsb = bus.read_i2c_block_data(MPL3115A2_ADDRESS,MPL3115A2_REGISTER_PRESSURE_MSB,3) | |
print msb, csb, lsb | |
return ((msb<<16) | (csb<<8) | lsb) / 64. | |
def calibrate(): | |
pa = int(pressure()/2) | |
bus.write_i2c_block_data(MPL3115A2_ADDRESS, MPL3115A2_BAR_IN_MSB, [pa>>8 & 0xff, pa & 0xff]) | |
@kronmall There's a kernel thing that you can enable which seems to help. See this page for details: http://ciaduck.blogspot.co.uk/2014/12/mpl3115a2-sensor-with-raspberry-pi.html
Setting that kernel flag stops this code outputting "Device not active", but I don't think the results I do get from it are that accurate, either. Did you get any further with it?
My code:
import MPL3115A2
#print MPL3115A2.pressure()
print MPL3115A2.altitude()
Output for pressure:
1 0 144
1026.25
Output for altitude:
95 251 208
24571.8125
A BMP280 sensor connected to the same Pi gives this output:
Temp = 21.84 *C
Pressure = 98467.00 Pa
Altitude = 240.71 m
Sealevel Pressure = 98460.00 Pa
Edit: Actually, the code on that linked blogspot page above gives more sensible output:
0xc4
Waiting for data...
Reading sensor data...
status: 0b0
Pressure and Temperature at 06/29/2017 10:28:40+0100
98287.25 Pa
18.8125°C
65.8625°F
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have a Sparkfun Weather Shield with the HTU21D and MPL3115A2 chips contained on it connected to my pi. I found another library for the HTU21D and it's working fine, however, I never make it past the "Device not active" part of this code. I've tried a couple other modules and get the same result. If I comment out that piece of code it runs properly and gives back values but they aren't accurate. Pressure = 47492 and Altitude = 11780 (I'm at around 600 feet). Any ideas what is going on or how to fix it?