How to Hookup BME280 Sensor to Arduino using SPI

Overview

The BME280 from Bosch Sensortec is a integrated environmental sensor designed for the mobile market. It a low power consumption design that combines high linearity and high accuracy sensors for pressure, humidity and temperature.

The BME280 supports either SPI or I2C interface to communicate. In this hookup guide we are using the SPI bus.

Because of the small size of the sensor, the best way to use this sensor is with a breakout board. The Adafruit breakout board is used here.

In this hookup we are only connecting one device to the Arduino using the SPI bus. It reads the barometric pressure, humidity and temperature and displays it on the console.

Hookup Diagram for Adafruit BME280 breakout board to Arduino using I2C

For hookup details using the I2C bus (including sketch and library) click on this link.

Hookup BME280 using I2C bus

Images

BME280 Sensor on Adafruit Breakout board
Note: Images shown on this page are not to scale.

Hardware and Software Parts List


Data Sheets


Wiring up the BME280 Sensor

The SPI bus requires 4 wires plus power and ground. We can power it using 5V or 3.3V as the breakout board has a power regulator on board to regulate it to 3.3V for the sensor.

Hookup Diagram for Adafruit BME280 breakout board to Arduino using SPI

We can use 5V or 3.3V for the power supply. Just make sure you connect it to the appropriate pin on the breakout board.

We can wire up SPI in two ways. We can use the SPI hardware pins which in the case of the Arduino Uno are shown below. The alternative is to emulate the SPI in software which allows us to use pins other than below. In this case we need to define what these pins are when we create the BME280 object. So in this case we can use other digital pins instead of the ones shown below.

  • Digital Pin 13 to SCK (Serial Clock)
  • Digital Pin 12 to SDO (Serial Data Out)
  • Digital Pin 11 to SDI (Serial Data In)
  • Digital Pin 10 to CS (Chip Select)
Hookup Arduino to Bosch BME280 Sensor using SPI


Software

The hookup and Arduino Sketch is for connecting a single Adafruit BME280 breakout board.

You will need to download the cactus.io BME280_SPI library to work with this sample sketch. The library supports reading the barometric pressure, humidity and temperature from the sensor.

We create the BME280 object in one of two ways. The first way is if we are using the hardware SPI pins as shown above. Uncomment line 10 in the code below and comment out line 11.

To use software SPI which allows us to use alternate digital pins we need to comment out line 10 and uncomment line 11. We also need to define what pins we are using. This is done in lines 4 to 7. We need to change the defines pin numbering to correspond to the SPI functions. For example if digital pin 7 is connected to CS on the breakout board we would change line 7 to #define BME280_CS from 10 to 7.

To get the data from the sensor we need to call bme.readSensor(). We can then call the various functions to get the temperature, humidity and pressure.

The sensor returns the temperature in degrees celsius by design. We can call the method in the BME280 library called getTemperature_C to get the celsius value or getTemperature_F for the value in fahrenheit.

The barometric pressure from the sensor is returned in Pascals. 100 Pascals = 1hPa = 1 millibar. The library method getPressure_MB will return the value in millibars.

When we create the BME280 object in the sketch we define the hardware pins that are used for chip select, data in and out and finally the serial clock.

We have found that when the BME280 is enclosed inside a case or nearby a circuit board the heat generated by voltage regulators can affect the temperature reading from the BME280. We have included a function in the library that allows us to apply a temperature calibration offset to the value returned by getTemperature_C or _F. Basically it justs adds the calibration offset. So if the temperature is reading 2 degrees high then we use this function:

bme.setTempCal(-2);    which will subtract 2 degrees from the temperture reading.

Limitations
  • The sketch Supports only SPI bus connection.
  • The sketch Supports only one sensor connected.
  • Its blocking code.

Library

The cactus_io_BME280_SPI library provides the following functions:

  • BME280_SPI(int cspin) Creates an object using hardware SPI
  • or BME280_SPI(int cspin, int mosipin, int misopin, int sckpin) Creates an object using software SPI
  • begin() Called to initialise the sensor after the object is created
  • setTempCal(float) Allows you to define a temp calibration offset if it reads high
  • readSensor() Called to read data from the sensor
  • getTemperature_C() Returns a float value in degrees celsius
  • getTemperature_F() Returns a float value in degrees fahrenheit
  • getHumidity() Returns a float as a percentage humidity
  • getPressure_MB() Returns a float in millibars
  • getPressure_HP() Returns a float in hectapascals

Sample Sketch

Bosch BME280 SPI Sample Sketch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <SPI.h>
#include "cactus_io_BME280_SPI.h"

#define BME_SCK 13;// Serial Clock
#define BME_MISO 12;// Serial Data Out
#define BME_MOSI 11;// Serial Data In
#define BME_CS 10;// Chip Select

// Create BME280 object
// BME280_SPI bme(BME_CS); // Using Hardware SPI
BME280_SPI bme(BME_CS,BME_MOSI,BME_MISO,BME_SCK); // Using Software SPI

void setup() {

Serial.begin(9600);
Serial.println("Bosch BME280 Pressure - Humidity - Temp Sensor | cactus.io");

if (!bme.begin()) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}

bme.setTempCal(-1);// Sensor was reading high so offset by 1 degree C

Serial.println("Pressure\tHumdity\t\tTemp\ttTemp");
}

void loop() {

bme.readSensor();

Serial.print(bme.getPressure_MB()); Serial.print(" mb\t"); // Pressure in millibars
Serial.print(bme.getHumidity()); Serial.print(" %\t\t");
Serial.print(bme.getTemperature_C()); Serial.print(" *C\t");
Serial.print(bme.getTemperature_F()); Serial.println(" *F");

// Add a 2 second delay.
delay(2000); //just here to slow down the output.
}

Sketch Console Output

Arduino Bosch BME280 Sensor Sketch

Other Resources

Adafruit BME280 Barometric Pressure Tutorial


License