🛠 Premier capteur sur le bus I2C
Objectives
Enable the I2C bus, scan the bus with i2cdetect, then read an environmental sensor from Python using smbus2. The BCM2711 exposes up to 6 I2C controllers; i2c-1 (pins 3 and 5) is the standard user bus.
Hardware
- Raspberry Pi 4B + BME680 (or BME280) sensor on a breakout board
- 4.7 kOhm pull-up resistors if the breakout board does not have them
- sudo apt install i2c-tools python3-smbus2
Complete code
# Activer I2C : sudo raspi-config -> Interface Options -> I2C (ou dtoverlay=i2c-rtc en plus)
sudo i2cdetect -y 1 # balayage du bus i2c-1
#!/usr/bin/env python3
# lecture_i2c.py - lire l'identifiant de puce du BME680
import smbus2
ADDR = 0x76 # 0x76 ou 0x77 selon la broche SDO du module
#
bus = smbus2.SMBus(1)
chip_id = bus.read_byte_data(ADDR, 0xD0)
print(f"Chip ID 0x{chip_id:02X} (BME680 attendu : 0x61)")
Expected verification
i2cdetect -y 1 displays a table; one cell (often 76 or 77) appears: this is the sensor. The script displays Chip ID 0x61 for an authentic BME680. No cell appears: the bus is inactive, SDA/SCL wiring is reversed, or the module is not powered.
Common errors
- Forgetting to enable I2C in raspi-config and then rebooting: i2cdetect responds with Could not open bus.
- Reversing SDA (pin 3) and SCL (pin 5): nothing appears in the scan.
- Powering the sensor with 5 V on a bus expected to be 3.3 V: check the breakout board documentation.
- Wrong address (0x76 vs 0x77): the i2cdetect scan provides the correct one.
- No pull-up resistors on long cables: lines do not rise, resulting in random readings.
Other tutorials for this board
- Allumer une LED via le GPIO avec libgpiod
- Bouton poussoir et messages dans la console
- Premier script Python : rapport d'information système
- Variateur d'intensité : faire varier une LED en PWM matériel
- Gigabit Ethernet natif et administration à distance en SSH
- Booter sur NVMe/USB et procédure de recouvrement
Code examples for this board
led_blink.pyPythonbutton_led.pyPythonservo_sweep.pyPythondht_temp.pyPythonoled_i2c.pyPythonmcp3008_read.pyPython