Here is a simple fan control script that I made to turn on and off a raspberry pi fan. It turns on a fan when the temp reaches a threshold (in this case, 60 degrees).
#!/bin/bash
#
# Wed Feb 17 19:14:54 EST 2021 JondZ very simple fan controller
GPIO_PIN=4
GPIO_NAME=gpio${GPIO_PIN}
GPIO_PATH=/sys/class/gpio/$GPIO_NAME/value
TEMP_THRESHOLD=60000
TEMP_SOURCE_FILE=/sys/class/thermal/thermal_zone0/temp
# 600: 10 minutes
# 900: 15 minutes
# 1800: 30 minutes
SLEEP_TIME=600
SLEEP_POLL=10
function get_temp {
if [[ -e $TEMP_SOURCE_FILE ]] ; then
cat $TEMP_SOURCE_FILE
fi
}
function init_ports {
if [[ ! -e /sys/class/gpio/$GPIO_NAME ]]; then
echo $GPIO_PIN > /sys/class/gpio/export && \
sleep 5 && \
echo out > /sys/class/gpio/$GPIO_NAME/direction
fi
}
init_ports
function turn_on_fan {
temp=$(get_temp)
if [[ -e $GPIO_PATH ]]; then
echo "$(date) fan on ($temp)"
v=$(< $GPIO_PATH )
if [[ $v == 0 ]]; then
echo 1 > $GPIO_PATH
fi
fi
}
function turn_off_fan {
temp=$(get_temp)
if [[ -e $GPIO_PATH ]]; then
v=$(< $GPIO_PATH )
if [[ $v == 1 ]]; then
echo "$(date) fan off ($temp)"
echo 0 > $GPIO_PATH
fi
fi
}
while :
do
CT=$(get_temp)
if [[ $TEMP_THRESHOLD -lt $CT ]]; then
turn_on_fan
sleep $SLEEP_TIME
else
turn_off_fan
fi
sleep $SLEEP_POLL
done
Here is the systemd script:
[Unit]
Description=Simple Fan Controller
DefaultDependencies=no
After=sysinit.target
[Service]
Type=simple
ExecStart=/root/scriptsd/system-fan-control.sh
[Install]
WantedBy=graphical.target multi-user.target
As far as I can see it is working fine.
Here is a picture of the circuit that i soldered. I copied the circuit from some other site on the web. This comprises of a resistor, a transistor, and a diode.
Here is what the fan circuit looks like on my setup. The case is already crowded with an OLED display and an RTC clock:
There may be minor bugs. I was tired from soldering so I only did the coding in a few minutes' rush so comments are welcome...
EP 20210217
No comments:
Post a Comment