Linux Howtos
Verbesserte Lüftersteuerung für den Raspberry Pi mit PWM - (Pulsweitenmodulation)
Voraussetzung:
Raspberry Pi 3 oder 4
USB Netzteil für Raspberry Pi 3 oder 4
Gehäuse für Raspberry Pi 3 oder 4
SD-Card oder USB Stick¹ mit mindestens 16 GB
Ethernet-Kabel
Raspberry Pi OS with desktop or Lite
Download: https://www.raspberrypi.com/software/operating-systems/#raspberry-pi-os-32-bit
Dokumentation: https://www.raspberrypi.com/documentation/computers/getting-started.html#setting-up-your-raspberry-pi
1:= Das Booten vom USB Stick ist nicht immer möglich.
Viele Gehäuse für den Raspberry Pi haben einen eingebauten Lüfter der meistens mit 5V betrieben wird. Wenn er ständig läuft, kann das manchmal nervigen Lärm verursachen. Es ist möglich den Lüfter an die 3,3 V Leitung anzuschließen, dadurch kann man den Lüfter leiser machen. Oder man bastelt eine elektronische Lüftersteuerung. Dazu benötigt man folgende Bauteile:
Menge | Bauteil | Wert | Beschreibung |
1 | Widerstand | 1 kΩ | https://components101.com/resistors/resistor |
1 | Transistor | 2N2222A | 2N2222A-CDIL.pdf |
Plan | Download | Quelle |
GPIO Pinbelegung | Download | Quelle: |
Schaltplan | Download |
pigpio library installieren
sudo apt-get update && sudo apt-get upgrade sudo apt install python-setuptools python3-setuptools sudo apt install pigpio python-pigpio python3-pigpio sudo systemctl start pigpiod sudo systemctl enable pigpiod
Die Steuerung wird mit dem folgenden Skript realisiert:
nano cooler_fan.sh
#!/bin/bash # Lüftersteuerung GPIO=12 frequency=50 # Celsius * 1000 THRESHOLD_MIN=47500 THRESHOLD_MAX=60000 PULSEMODE_MIN=500000 PULSEMODE_MAX=1000000 fanspeed=$(pigs gdc $GPIO) CPU_TEMP=$(cat /sys/class/thermal/thermal_zone0/temp) # Errechnung der linearen Funktion y = m*x +c m=$(echo "scale=2;($PULSEMODE_MAX-$PULSEMODE_MIN)/($THRESHOLD_MAX-$THRESHOLD_MIN)/10" | bc) # c = y - m*x c=$(echo "scale=2;(($PULSEMODE_MAX/10)-($m*$THRESHOLD_MAX))/1000" | bc) # y= m*x+c => der lineare Graph zur Berechnung der Pulsweite # THRESHOLD_MIN Grad Celsius entspricht PULSEMODE_MIN% Fanspeed und THRESHOLD_MAX Grad Celsius entspricht 100% Fanspeed tmppulsemode=$(echo "scale=3;($m*$CPU_TEMP/1000+$c)*10000" | bc) # $tmppulsemode/1 => Umwandung in Integer Zahl ohne Nachkommastellen pulsemode=$(echo "scale=0;$tmppulsemode/1" | bc) if [ $CPU_TEMP -ge $THRESHOLD_MAX ] then # Bei Maximum Temperatur 100% Pulsmode pulsemode=$PULSEMODE_MAX elif [ $CPU_TEMP -le $THRESHOLD_MIN ] then # Bei Minimum Temperatur 50% Pulsmode pulsemode=$PULSEMODE_MIN fi if [ $fanspeed != $pulsemode ] then # Errechneter Pulsmode echo $(($pulsemode/10000)) > /var/www/temperature/fan-status pigs hp $GPIO $frequency $pulsemode fi
Das Skript ausführbar machen:
chmod +x fancontrol.py
Nun noch einen Cronjob erstellen:
crontab -e
# Edit this file to introduce tasks to be run by cron. # # Each task to run has to be defined through a single line # indicating with different fields when the task will be run # and what command to run for the task # # To define the time you can provide concrete values for # minute (m), hour (h), day of month (dom), month (mon), # and day of week (dow) or use '*' in these fields (for 'any'). # # Notice that tasks will be started based on the cron's system # daemon's notion of time and timezones. # # Output of the crontab jobs (including errors) is sent through # email to the user the crontab file belongs to (unless redirected). # # For example, you can run a backup of all your user accounts # at 5 a.m every week with: # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ # # For more information see the manual pages of crontab(5) and cron(8) # # m h dom mon dow command */5 * * * * /home/#USER#/cooler_fan.sh > /dev/null
