Automatic CPU Fan Control
Oct 20, 2017 6:35:14 GMT
Post by Admin on Oct 20, 2017 6:35:14 GMT
EDIT: Designed and tested on ArchLinuxARM, reported as working on Ubuntu.
You need to be superuser to access gpio pins, sudo will not work.
The Geekbox fan (fan power plug) is controlled by GPIO3_B3 (gpio107). You can control this pin from a terminal by enabling it for output.
Turn the fan on with:
Turn the fan off with:
Let's make a bash script that checks the CPU temperature and turns the fan on or off automatically.
CPU temp can be found by
Enter this script with your favorite editor and save it as /usr/bin/fancontrold
Set permissions on this file with
You can execute this script from the commandline, or better yet, let's create a systemd service to execute the script automatically on boot.
With an editor, create this file and save as /lib/systemd/system/cpufan.service
Set permissions
Tell systemd you want this service to run on boot with
If you don't want to wait, you can start it now with
Tried and tested on ArchlinuxARM.
You need to be superuser to access gpio pins, sudo will not work.
sudo su
The Geekbox fan (fan power plug) is controlled by GPIO3_B3 (gpio107). You can control this pin from a terminal by enabling it for output.
echo 107 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio107/direction
Turn the fan on with:
echo 1 > /sys/class/gpio/gpio107/value
Turn the fan off with:
echo 0 > /sys/class/gpio/gpio107/value
Let's make a bash script that checks the CPU temperature and turns the fan on or off automatically.
CPU temp can be found by
cat /sys/rockchip_thermal/temp
The reply is not a simple numeric value so we have to manipulate it in our code.Enter this script with your favorite editor and save it as /usr/bin/fancontrold
#!/bin/bash
FAN_ON_TEMP="50" # Desired operating temp range
FAN_OFF_TEMP="45" # Off is always less than on
if [ ! -d /sys/class/gpio/gpio107 ];
then
echo 107 > /sys/class/gpio/export;
echo out > /sys/class/gpio/gpio107/direction;
fi
echo 1 > /sys/class/gpio/gpio107/value;
while [ 1 ]; do
read CPUTEMP < /sys/rockchip_thermal/temp
IFS=':' read -r -a temp <<< "$CPUTEMP"
if [ "${temp[1]}" -gt "$FAN_ON_TEMP" ]; then echo 1 > /sys/class/gpio/gpio107/value; fi
if [ "${temp[1]}" -lt "$FAN_OFF_TEMP" ]; then echo 0 > /sys/class/gpio/gpio107/value; fi
sleep 15
done
The temperature settings I have here are conservative. The fan should cycle when the CPUs have a light load, and remain on under heavier loads. A small hysteresis prevents the fan from rapidly cycling.Set permissions on this file with
chown root:root /usr/bin/fancontrold
chmod 755 /usr/bin/fancontrold
You can execute this script from the commandline, or better yet, let's create a systemd service to execute the script automatically on boot.
With an editor, create this file and save as /lib/systemd/system/cpufan.service
[Unit]
Description=CPU Cooling Fan Service
Requires=basic.target
[Service]
Type=simple
User=root
ExecStart=/usr/bin/fancontrold
ExecStop=/usr/bin/pkill -u root fancontrold
Restart=on-error
[Install]
WantedBy=multi-user.target
Set permissions
chown root:root /lib/systemd/system/cpufan.service
chmod 644 /lib/systemd/system/cpufan.service
Tell systemd you want this service to run on boot with
systemctl enable cpufan.service
If you don't want to wait, you can start it now with
systemctl start cpufan.service
Tried and tested on ArchlinuxARM.