Skip to content

Instantly share code, notes, and snippets.

@satori99
Last active March 20, 2018 13:36
Show Gist options
  • Save satori99/812d55afb79f5bbf156ef268237e43d2 to your computer and use it in GitHub Desktop.
Save satori99/812d55afb79f5bbf156ef268237e43d2 to your computer and use it in GitHub Desktop.

Revisions

  1. satori99 revised this gist Mar 20, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fancontrol.sh
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    #
    # Raspberry Pi Fan Control Script
    #
    # This script toggles a GPIO pin to control a cooling fan
    # This script toggles a GPIO output pin when a system temperature limit is exceed to control a cooling fan.
    #
    # usage:
    #
  2. satori99 renamed this gist Mar 20, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. satori99 revised this gist Mar 20, 2018. 1 changed file with 66 additions and 13 deletions.
    79 changes: 66 additions & 13 deletions pi-fan-control.sh
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,70 @@
    # Export pin to userspace
    echo "18" > /sys/class/gpio/export
    #!/bin/bash
    #
    # Raspberry Pi Fan Control Script
    #
    # This script toggles a GPIO pin to control a cooling fan
    #
    # usage:
    #
    # $ ./fancontrol.sh
    #
    # $ # show temp readings and fan state
    # $ DEBUG=1 ./fancontrol.sh
    #
    # $ # set monitor freq to 2s and temp threshold to 55 degrees
    # $ FREQ=2 THRESHOLD=55000 ./fancontrol.sh
    #

    # Unexport pin from userspace
    echo "18" > /sys/class/gpio/unexport
    # The GPIO Pin number to use for switching the fan. Default = 18
    PIN=${PIN:-18}

    # Fan On (Sets pin 18 to high)
    echo "out" > /sys/class/gpio/gpio18/direction
    echo "1" > /sys/class/gpio/gpio18/value
    # Temp monitoring frequency (in seconds). Default = 5
    FREQ=${FREQ:-5}

    # Fan Off (Sets pin 18 to low)
    echo "out" > /sys/class/gpio/gpio18/direction
    echo "0" > /sys/class/gpio/gpio18/value
    # Temp threshold. Fan will turn on when the temperature exceeds this value (in millidegrees). Default = 70000
    THRESHOLD=${THRESHOLD:-70000}

    # Fan State (Reads pin 18)
    echo "in" > /sys/class/gpio/gpio18/direction
    cat /sys/class/gpio/gpio18/value
    function init {
    echo $PIN > /sys/class/gpio/export 2> /dev/null
    if [ $? != 0 ]; then
    >&2 echo "$(date): fan control failed to start"
    exit 1
    fi
    sleep 0.25
    echo "out" > "/sys/class/gpio/gpio$PIN/direction"
    echo "$(date): fan control started ($THRESHOLD/$FREQ)"
    [ ! -f $DEBUG ] && echo "$(date): debug: exported GPIO Pin $PIN -> /sys/class/gpio/gpio$PIN"
    }

    function cleanup {
    echo "$PIN" > /sys/class/gpio/unexport 2> /dev/null
    [ ! -f $DEBUG ] && echo "$(date): debug: unexported GPIO Pin $PIN"
    echo "$(date): fan control stopped"
    }

    function fan_on {
    [ $FAN_STATE = 0 ] && echo "1" > "/sys/class/gpio/gpio$PIN/value"
    FAN_STATE=1
    }

    function fan_off {
    [ $FAN_STATE = 1 ] && echo "0" > "/sys/class/gpio/gpio$PIN/value"
    FAN_STATE=0
    }

    FAN_STATE=0
    CURR_TEMP=0
    init
    trap cleanup EXIT
    set -e
    while true
    do
    CURR_TEMP=$(cat /sys/class/thermal/thermal_zone0/temp)
    [ ! -f $DEBUG ] && echo "$(date): debug: Temp=$CURR_TEMP, Fan=$FAN_STATE"
    if [ $CURR_TEMP -gt $THRESHOLD ]; then
    fan_on
    else
    fan_off
    fi
    sleep $FREQ
    done
  4. satori99 revised this gist Mar 20, 2018. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions pi-fan-control.sh
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,17 @@
    # Export pin to userspace
    echo "18" > /sys/class/gpio/export

    # Sets pin 18 as an output
    echo "out" > /sys/class/gpio/gpio18/direction
    # Unexport pin from userspace
    echo "18" > /sys/class/gpio/unexport

    # Fan On (Sets pin 18 to high)
    echo "out" > /sys/class/gpio/gpio18/direction
    echo "1" > /sys/class/gpio/gpio18/value

    # Fan Off (Sets pin 18 to low)
    echo "out" > /sys/class/gpio/gpio18/direction
    echo "0" > /sys/class/gpio/gpio18/value

    # Fan State (Reads pin 18)
    echo "in" > /sys/class/gpio/gpio18/direction
    cat /sys/class/gpio/gpio18/value
  5. satori99 created this gist Mar 20, 2018.
    11 changes: 11 additions & 0 deletions pi-fan-control.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    # Export pin to userspace
    echo "18" > /sys/class/gpio/export

    # Sets pin 18 as an output
    echo "out" > /sys/class/gpio/gpio18/direction

    # Fan On (Sets pin 18 to high)
    echo "1" > /sys/class/gpio/gpio18/value

    # Fan Off (Sets pin 18 to low)
    echo "0" > /sys/class/gpio/gpio18/value