Skip to content

Instantly share code, notes, and snippets.

@geerlingguy
Last active Nov 24, 2021
Embed
What would you like to do?
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?

Loading

@geerlingguy
Copy link
Author

geerlingguy commented Nov 24, 2021

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

Loading

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