Tal O
Published © GPL3+

Raspberry pi 4 GPIO controlled cooling fan

When it gets too hot

BeginnerFull instructions provided1 hour2,080
Raspberry pi 4 GPIO controlled cooling fan

Things used in this project

Hardware components

Raspberry Pi 4 Model B
Raspberry Pi 4 Model B
×1
cooling fan for rpi
×1
NPN s8050
×1
Resistor 1k ohm
Resistor 1k ohm
×1
Resistor 220 ohm
Resistor 220 ohm
×1
LED (generic)
LED (generic)
×1
1N4007 – High Voltage, High Current Rated Diode
1N4007 – High Voltage, High Current Rated Diode
×1
Toggle Switch, (On)-Off-(On)
Toggle Switch, (On)-Off-(On)
×1

Story

Read more

Schematics

cooling_bb_9Bhghlq0Xw.png

Code

python code

Python
# code by talofer99@hotmail.com
# temp control with cooling fan.
# connect a fan via a npn transistor.
# power from the 5V of the rpi.

import os
import time
import RPi.GPIO as GPIO
fanPin = 26
GPIO.setmode(GPIO.BCM)
GPIO.setup(fanPin, GPIO.OUT)
threshHoldTempOn = 45
threshHoldTempOff = 40
pwmPrecentCycle = 75
p = GPIO.PWM(fanPin, 200)  # frequency=200Hz
p.start(0)

def measure_temp():
        temp = os.popen("vcgencmd measure_temp").readline()
        return float((temp.replace("temp=","").replace("\'C\n","")))

try:
    while True:
        currentTemp = measure_temp()
        if currentTemp >= threshHoldTempOn:
            p.ChangeDutyCycle(pwmPrecentCycle)
            #GPIO.output(fanPin, GPIO.HIGH)
            print ("hot")
        elif currentTemp <= threshHoldTempOff:
            p.ChangeDutyCycle(0)
            #p.stop()
            #GPIO.output(fanPin, GPIO.LOW)
            print ("ok")
        else:
            print ("no chnage")
        print(currentTemp)
        print("----------------")
        
        time.sleep(30)

except KeyboardInterrupt:
    GPIO.cleanup()
    print("Press Ctrl-C to terminate while statement")
    pass

Credits

Tal O

Tal O

20 projects • 55 followers
Maker @ heart

Comments