Make a Raspberry Pi weather station with the BME280

Adafruit BME280 temperature, humidity and pressure sensor breakout

Level: Beginner  ·  Time: 30 minutes  ·  Board: any Raspberry Pi with a 40-pin header

The BME280 measures temperature, humidity and barometric pressure, the three basics of a weather station. It talks over I2C, so it needs just four wires and no soldering if you use STEMMA QT cables.

What you need

1. Wire it up

BME280 Raspberry Pi pin
VIN Pin 1 (3.3V)
GND Pin 6 (GND)
SCK Pin 5 (GPIO 3, SCL)
SDI Pin 3 (GPIO 2, SDA)

Tip: Keep the sensor a few inches away from the Pi on its wires. The Pi's own heat will otherwise push the temperature reading up.

2. Turn on I2C and find the sensor

sudo raspi-config nonint do_i2c 0
sudo apt install -y i2c-tools
i2cdetect -y 1

You should see 77 in the grid, which is the BME280's default address.

3. Install the library

Raspberry Pi OS asks you to install Python packages in a virtual environment:

python3 -m venv ~/weather --system-site-packages
source ~/weather/bin/activate
pip install adafruit-circuitpython-bme280

4. Read the sensor

Save this as weather.py:

import csv, time
from datetime import datetime
import board
from adafruit_bme280 import basic as adafruit_bme280

i2c = board.I2C()
bme = adafruit_bme280.Adafruit_BME280_I2C(i2c)
bme.sea_level_pressure = 1013.25   # set to your local value for accurate altitude

with open("weather.csv", "a", newline="") as f:
    log = csv.writer(f)
    while True:
        row = [datetime.now().isoformat(timespec="seconds"),
               round(bme.temperature, 1),
               round(bme.relative_humidity, 1),
               round(bme.pressure, 1)]
        print(f"{row[1]} °C  {row[2]} %RH  {row[3]} hPa")
        log.writerow(row)
        f.flush()
        time.sleep(60)
python weather.py

You'll get a reading every minute on screen and in weather.csv, which opens straight into a spreadsheet for graphs.

5. Run it automatically

To start logging at boot, add a line to your crontab (crontab -e):

@reboot cd /home/yourname && /home/yourname/weather/bin/python weather.py

Ideas to take it further

  • Add the OLED display for a desk readout. It shares the same I2C pins.
  • Add a SCD-40 to track CO₂ and know when to open a window.
  • Publish the readings to Home Assistant over MQTT.