🛠 Lire un capteur I2C (BME680)

Raspberry Pi 3 Model B (model-3b) · intermediaire · Schéma : IMG-model-3b-intermediaire-2-model3b-i2c-capteur.svg — capteur BME680 relié à I2C1 : SDA broche 3, SCL broche 5, 3V3 broche 1, GND broche 9, adresses 0x76/0x77 selon broche SDO

Objectives

Enable the hardware I2C bus on the Raspberry Pi 3 Model B, detect a BME680 environmental sensor (temperature, humidity, pressure), and read a raw measurement in Python using smbus2. Understand the role of I2C addresses (fixed by the chip: 0x76 or 0x77 depending on the SDO pin), the i2c-1 bus, and pull-up resistors.

Hardware

Complete Code

# 1) Activer I2C (ou via raspi-config : Interface Options > I2C)
sudo raspi-config nonint do_i2c 0
sudo reboot

# 2) Détecter le capteur sur le bus i2c-1
sudo apt install -y i2c-tools
i2cdetect -y 1        # doit afficher 76 ou 77
#!/usr/bin/env python3
"""bme680_brut.py — lecture brute de température sur BME680 (i2c-1).
   Séquence minimale en mode forced ; conversion complète : datasheet Bosch."""
import smbus2, time

BUS, ADDR = 1, 0x77          # vérifier avec i2cdetect -y 1
b = smbus2.SMBus(BUS)

b.write_byte_data(ADDR, 0xE8, 0x2C)  # ctrl_hum : suréchantillonnage x1
b.write_byte_data(ADDR, 0xF4, 0x24)  # config : t_sb=1000 ms, filtre off
b.write_byte_data(ADDR, 0xF2, 0x01)  # ctrl_meas : temp x1, pression off
time.sleep(1.0)

msb, lsb, xlsb = b.read_i2c_block_data(ADDR, 0xFA, 3)
adc_t = (msb << 12) | (lsb << 4) | (xlsb >> 4)   # ADC 20 bits
print(f"ADC brut température : {adc_t}")          # convertir via datasheet

Expected Verification

i2cdetect -y 1 displays a 76 or 77 entry (the rest showing --). The Python script displays a consistent raw ADC value (it increases when you warm the sensor with your finger). With i2cdump -y 1 0x77, the calibration registers (0x88–0xA1) are not all 0x00 — proof that the chip is responding. For ready-to-use physical values, use the bme680 or adafruit-circuitpython-bme680 library afterwards.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials