🛠 Interroger un capteur I2C (BME280) sur le bus du 40 broches
Objectives
Enable the I2C bus on the Model B+ (I2C0 is available by default on the 40-pin connector), scan the bus with i2cdetect, then read the identification number of a BME280 sensor in Python — proving that the hardware/software chain works before using the measurements.
Hardware
- Raspberry Pi Model B+ running Raspberry Pi OS legacy 32-bit
- 1 BME280 (or BMP280) module with a 4-pin connector (VCC, GND, SCK/SCL, SDI/SDA)
- 4 Dupont wires; power the module with 3.3 V (pin 1), never with 5 V if the module is not 5 V tolerant
Complete code
# Activer I2C : raspi-config > Interfaces de connexion > I2C, ou :
echo "dtparam=i2c_arm=on" | sudo tee -a /boot/config.txt
sudo reboot
sudo apt install -y i2c-tools python3-smbus
i2cdetect -y 1 # le BME280 doit répondre à 0x76 (ou 0x77)
#!/usr/bin/env python3
"""Lecture de l'ID du BME280 sur i2c-1 — Model B+."""
import smbus
ADDR = 0x76 # 0x77 si l'adresse SDO est montée
bus = smbus.SMBus(1)
chip_id = bus.read_byte_data(ADDR, 0xD0)
print(f"BME280 chip id: 0x{chip_id:02X}") # attendu 0x60 (BMP280 : 0x58)
Expected verification
i2cdetect -y 1 displays a grid where the cell 76 is marked. The Python script prints “BME280 chip id: 0x60”. With a BMP280, 0x58 confirms a neighboring sensor; 0x55 or 0xFF indicates a power supply issue.
Common errors
- I2C not enabled or reboot forgotten: “Could not open bus: No such file or directory”.
- SDA/SCL swapped: i2cdetect shows nothing; on the B+ pins 3/5 are fixed to I2C0.
- Non-5V-tolerant module powered with 5 V while using 3.3 V logic: readings of 0xFF, or even a dead module.
- Address 0x77 wired but script using 0x76: adjust ADDR according to the i2cdetect scan.
- No onboard ADC: a raw analog sensor cannot be connected directly; it requires an I2C converter (PCF8591, ADS1115).
Other tutorials for this board
- Allumer une LED depuis la GPIO 17 avec libgpiod
- Lire un bouton et afficher les appuis dans la console
- Premier script Python : faire clignoter la LED
- Variation d'intensité d'une LED avec le PWM matériel (GPIO18)
- Mettre le B+ en réseau par Ethernet et piloter en SSH
- Amorçage microSD, pannes de boot et procédure de recouvrement
Code examples for this board
led_blink.pyPythonbutton_led.pyPythonservo_sweep.pyPythondht_temp.pyPythonoled_i2c.pyPythonmcp3008_read.pyPython