🛠 Lire un capteur BME280 en I2C sur le CM3
Objectives
Enable and use the CM3's I2C bus (the BCM2837 exposes two on the SODIMM) to read a BME280 temperature/humidity/pressure sensor. You will learn how to enable the bus via config.txt, scan the bus with i2cdetect, and read registers in Python using smbus. Note: the CM3 has no internal ADC (datasheet RP-008161-DS §2) — any analog sensor requires an external converter over I2C or SPI.
Hardware
- CM3 on CMIO3, Raspberry Pi OS booted
- BME280 sensor (I2C, address 0x76 or 0x77 depending on the SDO pin)
- Wires: SDA to GPIO2, SCL to GPIO3, VCC to 3.3 V, GND (3.3 V logic if bank0 is set to VREF 3.3 V as on the CMIO3)
Complete code
# 1. Activer I2C : ajouter dans /boot/firmware/config.txt (ou via raspi-config)
# dtparam=i2c_arm=on
sudo reboot
# 2. Outils et scan du bus
sudo apt install -y i2c-tools python3-smbus
sudo i2cdetect -y 1 # doit afficher 76 (ou 77)
#!/usr/bin/env python3
# bme280.py — lecture brute des registres
import time, smbus
BUS = smbus.SMBus(1) # /dev/i2c-1 (contrôleur « arm »)
ADDR = 0x76
BUS.write_byte_data(ADDR, 0xF4, 0x2F) # ctrl_meas : temp+pression, oversampling x16
time.sleep(0.1)
hi, lo = BUS.read_i2c_block_data(ADDR, 0xFA, 2) # regs 0xFA/0xFB : température 20 bits
print('code brut température :', ((hi << 8) | lo) >> 4)
# En pratique, utiliser la couche haute niveau (paquet pip bmp280 par exemple).
Expected verification
sudo i2cdetect -y 1 displays the sensor's address 76 (or 77). The script prints a raw code that changes when you warm the sensor with your finger. With the high-level library, temperature, pressure, and humidity are displayed in physical units.
Common errors
- Forgetting
dtparam=i2c_arm=onand rebooting:/dev/i2c-1does not exist, smbus raises a FileNotFoundError. - Address 0x77 not found: the sensor module's SDO pin is wired incorrectly (some modules change the default address).
- Powering the sensor with 5 V on a 1.8 V GPIO bank: I2C pins are tolerant to their bank's voltage, not beyond it — check the VREF rail.
- Confusing i2c0 (GPIO0/1) and i2c_arm (GPIO2/3): the "arm" bus corresponds to pins 2/3 on the CMIO3.
- Expecting a direct analog sensor: without an ADC on the SoC, use an MCP3008 (SPI) or a digital sensor.
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash