🛠 Premier capteur sur le bus I2C

Raspberry Pi 4 Model B (2019) — Board Sheet · intermediaire · Schéma : IMG-model-4b-intermediaire-2-model4b-i2c-capteur.svg — Bus I2C du Pi 4B : SDA (broche 3), SCL (broche 5), 3.3 V et GND vers un capteur BME680, résistances de pull-up

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials