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

ODROID-M1 (Hardkernel) · intermediaire · Schéma : IMG-odroid-m1-intermediaire-2-odroidm1-i2c-capteur.svg — petit capteur I2C à 4 broches relié au bus SDA/SCL du connecteur 40 broches de l'ODROID-M1 avec résistances de pull-up

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials