🛠 Lire un capteur I2C (température/pression)
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
- ODROID-C2 running Armbian/mainline
- 3.3 V I2C sensor (BME280 or BMP280), modules with integrated pull-ups
- Wiring SDA/SCL to the I2C A pins on connector J2 + 3.3 V + GND (never use 5 V on I2C)
- Packages i2c-tools and python3-smbus
- >
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
- Wrong bus: the I2C A number is not always /dev/i2c-1; check with ls /dev/i2c-* and i2cdetect -l.
- Missing pull-ups: without resistors to 3.3 V (many modules have them), the bus never goes high.
- Powering the sensor with 5 V and wiring SDA/SCL to the board: risk to the 3.3 V pins if the module is not level-shifted.
- Address 0x76/0x77 inverted depending on the SDO pin.
- I2C overlay not enabled: /dev/i2c-* missing initially on Armbian.
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash