AcademyTechnician

I2C Sensors: Read Temperature, Humidity and Pressure

Enable I2C, detect a BME280 sensor and read live data with Python.

LevelTechnician
Time30 min
Est. cost~$29
Steps5

I2C lets dozens of sensors share two wires (SDA and SCL). Learn it once and you can add displays, ADCs, IMUs and environmental sensors to any project.

Video tutorial

▶ Fun IncompleteWatch on YouTube ↗

Video by Fun Incomplete, embedded from YouTube. The written steps below are our own and match the parts list.

Mission steps

  1. Enable I2C

    Run sudo raspi-config → Interface Options → I2C → Enable, then reboot.

  2. Wire the sensor

    VIN to 3V3 (pin 1), GND to pin 6, SDA to pin 3, SCL to pin 5.

  3. Detect it

    Install i2c-tools and run i2cdetect -y 1. A BME280 appears at address 76 or 77.

  4. Install the library

    Inside your virtual environment install the Adafruit BME280 library.

  5. Read the data

    Run the script. Log the values to a file, a dashboard or the weather station project.

Code

sudo apt install -y i2c-tools
i2cdetect -y 1
source ~/venv/bin/activate
pip install adafruit-circuitpython-bme280
import time, board
from adafruit_bme280 import basic as adafruit_bme280

i2c = board.I2C()
bme = adafruit_bme280.Adafruit_BME280_I2C(i2c, address=0x76)  # or 0x77

while True:
    print(f"{bme.temperature:.1f} C  {bme.humidity:.0f}%  {bme.pressure:.1f} hPa")
    time.sleep(2)

Tip: test every script on the bench before installing it in a moving machine.

Questions

i2cdetect shows nothing

Check SDA/SCL are not swapped, the sensor is powered from 3V3, and I2C is enabled. Some cheap boards are BMP280 (no humidity).