gpio_pwm_fan_control.py

Xerxes Nano Carrier Board for Jetson Orin Nano Super and Turing RK1 Modules · File : gpio_pwm_fan_control.py · Language : Python · Use case : Contrôle dynamique du ventilateur de refroidissement via PWM en fonction de la température CPU.

Python script using RPi.GPIO to control the Xerxes Nano PWM fan connector based on Jetson/Turing CPU temperature.

Hardware: Xerxes Nano board with fan connected to PWM port. Software: Python 3, RPi.GPIO (or Jetson.GPIO) library. Execution: sudo python3 fan_control.py. Ensure the power jumper is configured to supply the GPIO port if required.

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()

← Back to the board