Monday, February 11, 2019

Control a cooling fan with gpio-fan (Device Tree Overlays)

I want to setup a cooling fan and enable it at temperature greater then 55 °C.
I found gpio-fan on device tree overlays.

Name:   gpio-fan
Info:   Configure a GPIO pin to control a cooling fan.
Load:   dtoverlay=gpio-fan,<param>=<val>
Params: gpiopin                 GPIO used to control the fan (default 12)
        temp                    Temperature at which the fan switches on, in
                                millicelcius (default 55000)
Reference: https://github.com/raspberrypi/firmware/tree/master/boot/overlays

Step to Setup

1. Setup a cooling fan and NPN transistor (S8050).

Reference:



2. Add config device tree overlays on /boot/config.txt.

dtoverlay=gpio-fan,gpiopin=12,temp=55000
 temp = 55000 is millicelcius

3. Restart Raspberry PI

After reboot success.

Hope you enjoy with it.



If you don't want to use gpio-fan overlay.

Create file on /home/pi/bin/temp_montior

#!/bin/bash

GPIO_PIN=12

HIGH_TEMP=50000
SAVE_TEMP=45000

function get_temp() {
  cat /sys/class/thermal/thermal_zone0/temp
}

function gpio_enable() {
  echo $1 > /sys/class/gpio/export
}

function gpio_disable() {
  echo $1 > /sys/class/gpio/unexport
}

function gpio_set_output() {
  echo out > /sys/class/gpio/gpio$1/direction
}

function gpio_set_input() {
  echo in > /sys/class/gpio/gpio$1/direction
}

function gpio_set_on() {
  echo 1  > /sys/class/gpio/gpio$1/value
}

function gpio_set_off() {
  echo 0 > /sys/class/gpio/gpio$1/value
}

function cleanup() {
  QUIT=1
}

QUIT=0

#echo "Enable GPIO ${GPIO_PIN}"
gpio_enable $GPIO_PIN

#echo "Set GPIO ${GPIO_PIN} Output"
gpio_set_output $GPIO_PIN

#echo "set GPIO ${GPIO_PIN} on"
#gpio_set_on $GPIO_PIN

LASTSTATUS=0


trap cleanup SIGINT SIGTERM

while [ 1 ]; do
  cputemp=$(get_temp)
  #echo "CPU TEMP = $cputemp"
  if [ $LASTSTATUS = 0 ]; then
    if [ $cputemp -gt $HIGH_TEMP ]; then
      echo "FAN ON"
      echo "CPU TEMP = $cputemp"
      gpio_set_on $GPIO_PIN
      LASTSTATUS=1
    fi
  else
    if [ $cputemp -lt $SAVE_TEMP ]; then
      echo "FAN OFF"
      echo "CPU TEMP = $cputemp"
      gpio_set_off $GPIO_PIN
      LASTSTATUS=0
    fi
  fi
  if [ "$QUIT" = "1" ]; then break; fi
  sleep 1
done

#echo "set GPIO ${GPIO_PIN} off"
gpio_set_off $GPIO_PIN

#echo "Disable GPIO ${GPIO_PIN}"
gpio_disable $GPIO_PIN


Change mode for execute file

$ chmod 755 /home/pi/bin/temp_montior

Add this script to startup.
Edit /etc/rc.local and add line before exit 0

$ sudo nano /etc/rc.local

...
/home/pi/bin/temp_monitor &
...
exit0

You can change GPIO_PIN, HIGH_TEMP and SAVE_TEMP for gpio pin, start and stop fan