Vaakkux
Posts: 13
Joined: Tue Sep 11, 2018 11:25 am
Location: UK

Automatic Cooling Fan on Pi

Wed Nov 28, 2018 10:01 am

I just bought a small fan for the raspberry pi. However I only want it to run after it exceeds a certain temperature.

Is it possible to do this either as a python script or C program. As if so how do I use the GPIO pins on either of these languages?

tqhien
Posts: 79
Joined: Thu Feb 02, 2012 10:07 am

Re: Automatic Cooling Fan on Pi

Wed Nov 28, 2018 11:29 am

You can get cpu temperature with the device virtual filesystem, like this (python) :

Code: Select all

    try:
        tfile = open('/sys/class/thermal/thermal_zone0/temp')
        t = tfile.read()
        temperature = float(t)/1000
        tfile.close()
    except:
        pass
then test temperature and trigger your fan (think about hysteresis so the fan does not flicker on/off around the threshold temp)

pcmanbob
Posts: 12198
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Automatic Cooling Fan on Pi

Wed Nov 28, 2018 11:35 am

If you have never used the gpio with python a good starting point would be the physical computing with python guide on raspberrypi.org

https://projects.raspberrypi.org/en/pro ... -computing

if you just want help with connecting your fan to the gpio there are plenty of examples on the web and on this very forum.

https://www.google.com/search?q=connect ... =firefox-b
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 12368
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Automatic Cooling Fan on Pi

Wed Nov 28, 2018 1:58 pm

Why not let the kernel do it for you? There's already the required device driver and DT overlay available.
https://github.com/raspberrypi/linux/bl ... EADME#L571
Add:

Code: Select all

dtoverlay=gpio-fan,gpiopin=12,temp=55000
to /boot/config.txt and gpio 12 should become active when the processor hits 55degC.

Use a standard transistor drive to connect the GPIO to the fan.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

Return to “General discussion”