BME280 - temperature, humidity and pressure sensor

In this tutorial we are going to meausure temperature, humidity and pressure using the BME280 sensor.


This tutorial assumes that you have done the i2c tutorial.

We recommend to have read the packages tutorial, since we are going to use a package for the BME280 driver.


Connect the BME280 to the ESP32 as follows:

  • VCC to 3V3
  • GND to GND
  • SCL to pin 25 (yellow on sparkfun boards)
  • SDA to pin 26 (blue on sparkfun boards)
Wiring diagram for a BME280 sensor
Wiring diagram for a BME280 sensor
Schematics for a BME280 sensor
Schematics for a BME280 sensor

Make sure the device is found by executing the list program from the i2c tutorial. It should print one of the BME280 I2C addresses: 0x76 (118) or 0x77 (119).


The driver for the BME280 is not part of the standard Toit distribution, but must be downloaded as package. See the packages tutorial for details.

We are using the bme280 package. To install it, run the following command in your project directory:

jag pkg install

You can probably just write jag pkg install bme280, but the full ID together with the version is more explicit, and will make sure you get the right package.


Create a new file bme280.toit and start jag watch bme280.toit to run the program whenever you save.

import gpio
import i2c
import bme280

  bus := i2c.Bus
    --sda=gpio.Pin 26
    --scl=gpio.Pin 25

  // Use 'I2C_ADDRESS' if your device has address 0x76 (118).
  // Use 'I2C_ADDRESS_ALT' if your device has address 0x77 (119).
  device := bus.device bme280.I2C_ADDRESS

  driver := bme280.Driver device

  print "$driver.read_temperature C"
  print "$driver.read_pressure Pa"
  print "$driver.read_humidity %"


The program first instantiates an I2C bus object, which it uses to get a handle to the device with the BME280 address. It then passes this device to the BME280 driver. At that point the driver configures the sensor.

When read_temperature, read_pressure and read_huminity is called, the driver then requests the corresponding values from the device. There is some computation to convert the raw values to human understandable values, but that's hidden by the driver.

Note that this program uses string interpolation.