🛠 Lire un capteur sur le bus I2C

ODROID-N2 / ODROID-N2+ (Hardkernel) · intermediaire · Schéma : IMG-odroid-n2-intermediaire-2-odroidn2-i2c-capteur.svg — Capteur température/humidité I2C relié aux lignes SDA et SCL du connecteur 40 broches de l'ODROID-N2 avec résistances de pull-up, adresses listées par i2cdetect.

Objectives

Connect a classic I2C sensor (e.g., BME280 or BMP280, temperature/humidity/pressure) to the ODROID-N2 and read its measurements in Python. The 40-pin connector exposes 2 I2C buses (CNX specifications); the internal PCF8563 RTC already occupies I2C3, which serves as an indicator of a functional bus.

Hardware

Complete Code

# 1. Activer I2C (Armbian : armbian-config > System > Hardware, ou overlays=
#    i2c0/i2c1 dans le config). 2. Scanner le bus :
sudo i2cdetect -y 0     # bus 0 — le RTC PCF8563 doit apparaître en 51
sudo i2cdetect -y 1     # bus 1 — le capteur doit apparaître en 76 ou 77
#!/usr/bin/env python3
"""bme280.py — lecture brute des registres température/pression (bus 1)."""
import smbus, time

BUS = smbus.SMBus(1); ADDR = 0x76
BUS.write_byte_data(ADDR, 0xD0, 0x00)          # soft reset
BUS.write_byte_data(ADDR, 0xF4, 0x27)          # capteur forcé, x16/x1
BUS.write_byte_data(ADDR, 0xF5, 0xA0)          # mode forcé

def read2(reg):
    l, h = BUS.read_i2c_block_data(ADDR, reg, 2)
    return (h << 8) | l

# Calibration (registres 0x88..0xA1, tronqué pour l'exemple)
t1 = read2(0x88); p1 = read2(0x8E)
while True:
    BUS.write_byte_data(ADDR, 0xF4, 0x27)
    time.sleep(0.1)
    adc_t = int.from_bytes(BUS.read_i2c_block_data(ADDR, 0xFA, 3), 'big') >> 6
    temp = adc_t / 5120.0 - 50.0               # approximation datasheet
    print(f"T ≈ {temp:5.1f} °C  (adc_t={adc_t}, t1={t1}, p1={p1})")
    time.sleep(2)

Expected Verification

i2cdetect -y 1 displays 76 (or 77) instead of dots. The script prints a temperature consistent with the room (15–30 °C) every 2 s. i2cdetect -y 0 shows 51: the embedded PCF8563 RTC, proving that the I2C controller is working.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials