🛠 Lire un capteur I2C (température/pression)

Hardkernel ODROID-C2 (Amlogic S905) · intermediaire · Schéma : IMG-odroid-c2-intermediaire-2-odroidc2-i2c-capteur.svg — capteur BME280 relié au bus I2C A du connecteur J2 de l'ODROID-C2

Objectives

Enable the I2C bus on the ODROID-C2 (the i2c_A controller is present and named "I2C A SDA/SCK" on connector J2 in the mainline meson-gxbb-odroidc2 DTS), detect a sensor using i2cdetect, then read temperature and pressure in Python.

Hardware

Complete Code

# 1) activer I2C (armbian-config > overlays > i2c) puis :
sudo i2cdetect -y 1        # le bus I2C A ; lister d'abord : ls /dev/i2c-*
#!/usr/bin/env python3
"""bme280.py — lecture brute registre temperature (python3-smbus)."""
import smbus, time

BUS = smbus.SMBus(1)       # /dev/i2c-1 — ajuster selon i2cdetect
ADDR = 0x76                # 0x77 si PSB lié à VDD

# configuration capteur BMP280/BME280 (mode force)
BUS.write_byte_data(ADDR, 0xF4, 0x2E)
time.sleep(0.1)
msb = BUS.read_byte_data(ADDR, 0xFA)   # press_msb — enchaîne LSB/XLSB
lsb = BUS.read_byte_data(ADDR, 0xFB)
xlsb = BUS.read_byte_data(ADDR, 0xFC)
t_raw = ((msb << 12) | (lsb << 4) | (xlsb >> 4))
temp_c = (t_raw / 100.0) - 50.0        # approximation lineaire simplifiee
print(f"registre brut temperature: {t_raw}  (~{temp_c:.1f} C)")

Expected Verification

i2cdetect displays the sensor address (76 or 77) on the bus. The script prints a plausible value; for accurate physical readings, use a calibrated library (adafruit-bme280 or the kernel driver /sys/bus/iio/devices) rather than the raw approximation.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials