Skip to content

Instantly share code, notes, and snippets.

@geerlingguy
Last active March 14, 2024 17:40
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geerlingguy/9c9c78463c0e3d9f4a23152912930821 to your computer and use it in GitHub Desktop.
Save geerlingguy/9c9c78463c0e3d9f4a23152912930821 to your computer and use it in GitHub Desktop.
Fan controller script for the CM4 IO Board EMC2301 and PWM fans
#!/bin/bash
#
# Jeff Geerling's super-rudimentary fan controller script for the CM4.
#
# Use:
# 1. Download this script to a path like `/opt/cm4-fan.sh`
# 2. Run it with: `nohup bash /opt/cm4-fan.sh`
# 3. Profit!
#
# You should wrap it in a systemd unit file if you want it to persist and come
# up after reboot, too.
# Explicitly set $PATH so i2c tools are found.
PATH=$PATH:/usr/sbin
# Temperature set point (Celcius).
TEMP_THRESHOLD=70
TEMP_COMPARE=$(($TEMP_THRESHOLD * 1000))
# Minimum fan speed (0-255).
MIN_FAN_SPEED=100
MIN_FAN_SPEED_HEX=$(printf "0x%02x" "$MIN_FAN_SPEED")
# Maximum fan speed (0-255).
MAX_FAN_SPEED=255
MAX_FAN_SPEED_HEX=$(printf "0x%02x" "$MAX_FAN_SPEED")
# TODO: The script should also check that `i2cget` is present, and that the
# fan is visible on the bus.
# Start a loop.
while true; do
# Get the current temperature.
CURRENT_TEMP=$(cat /sys/class/thermal/thermal_zone0/temp)
if [ $CURRENT_TEMP -ge $TEMP_COMPARE ]; then
# If current temperature is more than desired, set fan to maximum speed.
i2cset -y 10 0x2f 0x30 $MAX_FAN_SPEED_HEX
else
# If current temperature is good, set fan to minimum speed.
i2cset -y 10 0x2f 0x30 $MIN_FAN_SPEED_HEX
fi
sleep 10
done
@Matir
Copy link

Matir commented Nov 24, 2021

Is nup meant to be nohup? I'm not familiar with nup, perhaps this is an alias?

@geerlingguy
Copy link
Author

@Matir - Oops, indeed you are right, that was supposed to be a nohup!

@icman003
Copy link

Hi Jeff,
I have installed cm4io-fan driver , if I try your script are they both runing or just one is in use

@derpeter
Copy link

Thanks, this is very useful for non RPI modules like the SOQuartz which are pin compatible but don't have the overlay files and the pi kernel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment