gpio_pwm_fan_control.py
Script Python usando la biblioteca RPi.GPIO para controlar el conector PWM de la Xerxes Nano según la temperatura de la CPU Jetson/Turing.
Hardware: Tarjeta Xerxes Nano con ventilador conectado al puerto PWM. Software: Python 3, biblioteca RPi.GPIO (o Jetson.GPIO). Ejecución: sudo python3 fan_control.py. Asegúrese de que el jumper de alimentación esté configurado para alimentar el puerto GPIO si es necesario.
gpio_pwm_fan_control.py
#!/usr/bin/env python3
import RPi.GPIO as GPIO
import time
import subprocess
import re
# Pin GPIO pour le contrôle PWM du ventilateur (à vérifier selon le schéma Xerxes Nano)
FAN_PIN = 18
# Configuration GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(FAN_PIN, GPIO.OUT)
pwm = GPIO.PWM(FAN_PIN, 1000) # 1kHz PWM
pwm.start(0)
def get_cpu_temp():
"""Lit la température CPU via thermal_zone0"""
try:
with open('/sys/class/thermal/thermal_zone0/temp', 'r') as f:
temp = int(f.read().strip()) / 1000.0
return temp
except Exception as e:
print(f"Erreur lecture température: {e}")
return 0
def set_fan_speed(duty_cycle):
"""Ajuste le cycle de service PWM (0-100)"""
pwm.ChangeDutyCycle(duty_cycle)
try:
while True:
temp = get_cpu_temp()
# Logique de contrôle simple : 40°C = 0%, 80°C = 100%
if temp < 40:
speed = 0
elif temp > 80:
speed = 100
else:
speed = int((temp - 40) / 40 * 100)
set_fan_speed(speed)
print(f"Temp: {temp:.1f}°C | Fan: {speed}%")
time.sleep(5)
except KeyboardInterrupt:
pass
finally:
pwm.stop()
GPIO.cleanup()