BaReinhard's Tech Blog

A blog of various technologies, largely based around the Raspberry Pi.

Follow me on GitHub Follow me on Instagram

Cooling the Raspberry Pi

If you’ve done any stress testing or used your pi for any CPU-intensive processes, you are probably aware of the possible need for some form of cooling.

If you are using a Raspberry Pi 2 or older you likely will only need a heatsink, but given proper circumstances you may need a fan.

However, with the newer Raspberry Pi 3 board, they tend to run much warmer than the predecessors. I currently have my Raspberry Pi 3 running OSMC in an enclosed box with a; HifiBerry Amp+, a 12v fan, a 5v fan, and a infrared sensor attached for use with a remote. As you can imagine the Pi tends to get very warm, even though it is fitted with 4 heatsinks, 3 on the Pi and 1 on the Amp+.

To check your current temp use the following command:

/opt/vc/bin/vcgencmd measure_temp

You should see a similar output:

osmc@BKKHomeMediaCenter:~$ /opt/vc/bin/vcgencmd measure_temp
temp=47.2'C

Given, that I am a pretty lazy person in the sense I don’t like doing things that can be simplified or done automatically, I chose to give an alias the the previous command.

To setup an alias you will want to open your .bashrc file in your users home directory.

nano ~/.bashrc

Inside you can place the following line:

alias temp='/opt/vc/bin/vcgencmd measure_temp'

Where I have used the word temp before the equal sign, you can use any other word that you would prefer. Now save the file, you will need to reboot your Pi before you will be able to use the alias.

Now that we can easily check our temperature, I would suggest you do some R&D. Use your Pi, to stream videos or whatever CPU intensive processes you use when it starts to get warm, then check its temperature and try to do it on a consistent basis.

Once you get a good baseline on what temperature your Pi will spend most of its time at then we can customize a script I have already made.

import RPi.GPIO as GPIO
import time
import atexit
import subprocess
def exit_handler():
	pwm.stop()
	GPIO.cleanup()
def FanOn(dutyCycle,fanTime,pwm,pwme):
	process = subprocess.Popen('/opt/vc/bin/vcgencmd measure_temp',stdout = subprocess.PIPE,shell=True)
	temp,err = process.communicate()
	temp = str(temp).replace("temp=","")
	temp = str(temp).replace("\'C\n","")
	temp = float(temp)
	start=exit = int(time.time())
	pwme.ChangeDutyCycle(dutyCycle)
	pwm.ChangeDutyCycle(dutyCycle)
	while(temp > 50):                              #Change this line if you want the fans to turn off at a different temperature
		exit = int(time.time())
#		if (dutyCycle = 0):
#			time.sleep(300)
#		else:
		process = subprocess.Popen('/opt/vc/bin/vcgencmd measure_temp',stdout = subprocess.PIPE,shell=True)
		temp,err = process.communicate()
		temp = str(temp).replace("temp=","")
		temp = str(temp).replace("\'C\n","")
		temp = float(temp)
		if (int(exit - start) > 400):
			pwm.ChangeDutyCycle(100)
			pwme.ChangeDutyCycle(100)
		time.sleep(10)

GPIO.setmode(GPIO.BCM)
GPIO.setup(25,GPIO.OUT)
GPIO.setup(23,GPIO.OUT)
pwme = GPIO.PWM(23,50)
pwm = GPIO.PWM(25,50)
pwme.start(100)
pwm.start(0)
atexit.register(exit_handler)
try:
	while 1:
		process = subprocess.Popen('/opt/vc/bin/vcgencmd measure_temp',stdout = subprocess.PIPE,shell=True)
		temp,err = process.communicate()
		temp = str(temp).replace("temp=","")
		temp = str(temp).replace("\'C\n","")
		temp = float(temp)
		if(temp<78):                            #Change this line if you want the fans to turn on at a different temp
			time.sleep(10)		
#	FanOn(0,10,pwm,pwme)
		else:
			FanOn(25,180,pwm,pwme)
		pwm.ChangeDutyCycle(0)
		pwme.ChangeDutyCycle(0)
finally:
	exit_handler()

The current script will use GPIO 23 (pin 16) and GPIO 25 (pin 22).

Unfortunately, you cannot simply connect your Fan power sources to these leads, you will need to use a N-Channel MOSFET. Something along the lines of this: N-Channel MOSFET

You will want to connect the Gate Lead of the MOSFET (Left Leg) to the GPIO Pin, then the Drain (Middle Leg) to the negative leg of the fan, then the Source Lead (Right Leg) to a ground pin on the Pi. You will need 2 MOSFETs for the above script and 2 fans. I suggest using one for pushing new air into the box (over the heatsinks) and another pulling air out of the box. This setup will allow you to use PWM and set the fans at submaximal speeds so that they wont be too loud, but still have the option of cooling quicker if need be.

If you feel as though you don’t care about noise and just want it to stay as cool as possible whenever the Pi is on, just connect your 5v fan to the 5v + and the ground, pins 4 and 6 respectively.

The above may be overkill for most of you, but in certain cirumstances this may be necessary. I hope this helps you to better decide what may or may not necessary for your Pi.