Hello.
ive written a small script that monitors the temperature of the rpi3 which used to run hotter than hot even when idle and controls the fan speed using the PWM0 channel.
the PWM overlay must be enabled and setup correctly.
im driving the fan with a NPN transistor. If someone needs support on how to wire it. contact me.
the script which resides in /usr/bin/fan must be run with root privileges in order to enable the pwm and access the fan control sys interface
$this->bbcode_second_pass_code('', '
#!/bin/bash
#pwm settings
pwm_path="/sys/class/pwm/pwmchip0"
pwm0="$pwm_path/pwm0"
freq=10000000
duty_mult=100000
# when the temp is over the trip point, the speed is according to the duty point using the same index.
# for this to work, the trip_points_degrees must be sorted ascending.
trip_points_degrees=(0 40 45 50 60)
duty_points_percent=(0 20 40 80 100)
old_duty=0 #the old duty so that we dont write unless we have to.
for elem in ${duty_points_percent[*]}
do
duty_points=(${duty_points[*]} $(($duty_mult * $elem)))
done
for elem in ${trip_points_degrees[*]}
do
trip_points=(${trip_points[*]} $(( 1000 * $elem)))
done
echo "Duty cycle values: ${duty_points[@]}"
echo "temperature trip_points: ${trip_points[@]}"
if [ ! -d $pwm0 ]; then
echo "dir did not exist. initializing the pwm"
echo 0 > "$pwm_path/export"
echo $freq > "$pwm0/period"
echo "10000000" > "$pwm0/duty_cycle"
echo 1 > "$pwm0/enable"
old_duty=10000000
fi
while [ true ]
do
temp=`cat /sys/class/thermal/thermal_zone0/temp`
for index in "${!trip_points[@]}"
do
if [ $temp -gt ${trip_points[$index]} ]; then
duty=${duty_points[$index]}
fi
done
if [ $old_duty -ne $duty ]; then
echo "$duty" > "$pwm0/duty_cycle"
old_duty=$duty
fi
sleep 1
done
exit 0
')
the systemd fan.service unit which starts the script on startup.
$this->bbcode_second_pass_code('', '
[Unit]
Description= fan service
[Service]
ExecStart="/usr/bin/fan"
[Install]
WantedBy=multi-user.target
')
to do: add a lock file. and more checks.
I hope someone finds it useful
cheers.