2

I have Raspberry Pi 3b+ with the case, which has a small 5v fan, which is connected to GPIO. Look to picture below:

enter image description here

The Fan runs continuously, but I don't want continuous operation. I want the fan to run only when the temperature of the Raspberry Pi exceeds 60degC. How can I do it?

How can I do it?

1
4

The fan is not connected to the GPIO. The GPIO are 3V3 and only supply a few tens of milliamps of current. The fan is connected to the 5V and ground rail.

You will need to add circuitry to switch the fan on and off. The simplest is probably to add a transistor. The transistor would sit between the 5V supply and the fan. A GPIO could then be used to switch the transistor on and off.

You would need to write the control software to switch the GPIO/fan on when the Pi is too hot.

Hobby electronics shops should be a good source of typical transistors.

I'd probably aim for something which could switch at leas 500mA of current.

E.g. http://www.hobbytronics.co.uk/electronic-components/transistors/tip31c is overkill, but the others on that site only switch 100mA.

Once you find a suitable transistor its specs will show how much current is needed to the base to fully switch it on. Then calculate the needed resistance to allow that current to flow from a 3V3 GPIO.

3
  • Ok that seems really simple but after searching for a bit I can’t find any tutorial to show how to hook up a transistor to a raspberry pi 5v and control a 5v loaf such as a fan by gpio. What transistor to use what resistor if needed, etc. I think it’s obvious enough to someone that knows how that it doesn’t seem to be covered much. – T. M. Mar 24 '19 at 10:51
  • Thank, I found a couple basic tutorials as well. learn.adafruit.com/transistors-101?view=all learn.adafruit.com/… – T. M. Mar 24 '19 at 17:14
  • You don't even need to write the switching SW, there's a GPIO overlay for RPi that does this for you. – Piskvor left the building Nov 27 '19 at 13:17
4

It is simple to control a small fan with the Pi GPIO.

I use a simple transistor circuit (with only 3 inexpensive components).

I use the Pi 5V pin to power my fan, but this could be used with an external 12V supply for 12V fans.

This is suitable for fans which draw up to 500mA. Substitution of a power transistor could be used to power larger fans.

schematic

simulate this circuit – Schematic created using CircuitLab

The software is even simpler - only a single line needs to be added to config.txt

dtoverlay=gpio-fan,temp=60000

to switch the fan on at 60℃ - 60000 millicelcius (the default is 55℃ but you can select any temperature). The fan turns off when the temperature falls by 10℃.

This uses the gpio-fan overlay (which has been available in the kernel since late 2018) and should be included in recent versions of Raspbian. (Read /boot/overlays/README for description of this and other Device Tree overlays.)

3
  • cmdline.txt - or /boot/config.txt ? – Seamus Oct 14 '20 at 6:37
  • The diode serves no purpose here. Any BLDC motor already has freewheeling diodes inside (and much more). – Dmitry Grigoryev Oct 14 '20 at 13:42
  • @DmitryGrigoryev: too bad they don't also include a small transistor :) – Seamus Nov 10 '20 at 1:05
1

I'M USING THIS SCRIPT SAVED ON /home/runfan.py And the circuit is very simple using only a BD159, connect from GPIO to center of BD159 (connector number 2), and the negative to number 1 connector of BD159, and the negative fan wire to the number 3 connector of BD139.

#!/usr/bin/env python3
# Author: Edoardo Paolo Scalafiotti <edoardo849@gmail.com>

import os
from time import sleep
import signal
import sys
import RPi.GPIO as GPIO

pin = 2  # The pin ID, edit here to change it
maxTMP = 70 # The maximum temperature in Celsius after which we trigger the fan

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

def getCPUtemperature():
    res = os.popen('vcgencmd measure_temp').readline()
    temp =(res.replace("temp=","").replace("'C\n",""))
   # print("temp is {0}".format(temp)) #Uncomment here 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()
    else:
        fanOFF()
    return()
def setPin(mode): # A little redundant function but useful if you want to add logging
    GPIO.output(pin, mode)
    return()

try:
    setup() 
    while True:
        getTEMP()
    sleep(5) # Read the temperature every 5 sec, increase or decrease this limit if you want
except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt 
    GPIO.cleanup() # resets all GPIO ports used by this program
1
  • If you want to do some custom actions in fanOn()/fanOff() (e.g. send a notification), this is good. If you only need to pull GPIO high/low, use gpio-fan instead: does the same thing, but at a much lower level: no worrying about installed OS, script management, etc. – Piskvor left the building Nov 27 '19 at 13:19
1

An alternative to making a DIY circuit is to get a fan with speed control input. These typically have 4 wires (color code may vary):

BLACK        - Ground
YELLOW/RED   - +VDC
GREEN/YELLOW - Speed output signal
BLUE         - PWM input signal

enter image description here

PWM signal can be used to set the speed using a PWM (typically with 20-80% duty cycle). Some fans will allow you to use it as an ON/OFF signal without any modulation, but some will switch off if the duty cycle is outside of their valid range.

The pin can be driven by your script/application or the gpio-fan overlay. You'll have to check whether a 3.3V signal directly from GPIO is acceptable (unlikely on a 12V fan, possible with 5V).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.