i2c-sensor-temp.py
Example of reading a digital temperature sensor on the internal I2C/SMBus bus of the Panther Lake board.
Uses I2C bus 1 (verify via BIOS). The LM75 sensor is a common example. Ensure the i2c-dev kernel module is loaded. Connect SDA/SCL to the corresponding internal pins. Execution: python3 i2c_sensor.py.
i2c-sensor-temp.py
import smbus
import time
# Adresse I2C du capteur LM75 (0x48 par défaut)
I2C_BUS = 1
SENSOR_ADDR = 0x48
TEMP_REG = 0x00
def read_temperature():
try:
bus = smbus.SMBus(I2C_BUS)
# Lire 2 octets de température
raw = bus.read_word_data(SENSOR_ADDR, TEMP_REG)
# Conversion : décalage de 5 bits, MSB en premier
temp = ((raw << 8) & 0xFF00) + (raw >> 8)
temp = temp >> 5
if temp > 127:
temp -= 256
return temp / 8.0
except Exception as e:
print(f"Erreur lecture I2C : {e}")
return None
def main():
print("Lecture capteur température I2C...")
while True:
temp = read_temperature()
if temp is not None:
print(f"Température : {temp:.2f} °C")
time.sleep(2)
if __name__ == "__main__":
main()