FatalXception
Posts: 32
Joined: Fri Sep 25, 2015 10:55 pm

Fan control?

Sun Sep 27, 2015 8:22 pm

I have a RPi2b and I use it primarily to watch 1080p video. I have a heat sink and fan, which is powered by the GPIO header. How can I script the fan to only come on when the processor reaches a certain temp? Do I need to use certain pins on the header? Do I need a particular fan? Let me know. Thanks!

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Fan control?

Sun Sep 27, 2015 8:35 pm

How can we tell? Tell us how the fan you have is connected and controlled.

User avatar
rpdom
Posts: 17173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Fan control?

Sun Sep 27, 2015 8:39 pm

As usual when asked a question like this the standard answer is "Why would you want to put a heatsink and fan on a Pi?"

If you are just doing as an exercise there are answers.

You can use pretty much any gpio pin to control a fan if all you need is simple on/off switching, but you will need a driver circuit as the gpio can't supply the right voltage and current for a fan and the back emf from the fan can destroy the Pi.

There are examples of motor driver circuits on the forums as well as ways to check the temperature and turn gpios on and off.

Also look for examples in the MagPi magazine.

To check the temperature you can use "vcgencmd measure_temp". If that ever reads over 85°C you need a fan (Clue: It will never reach 85°C as the Pi will slow down to prevent it happening). ;)

FatalXception
Posts: 32
Joined: Fri Sep 25, 2015 10:55 pm

Re: Fan control?

Sun Sep 27, 2015 9:02 pm

The fan is a 5v fan on pins 4 and 6 and is always on.
I have a few reasons- part of it is that I am running video and my other solutions have always been weak in heat dissipation. Also I know it's possible and holds geek appeal for me. Also I'd like to reduce the noise and power consumption.

W. H. Heydt
Posts: 12648
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Fan control?

Sun Sep 27, 2015 10:31 pm

FatalXception wrote:The fan is a 5v fan on pins 4 and 6 and is always on.
I have a few reasons- part of it is that I am running video and my other solutions have always been weak in heat dissipation. Also I know it's possible and holds geek appeal for me. Also I'd like to reduce the noise and power consumption.
The way you reduce noise and power consumption is...don't use a fan. The Pi doesn't need it.

FatalXception
Posts: 32
Joined: Fri Sep 25, 2015 10:55 pm

Re: Fan control?

Tue Sep 29, 2015 6:16 pm

Maybe the Pi doesn't need a fan, but sometimes it helps to go through an exercise like this because the application can be altered or extended- I learned a lot from this script for example because it shows how simple the logic is to program for the Pi and it opened the lid of the black box for me. Plus, I thought it was pretty cool to be able to use the Pi as a controller of sorts as well as the other applications I have in mind.
Take a look at the following link- I have also included the script below it for simplicity. Looks like al I have to do is to move my connector over one pin and this will work. Not only that, but I'm going to lay with it and see how the properties translate across- perhaps by flipping true ad false I can reverse the direction of the fan, and create a low-noise (albeit less efficient) airflow. I know there isn't a huge utility to it, but like I said, I'm in it for the geek appeal. Hoping this is helpful to others on this forum:

https://github.com/andrewkarch/Eleduino-Fan-Controller

A simple python script to control the fan of the Eleduino 2015 New Version Raspberry Pi 2 mode B Transparent Acrylic Case With Fan

Once the case is assembled connect the black wire to pin 6 and red pin to 8

Boot the Raspberry Pi

Download the script

git clone https://github.com/andrewkarch/Eleduino ... roller.git

Open your rc.local file

sudo nano /etc/rc.local

Add this line to your /etc/rc.local If you didn't change the working directory (cd) when downloading the script, the directory will be be /home/pi/Eleduino-Fan-Controller

sudo python /directory/to/fanController.py &

Reboot and the fan will now turn on if the temperature gets above 45C. To change the temperature open the script in leafpad, and change maxTemp to the temperature you want the fan to turn on at in Celcius.

Script for Pi

import os

import time

import RPi.GPIO as GPIO



#Change this to whatever temperature you want the fan to kick on at.
maxTemp = 45 #Celsius

#Sets the time to wait in between checks
waitTime = 5 #seconds



GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(8, GPIO.OUT)

while(True):
temperature = os.popen('vcgencmd measure_temp').readline()
tempInC = float(temperature.replace("temp=","").replace("'C\n",""))
if(tempInC >= maxTemp):
GPIO.output(8, True)
else:
GPIO.output(8, False)
time.sleep(waitTime)

JimmyN
Posts: 1109
Joined: Wed Mar 18, 2015 7:05 pm
Location: Virginia, USA

Re: Fan control?

Tue Sep 29, 2015 6:43 pm

Pin #4 is the 5V rail, it's not an IO pin, and it can provide enough current to run a small fan. But if you move over to Pin #8 that's an IO pin with very limited current available. If you try to power a fan directly from Pin #8 your RPi may have a short life.

What does the label on the fan say regarding voltage and current?

FatalXception
Posts: 32
Joined: Fri Sep 25, 2015 10:55 pm

Re: Fan control?

Tue Sep 29, 2015 6:57 pm

Thanks for the heads up- Not sure on the current, but it is a 5v fan. Can the script be adapted to turn off current to pin 4? I guess not if it's a rail right? what would I have to do to make something like this work- would I need to build hardware or could I use a different pin?

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: Fan control?

Tue Sep 29, 2015 8:00 pm

FatalXception wrote:Thanks for the heads up- Not sure on the current, but it is a 5v fan. Can the script be adapted to turn off current to pin 4? I guess not if it's a rail right? what would I have to do to make something like this work- would I need to build hardware or could I use a different pin?
You would use a transistor to turn the fan on and off.
Doug.
Building Management Systems Engineer.

JimmyN
Posts: 1109
Joined: Wed Mar 18, 2015 7:05 pm
Location: Virginia, USA

Re: Fan control?

Tue Sep 29, 2015 8:11 pm

As @ BMS Doug pointed out you'll need a transistor. The base leg of the transistor would be controlled by Pin #8 to turn the fan on and off.

FatalXception
Posts: 32
Joined: Fri Sep 25, 2015 10:55 pm

Re: Fan control?

Tue Sep 29, 2015 8:29 pm

It's been a while since I've done any electronics work- The base goes to 8, one of the others goes to the fan and the last to the positive pin (Doesn't matter which because the 8 pin acts as a gateway, right?)? Also, how do I choose a transistor? fan is 5v. Thanks for all your help!

boyoh
Posts: 1468
Joined: Fri Nov 23, 2012 3:30 pm
Location: Selby. North Yorkshire .UK

Re: Fan control?

Tue Sep 29, 2015 9:44 pm

You seem to be good at scripting, I think you will struggle with the electronics on projects
You was asked what the fan current was , you said you did not know, You should have don
a current check on the fan, this could be don with a multi meter. When building projects you
must know the voltage and current of the components you will be using, to integrate them
in a circuit, Happy Geeking
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

texy
Forum Moderator
Forum Moderator
Posts: 5161
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Fan control?

Wed Sep 30, 2015 11:03 am

FatalXception wrote:It's been a while since I've done any electronics work- The base goes to 8, one of the others goes to the fan and the last to the positive pin (Doesn't matter which because the 8 pin acts as a gateway, right?)? Also, how do I choose a transistor? fan is 5v. Thanks for all your help!
The choice of the transistor will be dependant on the current that the fan uses. However I suspect that that little fan will draw less than 200mA and that 'any general purpose NPN transistor' will be suitable. Google is your friend - I found this although it uses a 2N7000 MOSFET :
http://www.next.gr/circuits/relay-How-c ... 25995.html

Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

Return to “Beginners”