4

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?

Seamus
  • 18,728
  • 2
  • 27
  • 57
romankh3
  • 125
  • 1
  • 1
  • 13
  • Found this nice [tuto](https://howchoo.com/g/ote2mjkzzta/control-raspberry-pi-fan-temperature-python) – Camille Jan 27 '21 at 16:13

4 Answers4

7

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.)

Milliways
  • 54,718
  • 26
  • 92
  • 182
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.

joan
  • 67,803
  • 5
  • 67
  • 102
  • 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. https://learn.adafruit.com/transistors-101?view=all https://learn.adafruit.com/adafruit-arduino-lesson-13-dc-motors?view=all – 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
2

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).

Dmitry Grigoryev
  • 26,688
  • 4
  • 44
  • 133
  • Dmitry, With a 4-pin fan like you describe, I assume the Ground and +VDC pins go to Ground and 5V on the Rasberry pi as you'd do with a normal 2-pin fan. What GPIO pins should the other two wires on the fan be connected to (the "Speed output signal" and "PWM input signal")? Will the single white connector in your fan picture need to be split into 2 or more connectors? Eric – Eric Claeys Apr 28 '21 at 01:47
  • @EricClaeys PWM input should be connected to a pin capable of PWM generation. Speed output can be left disconnected, unless you want to detect that your fan is damaged and needs to be replaced. And yes, you'll need to make a custom connector or terminate the fan wiring with jumper cables. – Dmitry Grigoryev May 05 '21 at 07:57
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
RalfFriedl
  • 2,160
  • 2
  • 9
  • 11
Nemesis
  • 19
  • 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