🛠 Lire un capteur I2C (Weather Board Zero)
Objectives
Enable the I2C bus on the ODROID-C4 under Armbian mainline and read a BME680-type environmental sensor (Hardkernel Weather Board Zero, ~$2): address detection with i2cdetect, register reading in Python via smbus, and declarative activation of overlays in armbianEnv.txt.
Hardware
- ODROID-C4 running Armbian current (kernel 6.6)
- Weather Board Zero (or any BME-class sensor in an I2C module) — 40-pin connector, 3.3 V logic
sudo apt install -y i2c-tools python3-smbus device-tree-compiler- >
Full code
# 1) Activer I2C (Armbian) puis redémarrer
sudo sed -i 's/^#*overlay_add=.*/overlay_add=i2c0/' /boot/armbianEnv.txt
sudo reboot
# 2) Détecter le capteur
i2cdetect -l # lister les bus
i2cdetect -y 0 # le capteur répond à 0x76 ou 0x77
#!/usr/bin/env python3
# lecture_bme.py - CHIP_ID + température brute (démo non calibrée)
import smbus, time
BUS, ADDR = 0, 0x77 # adapter selon i2cdetect
bus = smbus.SMBus(BUS)
chip_id = bus.read_byte_data(ADDR, 0xD0)
assert chip_id == 0x61, f"chip id inattendu {chip_id:#x}"
def read_reg24(reg):
msb, lsb, xlsb = bus.read_i2c_block_data(ADDR, reg, 3)
return (msb << 12) | (lsb << 4) | (xlsb >> 4)
while True:
t = read_reg24(0x24) / 16384.0 - 50.0
print(f"température brute ≈ {t:6.2f} °C (CHIP_ID={chip_id:#04x})")
time.sleep(2)
Expected verification
i2cdetect -y 0 displays a grid with a non-empty cell at 0x76 or 0x77. The Python script prints a temperature line and the CHIP_ID 0x61 every 2 seconds; Ctrl+C stops it cleanly. The raw value is approximate (demonstration without BME680 calibration table) but should change when you warm the sensor with your finger.
Common errors
OSError: [Errno 121] Remote I/O Error: wrong address or bus — recheck thei2cdetectoutput, test-y 1.- No bus listed: the
i2c0overlay is not enabled in/boot/armbianEnv.txtor the machine has not rebooted. - Trying to read the connector's ADCs at the same time: ADC inputs are limited to 1.8 V maximum, not 3.3 V!
- Plugging in the Weather Board while powered on: turn off the power before connecting a module to the 40-pin connector.
- >
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash