We want to setp up an automatic, GPIO and temp controlled fan using a Raspberry Pi without additinal software. After the CPU Core temperature exceeds a specific temperature, the fan starts spinning.
These hyperlinks are just recommended. You can use similar parts.
We connect Pin 4 (5V out) directly to the fan. Pin 6 (Ground) will be connected over the Transistor with the 2nd fan wire.
To activate the Transistor, we need a low volatage, which we gain by Pin 3 (GPIO2). The GPIO Pins have a standard voltage of 3.3V. We reduce this by inserting the Resistor.
If we now switch on Pin 3 (GPIO2), the fan should start to spinn.
echo ”2”>/sys/class/gpio/exportecho ”out”>/sys/class/gpio/gpio2/direction
echo ”1”>/sys/class/gpio/gpio2/value
echo “0”>/sys/class/gpio/gpio2/value
In the following, we will set up the fan so that it starts after reaching a special temperature.
We log into the Raspberry Pi as root.
The Raspberry Pi autmoaticly checks the core temperature throught the built-in control panel. If the temperature exceeds a certain value, the fan will start. As long as the temp doesn’t fall below our value, the fan will not stop.
apt-get update && apt-get upgradeapt-get install nano
Now we locate our directory of the script…
mkdir /home/fancd /home/fan
…and create our script.
nano fan.sh
Copy the following content into the script: (Use right mouse button to paste)
#!/bin/shtimestamp() {date +”%Y-%m-%d %T”}LOGDIR=”/var/log/fan.log”
VALUE=42
TEMP=`vcgencmd measure_temp | cut -c6,7`STATUS=`cat /sys/class/gpio/gpio2/value`echo `timestamp` ” Info: Temperature: $TEMP”>>$LOGDIRif [ $TEMP -ge $VALUE ] && [ $STATUS -eq 0 ]thenecho `timestamp` ” Warning: Fan started.”>>$LOGDIRecho ”1”>/sys/class/gpio/gpio2/valueelif [ $TEMP -le $VALUE ] && [ $STATUS -eq 1 ]thenecho `timestamp` ” Warning: Fan stopped.”>>$LOGDIRecho ”0”>/sys/class/gpio/gpio2/valuefi
To change the value, you have to edit line 6.
To let the script run in background, we need `cronjob`.
crontab -e
and paste the following content:
* * * * * /home/fan/./fan.sh
(repalce with your path)
The script ist now automaticly executed at every minute.