Pressure and temperature reading from BMP280 sensor on Raspberry Pi

Tomasz MeyerTomasz Meyer
4 min read

Raspberry OS is installed, everything booted up — now it’s time to breathe some life and meaning into this piece of silicon with an operating system. The reasons for starting this project, apart from learning the Raspberry platform with a long-term plan of mastering Yocto, included monitoring weather conditions — especially atmospheric pressure. I’d like to keep a record of pressure in the places where I live to try and confirm or disprove my observations — my mood seems to fluctuate along with the pressure. Atmospheric pressure data should be available online, but I wasn’t able to find a site with very local data and easy access to historical records. In my opinion, learning for the sake of learning is pointless — personally, I find it hard to stay motivated if there’s no specific goal behind it. If there’s a strong desire to learn but no goal — find one. Let’s treat this goal as my motivator for learning — without diving into the details of whether the whole project even makes sense.

What are my assumptions about the sensor itself? Very simple - it has to be cheap, measure pressure, it would be nice if it also measured temperature, and it has to be widely used - I don't want to write controllers from scratch. I settled on the BMP280 sensor. It meets all the requirements!

The sensor can measure pressure from 300 to 11000 hPa and temperature from -40 to 85 °C. I can communicate with it via I2C and SPI interfaces. Libraries, examples and development boards are available.

BME280 sensor

I will connect the sensor to the Raspberry via the I2C bus, which is a serial and synchronous bus using two communication lines:

  • SCL - the clock line, which, like a conductor, sets the rythm of communication,

  • SDA - the line on which the actual communication takes place.

Besides the communication lines, the GND (ground) line must be connected — you need to know the reference point against which to measure the potentials on the lines.

The sensor needs to know which bus to use - there is a choice of I2C and SPI buses. The CSB line is used to select the bus: a logic 1 (3.3 V) state selects the I2C bus, while a logic 0 (0 V) state selects the SPI bus. Below I show the sensor connection from my Raspberry Pi to the I2C1 interface.

Raspberry Pi Pinout Guide: How to use the Raspberry Pi GPIOs ...

BMP280Raspberry
VCC01
GND39
SCL05
CDA03
CSB17
SDONC

After connecting everything, we need to enable the I2C bus. We can do this in two ways — through the graphical interface:

Or via the command line:

sudo raspi-config

I had some minor issues getting the sensor started, so I’d suggest rebooting the Raspberry after enabling the interface.

The next step should be to verify if we connected the sensor correctly and if we can find its address. For that, we’ll need the tool i2cdetect.

sudo apt update
sudo apt install -y i2c-tools

We connected the device to the I2C1 bus — dedicated for connecting external sensors. I2C0 is reserved for the Raspberry hat with EEPROM. To find the sensor on the I2C1 bus, use the following command:

i2cdetect -y 1

Success! The sensor is available at address 0x76!

So let’s write some simple code to read the sensor’s parameters: pressure and temperature! Let’s create a project folder, start a virtual environment, install a ready-made Python library, and create a script file:

mkdir temppressread
cd temppressread/
python -m venv temppressread
source temppressread/bin/activate
pip install bmp280
touch run.py

Using the examples available on the library’s website, let’s create a simple script to read temperature and pressure, then run it!

#!/usr/bin/env python

import time
from smbus2 import SMBus
from bmp280 import BMP280
from datetime import datetime

# Initialise the BMP280
bus = SMBus(1)
bmp280 = BMP280(i2c_dev=bus)

while True:
    now = datetime.now()
    formatted = now.strftime("%y-%m-%d %H:%M:%S")
    temperature = bmp280.get_temperature()
    pressure = bmp280.get_pressure()
    print(formatted,f" -  {temperature:05.2f}*C {pressure:05.2f}hPa")
    time.sleep(1)

The sensor and the script are working! Next, I’ll try installing Home Assistant on the Raspberry and somehow connect this data so I can track it with nice charts and monitor the historical data!

0
Subscribe to my newsletter

Read articles from Tomasz Meyer directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tomasz Meyer
Tomasz Meyer

With 8+ years in embedded systems, I specialize in firmware development for ARM-based microcontrollers and wireless communication. I combine corporate best practices with startup agility, leading projects from concept to deployment.