SparkFun BME280 Breakout Hookup Guide a learn.sparkfun.com tutorial
Available online at: http://sfe.io/t419
Introduction
The BME280 Breakout Board is the easy way to measure pressure and humidity, and without taking up a lot of room. It gives you easy to solder 0.1" headers, runs I2C or SPI, takes measurements at less than 1mA and idles less than 5uA (yes, microamps!).
The BME280 can be used to take pressure, humidity, and temperature readings. Use the data to get relative altitude changes, or absolute altitude if the locally reported barometric pressure is known.
The BME280 Breakout Board.
Ranges:
- Temp: -40C to 85C
- Humidity: 0 - 100% RH, =-3% from 20-80%
- Pressure: 30,000Pa to 110,000Pa, relative accuracy of 12Pa, absolute accuracy of 100Pa
- Altitude: 0 to 30,000 ft (9.2 km), relative accuracy of 3.3 ft (1 m) at sea level, 6.6 (2 m) at 30,000 ft.
Covered In This Tutorial
This tutorial gives you all you need to get going with the BME280. First we’ll take a look at the IC and hardware, then we’ll use the SparkFun BME280 Arduino library to get data out of it by SPI or I2C.
The tutorial is split into the following pages:
- BME280 Hardware Overview– Basic information about the hardware.
- Assembly– Connect to the BME280 by I2C or SPI
- Installing the Arduino Library– How to get it
- Using the Arduino Library– explains the user API
- Theory and Example Data– Showcase of the examples included with the library.
- Resources and Going Further– Links to the datasheet and application notes, plus inspirational projects
Required Materials
Get the datasheet and application notes now. Keep a copy to refer to once you get off the charted path.
This tutorial explains how to use the BME280 Breakout Board with an RedBoard (or Arduino). To follow along, you’ll need the following materials:
- BME280 Breakout Board
- Arduino UNO, RedBoard, or another Arduino-compatible board
- Straight Male Headers– Or wire. Something to connect between the breakout and a breadboard.
- Breadboard– Any size (even mini) should do.
- M/M Jumper Wires– To connect between Arduino and breadboard.
- Logic Level Converter– To shift SPI levels from 5v to 3.3v.
If you use a 3.3V-based micro – like the Arduino Pro 3.3V or 3.3V Pro Mini– there is no need for level shifting.
Suggested Reading
Connection of the BME280 uses some basic concepts shared by a lot of our products. If you want to get more familiar with these basic tasks, these articles can help you out.
- Serial Peripheral Interface (SPI)
- Inter-IC Communication (I2C)
- Logic Levels
- Bi-Directional Level Shifter Hookup Guide
If the concepts of pressure are weighing on you, check out these links.
- (external) Air Pressure Altitude Calculator– Play around to get a feel for what the pressures are at different altitudes.
- Wikipedia: Atmospheric_pressure– Has a nice equation for conversion of pressure and altitude (referenced for library code).
- MPL3115a2-pressure-sensor-hookup-guide – pressure-vs-altimeter-setting– Confused why the reading pressure doesn’t match the reported pressure from your local weather station? Read this section.
Hardware Overview
The Front Side
The BME280 Breakout board has 10 pins, but no more than 6 are used at a single time.
Use one header for I2C connections, or the other for SPI connections – no need to use both!
The left side of the board are power, ground, and I2C pins.
Pin Label | Pin Function | Notes |
---|---|---|
GND | Ground | 0V voltage supply. |
3.3v | Power Supply | Supply voltage to the chip. Should be regulated between 1.8V and 3.6V. |
SDA | Data | I2C: Serial data (bi-directional) |
SCL | Serial Clock | I2C serial clock. |
The remaining pins are broken out on the other side. These pins break out SPI functionality and have another power and ground.
Pin Label | Pin Function | Notes |
---|---|---|
GND | Ground | 0V voltage supply. |
3.3v | Power Supply | Supply voltage to the chip. Should be regulated between 1.8V and 3.6V. |
SCK | Clock | Clock line, 3.6V max |
SDO | Data out | Data comming out of the BME280 |
SDI | Data in | Data going into the BME280, 3.6V max |
!CS | Chip Select (Slave Select) | Active low chip select, 3.6V max |
The Back Side
On the other side of the board you’ll find all the configuration jumpers. Pull-ups can be left connected even when using SPI mode, so you’ll probably never have to touch these. If you do, here’s what they’re for.
Jumper Label | Jumper Function | Notes |
---|---|---|
ADR: | I2C Address | Select between addresses 0x77 (default, '1' side) and 0x76 by slicing the trace and bridging the '0' side. Controls the least significant bit. |
CS PU | SPI chip select pull-up | Connects a 4.7k resistor to the CS line to make sure it is idle high. Can be disconnected by slicing between the jumper pads. |
I2C | I2C pull-ups | Connects the I2C pull-up resistors to 3.3V. Cut the trace to disconnect them if necessary. |
Assembly
Attaching the headers
The board comes without headers. Regular wires can be soldered in, but for a more configurable breadboard experience you may want to attach headers.
Use a breadboard to align and hold the pins
Prepare to solder
Solder on the pins
![]() | ![]() | ![]() |
For generic operation solder both headers (left). If you only need I2C (middle), or SPI (right), only attach those headers.
I2C Connection
The sensor pulls the I2C lines to 3.3V, so they can be directly connect to the redboard’s A4/A5 pins, or the SDA/SCL pins (as long as they’re configured by Wire). Make sure to power the sensor from 3.3v! The power and ground pins are connected, so you only need to connect to one side.
Diagram showing I2C connection to the BME280. You could also use the dedicated SDA and SCL lines found on most Arduino boards.
SPI Connection
The SPI connection isn’t quite straightforward when connected to a RedBoard. The Logic Level Converter is required to bridge between the 3.3v requirement of the BME280 and the 5v IO of the RedBoard. 3.3v microcontrollers such as the fabulous Teensy 3.2 can be directly connected.
Diagram showing SPI connection to the BME280
Installing the Arduino Library
We’ve created an Arduino library to get the BME280 operational with arduino IDE compatible boards. Before we get in to what the library does, obtain a copy of it.
Download the Github repository
Visit the GitHub repository to download the most recent version of the library, or click the link below:
Download the SparkFun BME280 Arduino Library
Use the library manager / install in the Arduino IDE
For help installing the library, check out our How To Install An Arduino Library tutorial.
If you don’t end up using the manger, you’ll need to move the SparkFun_BME280_Arduino_Library folder into a libraries folder within your Arduino sketchbook.
Functions of the Arduino Library
The basic library has the following parts
Construction
Example:
BME280 mySensor;
In the global scope, construct your sensor object (such as mySensor
or pressureSensorA
) without arguments.
Object parameters and setup()
Rather that passing a bunch of data to the constructor, configuration is accomplished by setting the values of the BME280 type in the setup()
function. They are exposed by being public:
so use the myName.aVarable = someValue;
syntax.
Settable variables of the class BME280:
uint8_t commInterface;
uint8_t I2CAddress;
uint8_t chipSelectPin;
uint8_t runMode;
uint8_t tStandby;
uint8_t filter;
uint8_t tempOverSample;
uint8_t pressOverSample;
uint8_t humidOverSample;
An example configuration setup()
language:c
#include <stdint.h>
#include "SparkFunBME280.h"
#include "Wire.h"
#include "SPI.h"
//Global sensor object
BME280 mySensor;
void setup()
{
//***Driver settings********************************//
//commInterface can be I2C_MODE or SPI_MODE
//specify chipSelectPin using arduino pin names
//specify I2C address. Can be 0x77(default) or 0x76
//For I2C, enable the following and disable the SPI section
mySensor.settings.commInterface = I2C_MODE;
mySensor.settings.I2CAddress = 0x77;
//For SPI enable the following and dissable the I2C section
//mySensor.settings.commInterface = SPI_MODE;
//mySensor.settings.chipSelectPin = 10;
//***Operation settings*****************************//
//runMode can be:
// 0, Sleep mode
// 1 or 2, Forced mode
// 3, Normal mode
mySensor.settings.runMode = 3; //Forced mode
//tStandby can be:
// 0, 0.5ms
// 1, 62.5ms
// 2, 125ms
// 3, 250ms
// 4, 500ms
// 5, 1000ms
// 6, 10ms
// 7, 20ms
mySensor.settings.tStandby = 0;
//filter can be off or number of FIR coefficients to use:
// 0, filter off
// 1, coefficients = 2
// 2, coefficients = 4
// 3, coefficients = 8
// 4, coefficients = 16
mySensor.settings.filter = 0;
//tempOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensor.settings.tempOverSample = 1;
//pressOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensor.settings.pressOverSample = 1;
//humidOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensor.settings.humidOverSample = 1;
delay(10); //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up. Serial.begin(57600);
Serial.print("Starting BME280... result of .begin(): 0x");
//Calling .begin() causes the settings to be loaded
Serial.println(mySensor.begin(), HEX);
}
begin();
In the above example, begin is used to start the sensor. Begin takes no arguments and returns ID word. The basic routine it follows is like this:
- Starts up the wiring library if necessary, though
#include "Wire.h"
and#include "SPI.h"
may be needed in your sketch. - Configures the CS pin as output if necessary.
- Concatenates the calibration words as specified by Bosch.
- Applies user settings to the configuration registers in the BME280.
- Returns the ID register (should read 0x60).
To use it, call mySensor.begin();
or assign the output to something like uint8_t myReturnedValue = mySensor.begin();
reset()
Send the reset word to the BME280. Afterwards, you’ll have to run begin() again.
Reset takes no arguments and returns void.
readTempC()
Use to get the temperature in Celsius, as a float. Takes no arguments and returns a floating point.
example: float myVar = mySensor.readTempC();
readTempF()
Use to get the temperature in Fahrenheit, as a float. Takes no arguments.
example: float myVar = mySensor.readTempF();
readFloatPressure()
Use to get pressure in units of kiloPascals, as a float. Takes no arguments.
example: float myVar = mySensor.readFloatPressure();
readFloatAltitudeMeters()
Use to get altitude in units of meters, as a float. This function calculates based off the measured pressure. Takes no arguments.
example: float myVar = mySensor.readFloatAltitudeMeters();
readFloatAltitudeFeet()
Use to get altitude in units of feet, as a float. This function calculates based off the measured pressure. Takes no arguments.
example: float myVar = mySensor.readFloatAltitudeFeet();
readFloatHumidity()
Use to get humidity in % relative, as a float. Takes no arguments.
example: float myVar = mySensor.readFloatHumidity();
Example Sketches
The examples are selectable from the drop-down menu in the arduino IDE, or they will run stand-alone if you put the contents of the libraries /src dirctory in with the example.ino file.
All of the examples default to 57600 baud.
I2C_and_SPI_Multisensor.ino
This example configures one BME280 on the SPI bus and another on the I2C bus. Then it gets the data and outputs from both sensors every second. If you only have 1 sensor connected the other channel reports garbage, so this can be a good troubleshooting and starting place.
Example output – shown is the configuration plus the first 3 sample readings
CSV_Output.ino
If you want to use the BME280 to record data as a function of time, this example is for you! It outputs text as CSV (comma separated vales) that can be copy-pasted into a textfile or spreadsheet app for graphing.
A note on accuracy: This sketch use “delay(50);” to wait 50ms between reads. The units of the ‘sample’ column are in (50ms + time-to-read) periods.
Example output – Shows the first few lines of the generated CSV.
In order to demonstrate the operation, the BME280 is connected with fine hookup wires that are then placed in a bottle and pressurized with breath.
The environmental test chamber! IT’S SCIENCE!
Data is collected from the event, and then a graph is made. To do this, the un-needed columns were deleted, and the pressure was scaled to kPa.
Example graph of pressure and humidity - shown after the data was loaded into OpenOffice Calc
I2C_ReadAllData.ino
Here’s an example that prints out the registers as well as the internally concatenated calibration words. It can be used to check the state of the BME280 after a particular configuration or can be implanted in your own sketch where you need to debug.
Example output – shows the full contents of memory, even those not specified in the datasheet
I2C_DeltaAltitude.ino
This example allows you to take measurements of change in altitude. It configures the BME280 with a lot of oversampling and also uses a software filter giving accurate but slow performance.
The sketch uses an additional button to zero the altitude. Push and hold until the average reaches zero.
After the sensor was zeroed out on the floor and moved to a desk hight, the output displays the rough height of the desk!
Product Video Sketches
The library also has a subfolder in the examples folder that contains the sketches used in the product video. They’re modifications of the basic examples with a LCD added on. They are not covered by this tutorial.
Resources and Going Further
Resources
- BME280 Product GitHub Repository– Your revision-controlled source for all things BME280. Here you’ll find our most up-to-date hardware layouts and code.
- Bosch BME280 Datasheet– This datasheet covers everything in one handy document.
Going Further
Hopefully this guide has gotten your BME280 operational. What will it become? Weather monitoring? Flight control on a quadcopter? Terrarium climate control?
To get you thinking, here are a few articles to browse.
- Weather Ballooning in the White Mountains
- SparkFun Data Service
- Enginursday: These Are Not the Drones You’re Looking For
Let us know what your BME280 becomes!
Check out these other great weather related tutorials.
Weather Shield Hookup Guide
Photon Weather Shield Hookup Guide
learn.sparkfun.com |CC BY-SA 3.0 | SparkFun Electronics | Niwot, Colorado