active_cooling_pwm_control-9.ino

Xerxes Pi 2026 Carrier Board for Raspberry Pi CM4/CM5 Modules · File : active_cooling_pwm_control-9.ino · Language : python · Use case : Contrôle automatique du ventilateur de refroidissement actif via PWM basé sur la température CPU

Python script dynamically adjusting the speed of the fan connected to the PWM connector based on processor temperature.

The fan must be connected to the active cooling connector on the board. Identify the specific GPIO pin used for PWM on the mezzanine connector (check KiCad docs). The script uses standard thermal reading. The example uses prints for debugging; replace set_pwm_fan with actual RPi.GPIO or pigpio calls for actuation.

active_cooling_pwm_control-9.ino

import os
import time
import subprocess

def get_cpu_temp():
    """Lit la température du CPU (en °C)"""
    try:
        with open('/sys/class/thermal/thermal_zone0/temp', 'r') as f:
            temp = int(f.read().strip()) / 1000.0
            return temp
    except IOError:
        return 0.0

def set_pwm_fan(pwm_pin, duty_cycle):
    """Configure le PWM pour le ventilateur (duty_cycle 0-100)
    Note : Utilise rpi.gpio ou pigpio. Exemple simplifié via sysfs si disponible,
    sinon nécessiterait la lib RPi.GPIO."""
    # Pour l'exemple, nous simulons le contrôle si la lib n'est pas gérée ici
    # En réalité, utilisez RPi.GPIO.setPWM ou pigpio pour la broche PWM spécifique
    print(f"Setting PWM duty cycle to {duty_cycle}%")
    # Placeholder pour la commande réelle dépendante de la broche physique
    # Exemple avec pigpio daemon si activé :
    # os.system(f"pigs p {pwm_pin} {int(duty_cycle * 2.55)}")

if __name__ == "__main__":
    PWM_PIN = 18  # Exemple de broche GPIO PWM disponible sur le mezzanine
    try:
        while True:
            temp = get_cpu_temp()
            print(f"CPU Temperature: {temp:.2f} C")
            
            if temp < 45.0:
                set_pwm_fan(PWM_PIN, 0)   # Ventilateur éteint
            elif temp < 60.0:
                set_pwm_fan(PWM_PIN, 30)  # Basse vitesse
            elif temp < 70.0:
                set_pwm_fan(PWM_PIN, 70)  # Moyenne vitesse
            else:
                set_pwm_fan(PWM_PIN, 100) # Pleine vitesse
                
            time.sleep(2)
    except KeyboardInterrupt:
        print("\nArrêt du contrôle du ventilateur.")

← Back to the board