Skip to content

Instantly share code, notes, and snippets.

@FrankWu100
Last active April 29, 2022 18:37
Show Gist options
  • Save FrankWu100/9b7dee45ce40729221cb277bce1102d9 to your computer and use it in GitHub Desktop.
Save FrankWu100/9b7dee45ce40729221cb277bce1102d9 to your computer and use it in GitHub Desktop.
GPIO Fan-Control daemon

How to use

This is a simple script and service setup that allows your Debian OS can monitor the temperature of the SoC regularly and then control it to be high or low speed automatically through the GPIO.

Install by ADB

If you can use ADB to access Tinker, you can use the batch command file fancontrol-gpio_ADB-INSTALL.cmd to push file in Debian & enable it.

Install manually

Or you can follow or refernce following commands to install them to your Debian OS.

cp fancontrol-gpio.sh /usr/bin/fancontrol-gpio
sudo chmod 755 /usr/bin/fancontrol-gpio

cp fancontrol-gpio.service /lib/systemd/system/fancontrol-gpio.service
sudo chmod 644 /lib/systemd/system/fancontrol-gpio.service

sudo systemctl enable fancontrol-gpio.service
sudo systemctl start fancontrol-gpio.service

How to configure if needed

KEY Value (default) Description
SLEEP_INTERVAL 1 check temperature for GPIO control every n seconds. If you don't need that instantly, you can increase the interval time.
FAN_GPIO 154 set GPIO you need. e.g. Tinker Board 2 is 154, Tinker Edge R is 158
DEBUG_LOG false print more logs for debug
TEMP_TRIGGER 45000 the trigger point (in degrees C * 1000) when turn GPIO high
TEMP_BUFFER 2500 the buffer range, let GPIO not change too frequently (if the temperature is just right around the trigger point). e.g. when or higher than 45 degrees, the GPIO is triggered high, but only when it is below 42.5 (45-2.5) degrees, it will turn the GPIO to low.
[Unit]
Description=fancontrol-gpio
[Service]
Type=simple
ExecStart=/usr/bin/fancontrol-gpio
[Install]
WantedBy=multi-user.target
#!/bin/bash
# configs
SLEEP_INTERVAL=1 # in s
FAN_GPIO=154
DEBUG_LOG=false
# set temps (in degrees C * 1000) and corresponding pwm values in ascending order and with the same amount of values
TEMP_TRIGGER=45000
TEMP_BUFFER=2500
# hardcoded termal_zone0 for CPU
FILE_TEMP=/sys/class/thermal/thermal_zone0/temp
#FILE_CONFIG=""
# checking for privileges
if [ $UID -ne 0 ]
then
echo "Writing to sysfs requires privileges, relaunch as root"
exit 1
fi
# export fan gpio
function exportPin {
if [ ! -d /sys/class/gpio/gpio$FAN_GPIO ]; then
echo $FAN_GPIO > /sys/class/gpio/export
fi
}
# set fan gpio as output
function setOutput {
echo "out" > /sys/class/gpio/gpio$FAN_GPIO/direction
}
function interpolate_gpio {
i=0
TEMP=$(cat $FILE_TEMP)
OLD_VALUE=$(cat /sys/class/gpio/gpio$FAN_GPIO/value)
if [ $DEBUG_LOG = true ]; then
echo "current temp: $TEMP, value: $(cat /sys/class/gpio/gpio$FAN_GPIO/value)"
fi
if [[ $TEMP -le $((TEMP_TRIGGER - TEMP_BUFFER)) ]]; then
if [ $OLD_VALUE = 1 ]; then
echo 0 > /sys/class/gpio/gpio$FAN_GPIO/value
echo "temp: $TEMP, change value to 0"
fi
elif [[ $TEMP -ge $TEMP_TRIGGER ]]; then
if [ $OLD_VALUE = 0 ]; then
echo 1 > /sys/class/gpio/gpio$FAN_GPIO/value
echo "temp: $TEMP, change value to 1"
fi
fi
return
}
function reset_on_fail {
echo "exiting, resetting fan to auto control..."
echo 1 > /sys/class/gpio/gpio$FAN_GPIO/value
echo $FAN_GPIO > /sys/class/gpio/unexport
exit 1
}
# always try to reset fans on exit
trap "reset_on_fail" SIGINT SIGTERM
function run_daemon {
while :; do
interpolate_gpio
sleep $SLEEP_INTERVAL
done
}
# print debug info
echo "--------------------------------"
echo "SLEEP_INTERVAL = $SLEEP_INTERVAL"
echo "FAN_GPIO = $FAN_GPIO"
echo "DEBUG_LOG = $DEBUG_LOG"
echo "TEMP_TRIGGER = $TEMP_TRIGGER"
echo "TEMP_BUFFER = $TEMP_BUFFER"
echo "--------------------------------"
echo
# set fan control
exportPin
setOutput
# finally start the loop
run_daemon
@echo on
adb push fancontrol-gpio.sh /usr/bin/fancontrol-gpio
adb shell "sudo chmod 755 /usr/bin/fancontrol-gpio"
adb push fancontrol-gpio.service /lib/systemd/system/fancontrol-gpio.service
adb shell "sudo chmod 644 /lib/systemd/system/fancontrol-gpio.service"
adb shell "sudo systemctl enable fancontrol-gpio.service"
adb shell "sudo systemctl start fancontrol-gpio.service"
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment