Adafruit BMP280 I2C or SPI Barometric Pressure & Altitude Sensor

Overview:

sensors_2651_iso_ORIG

Bosch has stepped up their game with their new BMP280 sensor, an environmental sensor with temperature, barometric pressure that is the next generation upgrade to the BMP085/BMP180/BMP183. This sensor is great for all sorts of weather sensing and can even be used in both I2C and SPI!
This precision sensor from Bosch is the best low-cost, precision sensing solution for measuring barometric pressure with ±1 hPa absolute accuraccy, and temperature with ±1.0°C accuracy. Because pressure changes with altitude, and the pressure measurements are so good, you can also use it as an altimeter with ±1 meter accuracy.

2651-00

The BME280 is the next-generation of sensors from Bosch, and is the upgrade to the BMP085/BMP180/BMP183 - with a low altitude noise of 0.25m and the same fast conversion time. It has the same specifications, but can use either I2C or SPI. For simple easy wiring, go with I2C. If you want to connect a bunch of sensors without worrying about I2C address collisions, go with SPI.

sensors_2651_kit_ORIG

Pinouts:

sensors_pinout

Power Pins:

  • Vin - this is the power pin. Since the sensor chip uses 3 VDC, we have included a voltage regulator on board that will take 3-5VDC and safely convert it down. To power the board, give it the same power as the logic level of your microcontroller - e.g. for a 5V micro like Arduino, use 5V.
  • 3Vo - this is the 3.3V output from the voltage regulator, you can grab up to 100mA from this if you like.
  • GND - common ground for power and logic.

SPI Logic Pins:

All pins going into the breakout have level shifting circuitry to make them 3-5V logic level safe. Use whatever logic level is on Vin!

  • SCK - This is the SPI Clock pin, its an input to the chip.
  • SDO- this is the Serial Data Out / Master In Slave Out pin, for data sent from the BMP183 to your processor.
  • SDI - this is the Serial Data In / Master Out Slave In pin, for data sent from your processor to the BME280.
  • CS - this is the Chip Select pin, drop it low to start an SPI transaction. Its an input to the chip.

If you want to connect multiple BME280's to one microcontroller, have them share the SDI, SDO and SCK pins. Then assign each one a unique CS pin.

I2C Logic pins:

  • SCK- this is also the I2C clock pin, connect to your microcontrollers I2C clock line.
  • SDI - this is also the I2C data pin, connect to your microcontrollers I2C data line.

Leave the other pins disconnected

Assembly:

sensors_2651_kit_ORIG-1
Prepare the header strip:
sensors_header
Cut the strip to length if necessary. It will be easier to solder if you insert it into a breadboard - long pins down

Add the breakout board:

sensors_place
Place the breakout board over the pins so that the short pins poke through the breakout pads

And Solder

Be sure to solder all pins for reliable electrical contact.

(For tips on soldering, be sure to check out our Guide to Excellent Soldering).

sensors_solder2

You're done! Check your solder joints visually and continue onto the next steps
sensors_done

Wiring And Test

Arduino Test
You can easily wire this breakout to any microcontroller, we'll be using an Arduino. For another kind of microcontroller, as long as you have 4 available pins it is possible to 'bit-bang SPI' or you can use two I2C pins, but usually those pins are fixed in hardware. Just check out the library, then port the code.

I2C Wiring
Use this wiring if you want to connect via I2C interface

  • Connect Vin to the power supply, 3-5V is fine. Use the same voltage that the microcontroller logic is based off of. For most Arduinos, that is 5V.
  • Connect GND to common power/data ground
  • Connect the SCK pin to the I2C clock SCL pin on your Arduino. On an UNO & '328 based Arduino, this is also known as A5, on a Mega it is also known as digital 21 and on a Leonardo/Micro, digital 3
  • Connect the SDI pin to the I2C data SDA pin on your Arduino. On an UNO & '328 based Arduino, this is also known as A4, on a Mega it is also known as digital 20 and on a Leonardo/Micro, digital 2
    sensors_2651_02

SPI wiring
Since this is a SPI-capable sensor, we can use hardware or 'software' SPI. To make wiring identical on all Arduinos, we'll begin with 'software' SPI. The following pins should be used:

  • Connect Vin to the power supply, 3V or 5V is fine. Use the same voltage that the microcontroller logic is based off of. For most Arduinos, that is 5V
  • Connect GND to common power/data ground
  • Connect the SCK pin to Digital #13 but any pin can be used later
  • Connect the SDO pin to Digital #12 but any pin can be used later
  • Connect the SDI pin to Digital #11 but any pin can be used later
  • Connect the CS pin Digital #10 but any pin can be used later

Later on, once we get it working, we can adjust the library to use hardware SPI if you desire, or change the pins to other

sensors_2651

Download Adafruit BMP280library

To begin reading sensor data, you will need to download Adafruit_BMP280 from our github repository. You can do that by visiting the github repo and manually downloading or, easier, just click this button to download the zip

Download Adafruit BMP280 Library

Rename the uncompressed folder Adafruit_BMP280 and check that the Adafruit_BMP280 folder contains Adafruit_BMP280.cppand Adafruit_BMP280.h

Place the Adafruit_BMP280 library folder your arduinosketchfolder/libraries/ folder.
You may need to create the libraries subfolder if its your first library. Restart the IDE.

We also have a great tutorial on Arduino library installation at:
http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use

Load Demo

Open up File->Examples->Adafruit_BMP280->bmp280test and upload to your Arduino wired up to the sensor

sensors_bmptest

Depending on whether you are using I2C or SPI, change the pin names and comment or uncomment the following lines.

#define BMP_SCK 13
#define BMP_MISO 12
#define BMP_MOSI 11 
#define BMP_CS 10

Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
//Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);

Once uploaded to your Arduino, open up the serial console at 9600 baud speed to see data being printed out

sensors_test

Temperature is calculated in degrees C, you can convert this to F by using the classic F = C * 9/5 + 32 equation.

Pressure is returned in the SI units of Pascals. 100 Pascals = 1 hPa = 1 millibar. Often times barometric pressure is reported in millibar or inches-mercury. For future reference 1 pascal =0.000295333727 inches of mercury, or 1 inch Hg = 3386.39 Pascal. So if you take the pascal value of say 100734 and divide by 3389.39 you'll get 29.72 inches-Hg.

You can also calculate Altitude. However, you can only really do a good accurate job of calculating altitude if you know the hPa pressure at sea level for your location and day! The sensor is quite precise but if you do not have the data updated for the current day then it can be difficult to get more accurate than 10 meters.

Library Reference

You can start out by creating a BMP280 object with either software SPI (where all four pins can be any I/O) using
Download file
Copy Code

Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);

Or you can use hardware SPI. With hardware SPI you must use the hardware SPI pins for your Arduino - and each arduino type has different pins! Check the SPI reference to see what pins to use.

In this case, you can use any CS pin, but the other three pins are fixed
Download file
Copy Code

Adafruit_BMP280 bmp(BMP_CS); // hardware SPI

or I2C using the default I2C bus, no pins are assigned
Download file
Copy Code

Adafruit_BMP280 bmp; // I2C

Once started, you can initialize the sensor with
Download file
Copy Code

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

begin() will return True if the sensor was found, and False if not. If you get a False value back, check your wiring!
Reading temperature and pressure is easy, just call
Download file
Copy Code

bmp.readTemperature()
bmp.readPressure()

Temperature is always a floating point, in Centigrade. Pressure is a 32 bit integer with the pressure in Pascals. You may need to convert to a different value to match it with your weather report.
It's also possible to turn the BMP280 into an altimeter. If you know the pressure at sea level, the library can calculate the current barometric pressure into altitude

Document

Datasheet for the BMP280 sensor used in the breakout
Arduino BMP280 Driver

Circuit Python Test

It's easy to use the BMP280 sensor with CircuitPython and the Adafruit CircuitPython BMP280 module. This module allows you to easily write Python code that reads the temperature and pressure from the sensor.
First wire up a BMP280 to your board exactly as shown on the previous pages for Arduino. You can use either I2C or SPI wiring, although it's recommended to use I2C for simplicity. Here's an example of wiring a Feather M0 to the sensor with I2C:
adafruit_products_m0_bmp280_i2c_bb

  • Board 3V to sensor VIN
  • Board GND to sensor GND
  • Board SCL to sensor SCK
  • Board SDA to sensor SDI

And an example of a Feather M0 wired with hardware SPI:

adafruit_products_m0_bmp280_spi_bb

  • Board 3V to sensor VIN
  • Board GND to sensor GND
  • Board SCK to sensor SCK
  • Board MOSI to sensor SDI
  • Board MISO to sensor SDO
  • Board D5 to sensor CS (or use any other free digital I/O pin)

Next you'll need to install the Adafruit CircuitPython BMP280 library on your CircuitPython board. Remember this module is for Adafruit CircuitPython firmware and not MicroPython.org firmware!

First make sure you are running the latest version of Adafruit CircuitPython for your board.

Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle. For example the Circuit Playground Express guide has a great page on how to install the library bundle for both express and non-express boards.

Remember for non-express boards like the Trinket M0, Gemma M0, and Feather/Metro M0 basic you'll need to manually install the necessary libraries from the bundle:
adafruit_bmp280.mpy
adafruit_bus_device
You can also download the adafruit_bmp280.mpy from its releases page on Github.

Before continuing make sure your board's lib folder or root filesystem has the adafruit_bmp280.mpy, and adafruit_bus_device files and folders copied over.

Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

Usage

To demonstrate the usage of the sensor we'll initialize it and read the temperature, humidity, and more from the board's Python REPL.
If you're using an I2C connection run the following code to import the necessary modules and initialize the I2C connection with the sensor:

Download file

import board
import busio
import adafruit_bmp280
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_bmp280.Adafruit_BMP280_I2C(i2c)

Remember if you're using a board that doesn't support hardware I2C (like the ESP8266) you need to use the bitbangio module instead:
Download file

import board
import bitbangio
import adafruit_bmp280
i2c = bitbangio.I2C(board.SCL, board.SDA)
sensor = adafruit_bmp280.Adafruit_BMP280_I2C(i2c)

Or if you're using a SPI connection run this code instead to setup the SPI connection and sensor:
Download file

import board
import busio
import digitalio
import adafruit_bmp280
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
cs = digitalio.DigitalInOut(board.D5)
sensor = adafruit_bmp280.Adafruit_BMP280_SPI(spi, cs)

Now you're ready to read values from the sensor using any of these properties:

  • temperature - The sensor temperature in degrees Celsius.
  • pressure - The pressure in hPa.
  • altitude - The altitude in meters.

For example to print temperature and pressure:
Download file

print('Temperature: {} degrees C'.format(sensor.temperature))
print('Pressure: {}hPa'.format(sensor.pressure))

adafruit_products_Screen_Shot_2017-11-10_at_3.33.47_PM

For altitude you'll want to set the pressure at sea level for your location to get the most accurate measure (remember these sensors can only infer altitude based on pressure and need a set calibration point). Look at your local weather report for a pressure at sea level reading and set the seaLevelhPA property:

Download file

sensor.seaLevelhPa = 1013.4

Then read the altitude property for a more accurate altitude reading (but remember this altitude will fluctuate based on atmospheric pressure changes!):

Download file

print('Altitude: {} meters'.format(sensor.altitude))

adafruit_products_Screen_Shot_2017-11-10_at_1.35.20_PM

That's all there is to using the BMP280 sensor with CircuitPython

Using the Adafruit BMP085 Python Library

The BMP085 Python code for Pi is available on Github at https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code

While many of these drivers and classes are works in progress -- we're still trying to figure out how we can make accessing HW as painless as possible on the Pi -- the current code should serve as a good starting point to understanding how you can access SMBus/I2C devices with your Pi, and getting some basic data out of your BMP085.

Downloading the Code from Github
The easiest way to get the code onto your Pi is to hook up an Ethernet cable, and clone it directly using 'git', which is installed by default on most distros. Simply run the following commands from an appropriate location (ex. "/home/pi"):

Download file

$ git clone https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git
$ cd Adafruit-Raspberry-Pi-Python-Code
$ cd Adafruit_BMP085 

raspberry_pi_GitClone

Testing the Library

If you're using a version 2 Pi (512 M) then you'll have to change the I2C bus as it flipped from #0 to #1 in the version 2.
Edit Adafruit_I2C.py with nano Adafruit_I2C.py and change this line

def __init__(self, address, bus=smbus.SMBus(0), debug=False): 
to
def __init__(self, address, bus=smbus.SMBus(1), debug=False)

Once the code has be downloaded to an appropriate folder, and you have your BMP085 properly connected, you can start reading some data via the following command (the driver includes a simple demo program):

Download file

sudo python Adafruit_BMP085_example.py

Which should give you something similar to the following
raspberry_pi_BMP085Example_Update

Modifying the Code

The BMP085 library is organized as two seperate classes. There is one class to handle the low-level SMBus/I2C calls (Adafruit_I2C), and another class that handles the BMP085-specific functionality.

The library includes the basic example shown above, but you can also customize the code a bit to provide full debug output if you're having any problems, change the address, or use the BMP085 in one of it's four different modes (ULTRALOWPOWER, STANDARD, HIRES, and ULTRAHIRES), as seen in the commented out initializors in the sample code below:

Download file

#!/usr/bin/python

from Adafruit_BMP085 import BMP085

# ===========================================================================
# Example Code
# ===========================================================================

# Initialise the BMP085 and use STANDARD mode (default value)
# bmp = BMP085(0x77, debug=True)
bmp = BMP085(0x77)

# To specify a different operating mode, uncomment one of the following:
# bmp = BMP085(0x77, 0)  # ULTRALOWPOWER Mode
# bmp = BMP085(0x77, 1)  # STANDARD Mode
# bmp = BMP085(0x77, 2)  # HIRES Mode
# bmp = BMP085(0x77, 3)  # ULTRAHIRES Mode

temp = bmp.readTemperature()
pressure = bmp.readPressure()
altitude = bmp.readAltitude()

print "Temperature: %.2f C" % temp
print "Pressure:    %.2f hPa" % (pressure / 100.0)
print "Altitude:    %.2f" % altitude