$ cat bmp280.py # 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. # BMP280 # This code is designed to work with the BMP280_I2CS I2C Mini Module available from ControlEverything.com. # https://www.controleverything.com/content/Barometer?sku=BMP280_I2CSs#tabs-0-product_tabset-2 import smbus import time
# Get I2C bus bus = smbus.SMBus(1)
# BMP280 address, 0x76(118) # Read data back from 0x88(136), 24 bytes b1 = bus.read_i2c_block_data(0x76, 0x88, 24)
# BMP280 address, 0x76(118) # Select Control measurement register, 0xF4(244) # 0x27(39) Pressure and Temperature Oversampling rate = 1 # Normal mode bus.write_byte_data(0x76, 0xF4, 0x27) # BMP280 address, 0x76(118) # Select Configuration register, 0xF5(245) # 0xA0(00) Stand_by time = 1000 ms bus.write_byte_data(0x76, 0xF5, 0xA0)
time.sleep(0.5)
# BMP280 address, 0x76(118) # Read data back from 0xF7(247), 8 bytes # Pressure MSB, Pressure LSB, Pressure xLSB, Temperature MSB, Temperature LSB # Temperature xLSB, Humidity MSB, Humidity LSB data = bus.read_i2c_block_data(0x76, 0xF7, 8)
# Convert pressure and temperature data to 19-bits adc_p = ((data[0] * 65536) + (data[1] * 256) + (data[2] & 0xF0)) / 16 adc_t = ((data[3] * 65536) + (data[4] * 256) + (data[5] & 0xF0)) / 16