Skip to content

BME280 Environmental Sensor

The Bosch BME280 is a low-cost ($2) temperature, and pressure sensor that has an I2C interface. This is an ideal way to learn how to use an I2C interface.

Note mbe280 is different from the BMP280 and does not read humidity. The default address is Hex x76 or decimal 118.

Circuit

The BME280 has a standard I2C interface with four wires:

  • GND - connect to any of the GND pins
  • VCC - connect this to the 3.3V output of the Pico
  • SCL - clock
  • SDA - data

I2C Scanner

After you have connected your sensor you can check the connection by running a quick "I2C Scanner" to find the address of the sensor.

1
2
3
4
5
import machine
sda=machine.Pin(0) # This is GP0 on row one of our standard Pico breadboard with the USB on top
scl=machine.Pin(1) # Row two of our standard Pico breadboard
i2c=machine.I2C(0, sda=sda, scl=scl, freq=400000)
print("Device found at decimal", i2c.scan())

Results:

1
[118]

This is decimal of hex 0x76

If the scanner does not return a number then your connections might not be working.

BME280 Driver

You should be able to find the BME280 driver by using the Thonny Tool -> Manage Packages... menu. If that does not work you can try a github search:

Search GitHub for MBE 280 Python Code

Test Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import machine
from utime import sleep
import BME280
sda=machine.Pin(0) # This is GP0 on row one of our standard Pico breadboard with the USB on top
scl=machine.Pin(1) # Row two of our standard Pico breadboard
i2c=machine.I2C(0, sda=sda, scl=scl, freq=400000)

# initialize the bme class using the default address
bme = BME280()
(chip_id, chip_version) = bme.getID()
print( "Chip ID:", chip_id)
print( "Version:", chip_version)

while True():
    # get new data from the 
    temperature,pressure,humidity = bme.getData()
    #adj_bar = bme.adj_baro(pressure, temperature)
    print("Adj {}".format(bme.adj_baro(pressure, temperature)))

    print( "Temperature: {}C".format(temperature))
    print( "Pressure: {}hpa, {}In".format(pressure, round(pressure * 0.02953, 2)))

    sleep(1)

Code 2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from machine import I2C
import BME280
from time import sleep

sda=machine.Pin(16)
scl=machine.Pin(17)
i2c=machine.I2C(0,sda=sda, scl=scl, freq=400000)
bme = BME280.BME280(i2c=i2c)
# print(i2c.scan())

while True:

  temp = bme.temperature
  hum = bme.humidity
  pres = bme.pressure
  # uncomment for temperature in Fahrenheit
  temp = (bme.read_temperature()/100) * (9/5) + 32
  #temp = str(round(temp, 2)) + 'F'
  print('Temperature: ', temp, end='')
  print(' Humidity:', hum, end='')
  print(' Pressure:', pres)

  sleep(5)

References

  1. Robert HH BME280 Test Page
  2. Official Datasheet
  3. eBay BME280 Sensor Search
  4. Driver Sample code

Digital Barometric Pressure Sensor Board Swap I2C/SPI BMP280 BME280 3.3V

BME280 4. BME280 logger 5. The Electronics India