Tom Kintop
Published © GPL3+

Temperature Controlled Fan for Raspberry Pi

Keep your Pi cool by having it turn on a fan whenever it gets hot.

IntermediateFull instructions provided2 hours11,280
Temperature Controlled Fan for Raspberry Pi

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
I'm using a Model 3 B+, but any Pi will do - Just adjust your case choice to the model you have
×1
General Purpose Transistor NPN
General Purpose Transistor NPN
×1
40 mm Fan
Any 40 mm 5 volt fan will work. There are plenty of choices out there depending on the connector you want. Mine is similar to these
×1

Software apps and online services

Fritzing App
Used for wiring diagram
Makerbot Thingiverse
Find a custom case for Raspberry Pi
puTTY SSH Client
Used to remote into the Raspberry Pi to the command line.
RPi-GPIO
Needed for your python code to interact with the RPi pins

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)
I used my trusty printrbot Simple Metal to fabricate my case. Sadly printrbot is no longer in business.
Soldering Station, Hobbyist
Soldering Station, Hobbyist

Story

Read more

Custom parts and enclosures

Raspberry Pi 3 B+ Fan Enclosure

There are many options for Raspberry Pi cases on Thingiverse. Just choose one that fits the model you own.

Schematics

Basic Wiring

The wiring is pretty simple. Below the motor represents the fan. The fan I used has a red and black wire, so I hooked the red wire to a 5v pin of the rPi and the black wire one of the outside legs of the NPN transistor. The other outside leg is connected to the rPi ground and the center leg to the rPi control pin. I used 18, but you can use any of them—just make sure your code references the correct one.

Code

run-fan.py Code

Python
#!/usr/bin/env python3

# adapted from Edoardo Paolo Scalafiotti <edoardo849@gmail.com>
# https://hackernoon.com/how-to-control-a-fan-to-cool-the-cpu-of-your-raspberrypi-3313b6e7f92c

from time import sleep
import subprocess
import RPi.GPIO as GPIO

# Adjust these settings for your environment
pin = 18
maxTMP = 42
minTMP = 38
sleepTime = 5

def setup():
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(pin, GPIO.OUT)
    GPIO.setwarnings(False)
    return()

def getCPUtemperature():
    res=subprocess.check_output(['vcgencmd', 'measure_temp']).decode("utf-8")
    temp = (res.replace("temp=","").replace("'C\n",""))
    # print("temp is {0}".format(temp)) - uncomment for testing
    return temp

def fanON():
    setPin(True)
    return()
def fanOFF():
    setPin(False)
    return()
def getTEMP():
    CPU_temp = float(getCPUtemperature())
    if CPU_temp>maxTMP:
        fanON()
    elif CPU_temp<minTMP:
        fanOFF()
    return()
def setPin(mode):
    GPIO.output(pin, mode)
    return()

try:
    setup() 
    while True:
        getTEMP()
        sleep(sleepTime)
except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt. Not needed for autorun but good for testing 
    GPIO.cleanup() # resets all GPIO ports used by this program

runfan.system

Plain text
This code is used to auto-start the python script at start-up
[Unit]
Description=Run fan contol at startup
After=multi-user.target

[Service]
Type=idle
ExecStart=/usr/bin/python3 /home/pi/scripts/run-fan.py

[Install]
WantedBy=multi-user.target

Credits

Tom Kintop

Tom Kintop

2 projects • 7 followers
Too many projects, so little time.

Comments