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?

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

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

Posts: 12841
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 ... -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. ... =firefox-b
We want information… information… information no help
The use of crystal balls & mind reading are not supported

Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 12848
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. ... EADME#L571

Code: Select all

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”