Built-in project · Raspberry Pi Zero 2 W

BME280 on a Pi Zero 2 W with smbus2

A Raspberry Pi Zero 2 W reads a BME280 over I2C with smbus2 and the datasheet formulas: temperature, pressure and humidity every two seconds. The whole build, a Raspberry Pi Zero 2 W and 1 more part, runs here in your browser on the Python below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate Runs in your browser. Free, and no account needed.

The parts retail for about $21. On Mokxi they're free.

BME280 on a Pi Zero 2 W with smbus2 · Raspberry Pi Zero 2 Wlive0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The Python exactly as the editor opens it. Change a line there and press Run: the board runs main.py in a Python engine in the page, with gpiozero and RPi.GPIO, and nothing to compile.

main.py · Raspberry Pi Zero 2 W
# A BME280 on a Raspberry Pi Zero 2 W, over smbus2.
# SDA to GPIO2 (pin 3), SCL to GPIO3 (pin 5), VIN to 3V3 (pin 1), GND (pin 9).
# bme280_smbus.py reads it with the datasheet's compensation formulas.
import time
from smbus2 import SMBus
from bme280_smbus import BME280

bus = SMBus(1)
print("chip id 0x%02x" % bus.read_byte_data(0x76, 0xD0))   # 0x60 is a BME280
sensor = BME280(bus, 0x76)

while True:
    t, p, h = sensor.read()
    print("%.2f C  %.2f hPa  %.1f %%RH" % (t, p, h))
    time.sleep(2)
bme280_smbus.py · Raspberry Pi Zero 2 W
# bme280_smbus.py: read a Bosch BME280 from a Raspberry Pi over smbus2 on
# /dev/i2c-1, with the compensation formulas of the BME280 datasheet
# (BST-BME280-DS002, section 8.1, the double-precision ones). Runs unchanged
# on a real Pi with "pip install smbus2".

import time
from smbus2 import SMBus


def _s16(lo, hi):
    v = lo | hi << 8
    return v - 65536 if v > 32767 else v


class BME280:
    def __init__(self, bus=1, address=0x76):
        self.i2c = SMBus(bus) if isinstance(bus, int) else bus
        self.address = address
        chip = self.i2c.read_byte_data(address, 0xD0)
        if chip != 0x60:
            raise RuntimeError('no BME280 at 0x%02x (chip id 0x%02x)' % (address, chip))
        c = self.i2c.read_i2c_block_data(address, 0x88, 26)
        h = self.i2c.read_i2c_block_data(address, 0xE1, 7)
        self.t1 = c[0] | c[1] << 8
        self.t2 = _s16(c[2], c[3])
        self.t3 = _s16(c[4], c[5])
        self.p1 = c[6] | c[7] << 8
        self.p = [_s16(c[i], c[i + 1]) for i in range(8, 24, 2)]
        self.h1 = c[25]
        self.h2 = _s16(h[0], h[1])
        self.h3 = h[2]
        e4, e5, e6 = h[3] - 256 if h[3] > 127 else h[3], h[4], h[5] - 256 if h[5] > 127 else h[5]
        self.h4 = e4 * 16 | (e5 & 0x0F)
        self.h5 = e6 * 16 | (e5 >> 4)
        self.h6 = h[6] - 256 if h[6] > 127 else h[6]

    def read(self):
        """One forced measurement: (degrees C, hPa, % relative humidity)."""
        a = self.address
        self.i2c.write_byte_data(a, 0xF2, 0x01)        # humidity x1
        self.i2c.write_byte_data(a, 0xF4, 0x25)        # temperature x1, pressure x1, forced
        time.sleep(0.01)
        while self.i2c.read_byte_data(a, 0xF3) & 0x08:
            time.sleep(0.002)
        d = self.i2c.read_i2c_block_data(a, 0xF7, 8)
        adc_p = d[0] << 12 | d[1] << 4 | d[2] >> 4
        adc_t = d[3] << 12 | d[4] << 4 | d[5] >> 4
        adc_h = d[6] << 8 | d[7]

        v1 = (adc_t / 16384.0 - self.t1 / 1024.0) * self.t2
        v2 = (adc_t / 131072.0 - self.t1 / 8192.0) ** 2 * self.t3
        t_fine = v1 + v2
        celsius = t_fine / 5120.0

        p2, p3, p4, p5, p6, p7, p8, p9 = self.p
        v1 = t_fine / 2.0 - 64000.0
        v2 = v1 * v1 * p6 / 32768.0 + v1 * p5 * 2.0
        v2 = v2 / 4.0 + p4 * 65536.0
        v1 = (p3 * v1 * v1 / 524288.0 + p2 * v1) / 524288.0
        v1 = (1.0 + v1 / 32768.0) * self.p1
        pascals = 0.0
        if v1:
            pascals = 1048576.0 - adc_p
            pascals = (pascals - v2 / 4096.0) * 6250.0 / v1
            v1 = p9 * pascals * pascals / 2147483648.0
            v2 = pascals * p8 / 32768.0
            pascals += (v1 + v2 + p7) / 16.0

        hum = t_fine - 76800.0
        hum = (adc_h - (self.h4 * 64.0 + self.h5 / 16384.0 * hum)) * (
            self.h2 / 65536.0 * (1.0 + self.h6 / 67108864.0 * hum * (1.0 + self.h3 / 67108864.0 * hum)))
        hum = hum * (1.0 - self.h1 * hum / 524288.0)
        hum = min(max(hum, 0.0), 100.0)
        return celsius, pascals / 100.0, hum

Parts list

2 parts, plus the jumper wires. They retail for about $21; on Mokxi they're free.

How it is wired

4 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Raspberry Pi Zero 2 W pin 3V3; Pressure, humidity and temperature, BME280 pin VIN
  • Raspberry Pi Zero 2 W pin GPIO2; Pressure, humidity and temperature, BME280 pin SDA
  • Raspberry Pi Zero 2 W pin GPIO3; Pressure, humidity and temperature, BME280 pin SCL
  • Ground: Raspberry Pi Zero 2 W pin GND; Pressure, humidity and temperature, BME280 pin GND

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.