🛠 Lire un capteur I2C (BME680)
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
- Raspberry Pi 3 Model B running Raspberry Pi OS Lite
- BME680 sensor on a breakout module (or BME280, identical addresses 0x76/0x77)
- Wiring: SDA → physical pin 3, SCL → physical pin 5, VCC → 3.3 V (pin 1), GND → pin 9
- Packages:
i2c-tools,python3-smbus(orpip install smbus2)
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
- I2C not enabled:
i2cdetectreturns "Could not open bus"; enable it via raspi-config ordtparam=i2c_arm=onin config.txt, then reboot. - Wrong bus: the Pi 3B connector corresponds to i2c-1 (
-y 1), not i2c-0 (camera). - Swapping SDA/SCL: no response; swap the two wires.
- Powering the module with 5 V on VCC and reading 0x00: the I2C logic is 3.3 V, power the breakout with 3.3 V.
Address in useor sensor missing from scan: different SDO address (0x76 vs 0x77) — test both.- I2C wire too long or dirty breadboard: random bus behavior; breakout modules already include pull-up resistors.
Other tutorials for this board
Code examples for this board
led_blink.pyPythonbutton_led.pyPythonservo_sweep.pyPythondht_temp.pyPythonoled_i2c.pyPythonmcp3008_read.pyPython