mdoff's blog

Cool your Raspberry Pi with GPIO controlled fan

· Tomasz Lewiński

Recently I bought enclosure for my Raspberry Pi 3 B+ with included fan. Instruction said that it should be connected to GPIO pins 4 and 6 (5V and GND). Problem with that is fan works constantly and it’s quite loud.

I was googling a solution and found this post on hackernoon and it was almost exactly what I was looking for.

The only thing that I didn’t like was using python and configuring this program as a service (which normally is really good solution), so I decided to write bash script to do exactly same thing.


# Check if gpio is already exported
if [ ! -d /sys/class/gpio/gpio$GPIO  ]
    echo $GPIO > /sys/class/gpio/export
    sleep 1;
# Make sure that pin is in output mode
echo "out" > /sys/class/gpio/gpio$GPIO/direction
# Get the temperature as integer
TEMP=`vcgencmd measure_temp | egrep -oh -m 1 [0-9]+ | head -n 1`
if [ "$TEMP" -gt "$TEMP_MAX" ]; then
    # If temperature on CPU is greater than $TEMP_MAX then set GPIO to high
    echo "1" > /sys/class/gpio/gpio$GPIO/value
    # If not, set GPIO to low
    echo "0" > /sys/class/gpio/gpio$GPIO/value

Then I put it in /usr/local/bin/ file and added it as cron task to run every minute.