BME280 Environmental Sensor

This documentation includes installation guidelines and sample code for your hardware.

Python Library

We upgraded the BME280 sensor to BME680 in the IoT HAT revision 2. We're keeping this documentation for backward compatibility. If you have the IoT HAT revision 2, please use the BME680 library.

The new BME680 sensor adds gas resistance (hence, air quality) reading to the IoT HAT.

Turta_BME280 library is responsible for communicating with BME280 sensor.

To install the library from the (PyPI) Python Package Index, enter the following command to the console.

pip install turta-iothat

Add the following statement to the top of your Python code.

from turta_iothat import Turta_BME280

Then, create an instance of the Turta_BME280 class.

bme280 = Turta_BME280.BME280Sensor()

Now you're ready to access the library by calling the bme280 instance.

Initialization

The library automatically initializes the required components when its instance is being created, so there is no need to do a manual initialization.

Basic Members

Read Temperature

Reads the temperature in Celsius.

float read_temperature()
  • Parameters

    • None

  • Returns

    • Float: Temperature reading.

Read Humidity

Reads the relative humidity.

float read_humidity()
  • Parameters

    • None

  • Returns

    • Float: Relative humidity reading.

Read Pressure

Reads the pressure in Pa.

float read_pressure()
  • Parameters

    • None

  • Returns

    • Float: Air pressure in Pa.

Read Altitude

Reads the altitude from the sea level in meters.

float read_altitude(meanSeaLevelPressureInBar)
  • Parameters

    • Float meanSeaLevelPressureInBar: Mean sea level pressure in bar.

  • Returns

    • Float: Altitude from the sea level.

Samples

You can copy the example code from https://github.com/Turta-io/IoTHAT/tree/master/Samples/Raspbian/Python address, and then copy it to the Raspberry Pi. There is one example of this library.

BME280 Sample App

This application demonstrates temperature, humidity, pressure, and altitude readings.

To run the example:

  1. Copy the library and sample code to a folder on the Raspberry Pi.

  2. Open terminal, and go to the folder you copied the codes.

  3. Run the sample with the following command:

python BME280SampleApp.py

Sample Code

BME280SampleApp.py
#! /usr/bin/python

import time
from turta_iothat import Turta_BME280
#Install IoT HAT library with "pip install turta-iothat"

#Variables
#Sea level pressure in bar
slp = 1000.0 #Update this from weather forecast to get precise altitude

#Initialize
bme280 = Turta_BME280.BME280Sensor()

try:
    while True:
        #Hint: To get temperature, pressure and humidity readings at the same time,
        #call BME280Sensor.ReadTPH() method.

        #Read & print temperature
        print("Temperature.....: " + str(round(bme280.read_temperature(), 1)) + "C")

        #Read & print humidity
        print("Humidity........: %" + str(round(bme280.read_humidity(), 1)) + "RH")

        #Read & print pressure
        print("Pressure........: " + str(round(bme280.read_pressure(), 1)) + "Pa")

        #Read & print altitude
        print("Altitude........: " + str(round(bme280.read_altitude(slp), 1)) + "m")

        #Rest a bit
        print("-----")
        time.sleep(10.0)

#Exit on CTRL+C
except KeyboardInterrupt:
    print('Bye.')

Result

When you run the sample, it prints sensor readings to the terminal. The example output should be like this:

Temperature.....: 25.0
Humidity........: 50.0
Pressure........: 1000.0
Altitude........: 100.0
-----

To stop the Python script, just press CTRL + C.

Last updated