The BME280 is one of the handiest sensors for a home weather station: it measures temperature, humidity and pressure with just four wires. In this guide you connect it to the Raspberry Pi over I2C, check that the system sees it and log readings to a CSV with Python, working around the pip restriction in Bookworm.
In short
- Connect it to 3V3, GND, SDA (GPIO 2) and SCL (GPIO 3); common modules need no external resistors.
- After enabling I2C in
raspi-config,i2cdetect -y 1should show it at 0x76 or 0x77.- On Raspberry Pi OS Bookworm, libraries are installed inside a Python virtual environment.
What you need
- A Raspberry Pi with a 40-pin header (Pi 5, Pi 4, Pi 3 or Zero 2 W) and Raspberry Pi OS Bookworm.
- A BME280 breakout module with VCC (or VIN), GND, SCL and SDA pins. Some also have CSB and SDO.
- Four female-to-female jumper wires.
Wiring to the pins
| BME280 pin | Pi physical pin | Signal |
|---|---|---|
| VCC / VIN | 1 | 3.3 V |
| GND | 6 | Ground |
| SDA | 3 | GPIO 2 (I2C SDA) |
| SCL | 5 | GPIO 3 (I2C SCL) |
Three details that avoid grief:
- Power it with 3.3 V, not 5 V. Some modules have a regulator that accepts 5 V, but the Pi’s GPIOs work at 3.3 V and do not tolerate 5 V on SDA and SCL. With 3V3 you cannot go wrong.
- The Pi already has pull-up resistors on GPIO 2 and GPIO 3, and most modules include their own. Do not add more.
- Make the connections with the Pi powered off.
If your module has an SDO pin, it decides the address: tied to GND gives 0x76, to VCC gives 0x77. If you leave it unconnected, whatever the manufacturer set on the board applies.
Enable I2C
I2C is disabled by default. To enable it:
- Open the configuration tool:
sudo raspi-config
- Go to Interface Options and then I2C.
- Answer Yes to enable the interface.
- Exit with Finish. If it asks to reboot, accept.
On the desktop you can also do it from Preferences → Raspberry Pi Configuration → Interfaces.
Detect the sensor with i2cdetect
Install the I2C tools and scan bus 1, which is the one on pins 3 and 5:
sudo apt update
sudo apt install -y i2c-tools
i2cdetect -y 1

You will see a grid. If all goes well, a number appears in one cell:
0 1 2 3 4 5 6 7 8 9 a b c d e f
70: -- -- -- -- -- -- 76 --
Note that address; you will use it in the code. If the grid is empty, check the wiring before going on.
Trick: tell a BME280 from a BMP280
Many cheap modules are sold as BME280 but carry a BMP280, which does not measure humidity. They look the same at a glance. The chip’s 0xD0 register returns its ID:
i2cget -y 1 0x76 0xD0
| Returned value | Chip |
|---|---|
0x60 | BME280 (temperature, humidity and pressure) |
0x58 | BMP280 (temperature and pressure, no humidity) |
Replace 0x76 with 0x77 if your sensor uses that address.
Python virtual environment
Since Bookworm, pip install outside a virtual environment returns the externally-managed-environment error. It is not a bug: the system protects the Python packages managed by apt. Bypassing it with --break-system-packages works but can break system tools. The right way is a venv:
- Create the environment in your home folder:

python3 -m venv ~/bme280-env
- Activate it:
source ~/bme280-env/bin/activate
- Install the
RPi.bme280library, which usessmbus2to talk over I2C:
pip install RPi.bme280
The prompt shows (bme280-env) while the environment is active. Leave it with deactivate.
First reading
Create ~/bme280/leer.py:
import smbus2
import bme280
PUERTO = 1
DIRECCION = 0x76
bus = smbus2.SMBus(PUERTO)
calibracion = bme280.load_calibration_params(bus, DIRECCION)
datos = bme280.sample(bus, DIRECCION, calibracion)
print(f"Temperature: {datos.temperature:.1f} °C")
print(f"Humidity: {datos.humidity:.1f} %")
print(f"Pressure: {datos.pressure:.1f} hPa")
Run it with the environment active:
python ~/bme280/leer.py
The load_calibration_params function reads the coefficients each chip has stored at the factory. Without them, raw values mean nothing.
Save readings to a CSV
To log data over days, this script adds a row each time it runs and writes the header only if the file is new:
import csv
from datetime import datetime
from pathlib import Path
import smbus2
import bme280
DIRECCION = 0x76
ARCHIVO = Path.home() / "bme280" / "lecturas.csv"
bus = smbus2.SMBus(1)
calibracion = bme280.load_calibration_params(bus, DIRECCION)
datos = bme280.sample(bus, DIRECCION, calibracion)
nuevo = not ARCHIVO.exists()
with ARCHIVO.open("a", newline="") as f:
escritor = csv.writer(f)
if nuevo:
escritor.writerow(["date", "temperature_c", "humidity_pct", "pressure_hpa"])
escritor.writerow([
datetime.now().isoformat(timespec="seconds"),
round(datos.temperature, 2),
round(datos.humidity, 2),
round(datos.pressure, 2),
])
Use an absolute path (here, built from Path.home()) so it works the same from the terminal and from a scheduled task. To run it every 10 minutes with cron, call the virtual environment’s Python directly:
*/10 * * * * /home/your-user/bme280-env/bin/python /home/your-user/bme280/guardar.py
The CSV opens fine in a spreadsheet or with pandas.
BME280, BMP280 and DHT22 compared
| BME280 | BMP280 | DHT22 (AM2302) | |
|---|---|---|---|
| Temperature | Yes | Yes | Yes |
| Humidity | Yes | No | Yes |
| Pressure | Yes | Yes | No |
| Bus | I2C or SPI | I2C or SPI | Own one-wire protocol |
| Several on one bus | 2 (0x76 and 0x77) | 2 (0x76 and 0x77) | One per GPIO |
| Read rate | Fast | Fast | At most one reading every 2 s |
| Reliability on the Pi | High: I2C is handled by hardware | High | Medium: depends on precise timing and sometimes a read fails |
According to the datasheets, the BME280’s typical accuracy is ±1 °C in temperature, ±3% relative humidity and ±1 hPa pressure. The DHT22 claims slightly better temperature accuracy, but its one-wire protocol is fragile on Linux and produces failed readings you have to retry. If you need humidity, the BME280 is the most convenient; if you only care about pressure or altitude, a BMP280 is enough and cheaper.
Common problems
i2cdetectshows nothing. I2C is not enabled, SDA and SCL are swapped or a wire is not making contact. Also check you are using bus 1.- It appears at 0x77 and the code uses 0x76 (or the reverse). Typical error:
OSError: [Errno 121] Remote I/O error. ChangeDIRECCION. - Humidity is missing or stuck. It is probably a BMP280. Check the
0xD0register. - Temperature reads 2–3 °C above reality. The sensor is too close to the Pi or its case and gets its heat. Move it away with longer wires and do not enclose it with the board.
ModuleNotFoundError: No module named 'bme280'. You are running the systempython3instead of the virtual environment’s. Activate the venv or use the full path to itspython.- Erratic readings with long wires. I2C is designed for short distances. Over a meter, use twisted cable or bring the sensor closer.



