🛠 Lire un capteur I2C (température/humidité)
Objectives
Enable and use the I2C bus on the ODROID-M1 to read an environmental sensor. The mainline device tree rk3568-odroid-m1.dts (compatible with “hardkernel,odroid-m1”, “rockchip,rk3568”) enables i2c0 and i2c3; the controller is the one from the RK3568B2.
Hardware
- ODROID-M1 running Armbian (mainline or vendor 4.19 kernel)
- 1 standard I2C sensor (BME280, BMP280, AHT20...) + 4 jumpers
- >
Complete Code
# Activer l'overlay I2C (Armbian) : éditer /boot/armbianEnv.txt
# overlays=i2c3
sudo reboot
# Inventaire des contrôleurs et détection du capteur
ls /dev/i2c-*
sudo apt-get install -y i2c-tools
sudo i2cdetect -y 3 # le capteur apparaît p. ex. à l'adresse 0x76
#!/usr/bin/env python3
"""Lecture brute d'un registre de capteur via /dev/i2c-N (smbus)."""
import smbus
bus = smbus.SMBus(3) # /dev/i2c-3
ADDR = 0x76 # adresse révélée par i2cdetect
who = bus.read_byte_data(ADDR, 0xD0) # ID puce BME280 : 0x60
print(f"Chip ID : 0x{who:02x}")
msb, lsb, xlsb = (bus.read_byte_data(ADDR, 0xFA + i) for i in range(3))
print("ADC brut température :", (msb << 12) | (lsb << 4) | (xlsb >> 4))
Expected Verification
i2cdetect displays the sensor address; the Python script reads a consistent chip ID (0x60 for a BME280) and ADC conversions that vary when warming the sensor with your finger.
Common Errors
- I2C overlay not enabled: no
/dev/i2c-*devices appear. - Wrong bus: try
i2cdetecton each number (0 and 3 depending on the overlay). - Sensor powered at 5 V on a bus designed for 3.3 V: check the pull-up voltage.
- SDA/SCL swapped:
i2cdetectshows nothing, no component responds.
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash