Skip to content

Instantly share code, notes, and snippets.

@geerlingguy
Last active November 23, 2024 07:54
Show Gist options
  • Select an option

  • Save geerlingguy/9c9c78463c0e3d9f4a23152912930821 to your computer and use it in GitHub Desktop.

Select an option

Save geerlingguy/9c9c78463c0e3d9f4a23152912930821 to your computer and use it in GitHub Desktop.

Revisions

  1. geerlingguy revised this gist Nov 24, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cm4-fan.sh
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    #
    # Use:
    # 1. Download this script to a path like `/opt/cm4-fan.sh`
    # 2. Run it with: `nup bash /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
  2. geerlingguy created this gist Nov 22, 2021.
    43 changes: 43 additions & 0 deletions cm4-fan.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    #!/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: `nup 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