Quick script to get the TACO fan working with Radxa CM5

I don’t know how many people are still after this colution but the following works on my machine to get some degree of on-off fan control on the Taco when using a Radxa CM5 module on the board. Nothing special going on, but hopefully it is useful to someone.

First make sure you have the necessary libraries to manage the gpio pins in userspace

$sudo apt install gpiod

I then ran $ gpioinfo in the terminal and found which gpio lines were attached to which ‘“pins’“ and were set into output mode etc. It turned out that gpiochip4 line1 was wired to PIN 33 and that seemed to be the pin that controlled the mosfet that powered the fan connector. (Yes this was a little painful without the schematic.

Now, create a bash script somewhere sensible e.g. /usr/bin/taco-fan.sh

#!/bin/bash

GPIOCHIP="gpiochip4"
GPIOLINE="1"
ON_TEMP=50000   #50°C
OFF_TEMP=40000  #40°C

gpioset "$GPIOCHIP" "$GPIOLINE"=0

cleanup() {
    echo "Shutting down fan control..."
    gpioset "$GPIOCHIP" "$GPIOLINE"=0
    exit 0
}
trap cleanup SIGINT SIGTERM

echo "Fan control started (on at $(( ON_TEMP/1000 ))°C, off at $(( OFF_TEMP/1000 ))°C)"

FAN_STATE=0

while true; do
  TEMP=$(cat /sys/class/thermal/thermal_zone0/temp)
  
  if [ $TEMP -gt $ON_TEMP ] && [ "$FAN_STATE" -eq 0 ]; then
    echo "$(date): ${TEMP}m°C — fan ON"    
    gpioset "$GPIOCHIP" "$GPIOLINE"=1
    FAN_STATE=1
  elif [ $TEMP -lt $OFF_TEMP ] && [ "$FAN_STATE" -eq 1 ]; then
    echo "$(date): ${TEMP}m°C — fan OFF"
    gpioset "$GPIOCHIP" "$GPIOLINE"=0
    FAN_STATE=0
  fi
  sleep 10
done

ON_TEMP and OFF_TEMP are exactly what they sound like… the temperature in milliCelcius at which the fan should come on and off. Make sure OFF is a good bit lower than ON to prevent constant cycling and make sure OFF is above your ambient temperature or else it will never go off.

On mine at least the thermal zones are as follows:

$for zone in /sys/class/thermal/thermal_zone*; do
    echo -n "$(basename "$zone"): "
    cat "$zone/type"
done

This returned the following for me

thermal_zone0: soc-thermal
thermal_zone1: bigcore0-thermal
thermal_zone2: bigcore1-thermal
thermal_zone3: littlecore-thermal
thermal_zone4: center-thermal
thermal_zone5: gpu-thermal
thermal_zone6: npu-thermal

I chose to use soc-thermal as my measurement sensor, but the only way to know for certain if that is the right idea is for an engineer to share the schematics.

Now make sure this script is executable

$sudo chmod o+x /usr/bin/taco-fan.sh

Create an appropriate service file e.g. /etc/systemd/system/taco-fan.service

[Unit]
Description=Taco Fan

[Service]
Type=simple
ExecStart=/usr/bin/taco-fan.sh
Restart=always
RestartSec=1
WorkingDirectory=/usr/bin

[Install]
WantedBy=multi-user.target

Finally start and enable the service and you should have a functioning fan system.

$sudo systemctl start taco-fan.service
$sudo systemctl enable taco-fan.service
$sudo systemctl status taco-fan.service

Hope this helps someone out.

1 Like