🛠 Lire un capteur BME280 en I2C sur le CM3

Raspberry Pi Compute Module 3 (CM3 / CM3L) · intermediaire · Schéma : capteur BME280 relié au bus I2C1 du CM3 sur carte CMIO3 — SDA sur GPIO2, SCL sur GPIO3, VCC 3,3 V, GND.

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials