🛠 Lire un capteur I2C (température)

Hardkernel ODROID-XU4 (Samsung Exynos5422) · intermediaire · Schéma : IMG-odroid-xu4-intermediaire-2-odroidxu4-capteur-i2c.svg — Capteur de température I2C relié au header 12 broches (bus I2C) de l'ODROID-XU4 avec résistances de pull-up SDA/SCL et alimentation 3,3 V

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials