🛠 Lire un capteur sur le bus I2C
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
- ODROID-N2/N2+ running Armbian
- BME280 I2C sensor (address 0x76 or 0x77) + 4 Dupont cables
- SDA/SCL pins on the connector (identify the I2C bus exposed on the header) >
sudo apt install i2c-tools python3-smbus
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
- I2C bus not enabled: no device detected on the header bus; enable the overlay and reboot.
- Wrong bus number: the header is not necessarily on i2c-1; test
i2cdetect -lthen check each bus. - Address 0x76 vs 0x77: depends on the module's SDO pin; read the module's silkscreen.
- Missing pull-ups on a bare sensor (not a module): floating lines, addresses jumping; add 4.7 kΩ resistors to 3.3 V.
- Conflict with the RTC: do not confuse 0x51 (PCF8563, internal bus) with your sensor.
- >
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash