🛠 Lire un capteur I2C (température)
Objectives
Enable and use an I2C bus on the ODROID-XU4: the 12-pin header (next to the 30-pin header) is described in the datasheet as providing GPIO, I2S, and I2C. You will scan the bus with i2cdetect and then read a small temperature sensor in Python.
Hardware
- ODROID-XU4 running Armbian (kernel 6.6.x)
- Standard I2C sensor (TMP102, BMP280, etc.) — 3.3 V power supply, never 5 V if the sensor does not support it
sudo apt install i2c-tools python3-smbus
Complete Code
# 1) Lister les bus puis balayer (adapter le numero, p. ex. i2c-1)
i2cdetect -l
i2cdetect -y 1
#!/usr/bin/env python3
"""temp_i2c.py — lire un TMP102 (adresse 0x48) sur le XU4."""
import smbus, time
BUS, ADDR = 1, 0x48 # bus/adresse confirmes par i2cdetect
bus = smbus.SMBus(BUS)
while True:
msb, lsb = bus.read_i2c_block_data(ADDR, 0x00, 2)
raw = (msb << 8) | (lsb & 0xF0) # 12 bits, LSB = 0,0625 C
if raw >= 0x800: # complement a deux
raw -= 0x1000
print(f"Temperature : {raw * 0.0625:.2f} C")
time.sleep(2)
Expected Verification
i2cdetect -y <bus> displays the sensor address (0x48 for a default TMP102). The script displays a temperature consistent with the room, which rises when you pinch the sensor with your finger.
Common Errors
- Wiring to the wrong header: I2C is on the 12-pin header (GPIO, I2S, I2C), not necessarily where you connected the LED.
- Forgetting SDA/SCL pull-ups: many modules include them, but a homemade board without pull-ups will never respond.
- Wrong bus number in the script when i2cdetect worked on another one.
- 5 V power supply on a 3.3 V only sensor; check the module's datasheet.
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash