Using the BME280 I2C Temperature and Pressure Sensor in Python

The BME280 device is a digital barometric pressure sensor and is a slightly upgraded version of the BMP180. This is available on a small module which provides access to the sensor via the I2C interface. This allows us to easily connect it to the Raspberry Pi and read the data using Python. The BME280 provides temperature, pressure and humidity.

BME280 Temperature Pressure SensorThe BME280 is made by Bosch and the official BME280 datasheet includes all the technical details. Their device can offer both SPI and I2C interfaces so you need to make sure your module provides the interface you prefer.

My module is a small pcb measuring 14x10mm with a 4 pin I2C header. The order of the pins may vary on other modules so keep an eye on the labels so you connect up the correct wires from the Pi.

Configure I2C Interface

In order to use this module you must enable the I2C interface on the Raspberry Pi as it is not enabled by default. This is a fairly easy process and is described in my Enabling The I2C Interface On The Raspberry Pi tutorial.

Connecting Hardware

The table below shows how the module is connected to the Raspberyr Pi’s GPIO header (P1). Please refer to my GPIO header guide for a diagram.

Module PCB Desc GPIO Header Pins
VCC 3.3V P1-01
GND Ground P1-06

Here is a diagram of a breadboard setup. If you are connecting the module’s four pins directly to the Pi you only need four female-female wires.

BME280 Module Setup

Other modules are available which have different pin arrangements so make sure you are connecting the correct pins to the Pi if yours is different to the one shown in this tutorial.

With the device connected and the Pi powered up the “i2cdetect” command should show the device with address 0x76 or 0x77.

Download BME280 Script

To download the BME280 Python script from my Bitbucket repository you can use :

wget -O



or right-click on this link in your browser and save locally.

Run the Script

Before running the script you should check that your device is connected. If you installed the i2c-tools package as part of the i2c setup you should use the i2cdetect command to check it returns an address for your device. The script assumes the address is 0x76. You can change that by editing the DEVICE variable in bme280,py using your favourite text editor.

Run the script using :


Here is the output I see on my Pi :

BME280 Sensor Setup

Script Breakdown

The script is fairly straight forward but has some scary looking maths in it. This is defined in the datasheet and you will be forgiven for not worrying too much how it works! I rough summary of the script is given below if you want to follow it through in a bit more detail :

  • imports some libraries
  • defines some functions
  • function main uses the readBME280ID function to get the ID of the device
  • function main then calls readBME280All which …
    • sets the oversampling and mode
    • reads calibration data from the device that was preset in the factory
    • reads the raw temperature, pressure and humidity data
    • refines the data using the maths from the datasheet
    • returns the values to main()

Include In Your Own Script

You could just modify the main function in my script but you may want to include the functionality in your own. To do this you can import my script and then reference the readBME280ID and readBME280All functions as in the example below :

import bme280

(chip_id, chip_version) = bme280.readBME280ID()
print "Chip ID :", chip_id
print "Version :", chip_version

temperature,pressure,humidity = bme280.readBME280All()

print "Temperature : ", temperature, "C"
print "Pressure : ", pressure, "hPa"
print "Humidity : ", humidity, "%"

The rest is down to your imagination.


  • If the i2cdetect command results in an error you have either not installed i2c-tools or you need to use 0 rather than 1
  • If your device is not detected and you don’t see the address you have either not connected the device properly or have not correctly enabled the i2c interface
  • Double check your wiring and reboot the Pi

In most cases i2c experiments fail to work because :

  • i2c is not enabled
  • The device is not connected properly
  • i2c-tools has not been installed so the i2cdetect command is not available

In rare cases your device may be faulty but it is far more likely to be one of the above issues.

The module is available from eBay, Amazon and many other online electronics shops.