lp-mu-i2c-sensor.py

LattePanda Mu Ultra: x86 Compute Module for Embedded AI · File : lp-mu-i2c-sensor.py · Language : python · Use case : Lire un capteur environnemental via bus I2C.

Python script using smbus to communicate with an I2C device.

Enable the I2C bus via BIOS or device tree if needed. Install python3-smbus. Verify sensor presence with 'i2cdetect -y 1'. Adapt address and registers according to the sensor connected to the carrier board.

lp-mu-i2c-sensor.py

import smbus
import time

# Adresse I2C typique pour un capteur BMP280 ou BME680
I2C_BUS = 1
DEVICE_ADDR = 0x76

def read_sensor():
    try:
        bus = smbus.SMBus(I2C_BUS)
        # Lecture d'un registre de configuration ou données (exemple simplifié)
        # Pour un vrai capteur, consultez la datasheet pour les registres spécifiques
        val = bus.read_byte_data(DEVICE_ADDR, 0xD0) # Chip ID
        print(f"Chip ID lu : {hex(val)}")
        
        # Simulation de lecture température (à adapter au capteur réel)
        # temp_raw = bus.read_word_data(DEVICE_ADDR, 0xFA)
        # print(f"Température brute : {temp_raw}")
        
    except Exception as e:
        print(f"Erreur I2C : {e}")
    finally:
        try:
            bus.close()
        except:
            pass

if __name__ == "__main__":
    while True:
        read_sensor()
        time.sleep(1)

← Back to the board