geoffr
Posts: 85
Joined: Wed Aug 22, 2012 11:25 am
Location: Melbourne, VIC

picfan - A PWM cooling fan controller written in C

Wed Sep 11, 2019 7:06 am

With the Pi 4b+ being such "hot property" - quite literally speaking, I found the need to run a cooling fan. Given that my Pi4 still spends a lot of time doing very little, I don't want the fan running full tilt all the time. That led me to start working on controlling the fan using PWM.
The result is the beginnings of a daemon which provides PWM control of a fan which can be controlled via hardware PWM on GPIO 18 (pin 12).
I have now uploaded the code to GitHub: https://github.com/gmrza/picfan.

Please keep in mind that this is very much alpha code. If it blows up your Pi, don't blame me!
You can set the target temperature and a few other things either via a config file in /etc (picfan.conf) or using command line options.

Documentation is pretty much non-existent at the moment.

User avatar
PeterO
Posts: 5005
Joined: Sun Jul 22, 2012 4:14 pm

Re: picfan - A PWM cooling fan controller written in C

Wed Sep 11, 2019 8:14 am

A couple of questions....
What type of fans have you tried this with ?
Did you use any external circuit to control the fan ?

I'm currently using a fan-shim and controlling it with the kernel device configured with
"dtoverlay=gpio-fan,gpiopin=18,temp=65000"

ISTR the Pimoroni guys saying their fan doesn't work with PWM, but that may be a misinterpretation of "we don't use PWM" so I might just give it a try 8-)

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

geoffr
Posts: 85
Joined: Wed Aug 22, 2012 11:25 am
Location: Melbourne, VIC

Re: picfan - A PWM cooling fan controller written in C

Wed Sep 11, 2019 8:46 am

Hi Peter,

at the moment I'm using a cheap 30mm "Pifan" it is doing the job nicely of keeping a Pi4 cool. One reason why I didn't use the gpio-fan overlay is because it didn't seem to handle GPIO.
I probably can still improve the control, as it is challenging to respond quickly enough, but not overreact.
I'm using a separate circuit to drive the fan - it uses a NPN transistor together with a small capacitor to help smooth the output plus a flywheel diode. The reason I used a BJT rather than a MOSFET is because it is easier to find a BJT that can handle the current you might run with a fan - the smaller MOSFETs are a bit on the borderline in terms of the current they can handle.

I was debating looking at getting a 4-pin fan that already handles PWM, but gave up looking after I decided that it wouldn't be worth the effort to find one. I am still debating whether it wouldn't be worth shelling out on a Noctua fan for a production workload.

I am finding that most of the time with low CPU load, the fan is being run on about a 4% to 6% PWM duty cycle to maintain a temperature of 50 degrees. With this approach, using PWM, I manage to keep the fan fairly quiet.

User avatar
PeterO
Posts: 5005
Joined: Sun Jul 22, 2012 4:14 pm

Re: picfan - A PWM cooling fan controller written in C

Wed Sep 11, 2019 9:34 am

geoffr wrote:
Wed Sep 11, 2019 8:46 am
Hi Peter,

at the moment I'm using a cheap 30mm "Pifan" it is doing the job nicely of keeping a Pi4 cool. One reason why I didn't use the gpio-fan overlay is because it didn't seem to handle GPIO.
I probably can still improve the control, as it is challenging to respond quickly enough, but not overreact.
I'm using a separate circuit to drive the fan - it uses a NPN transistor together with a small capacitor to help smooth the output plus a flywheel diode. The reason I used a BJT rather than a MOSFET is because it is easier to find a BJT that can handle the current you might run with a fan - the smaller MOSFETs are a bit on the borderline in terms of the current they can handle.

I was debating looking at getting a 4-pin fan that already handles PWM, but gave up looking after I decided that it wouldn't be worth the effort to find one. I am still debating whether it wouldn't be worth shelling out on a Noctua fan for a production workload.

I am finding that most of the time with low CPU load, the fan is being run on about a 4% to 6% PWM duty cycle to maintain a temperature of 50 degrees. With this approach, using PWM, I manage to keep the fan fairly quiet.
Thanks for the answers/info Geoff.

The fan overlay was revised a few weeks ago as it was buggy but it works as advertised now :-) The fan-shim is quite quiet, it's about the same level as the USB hard disk I use so I hardly even notice it above the domestic ambient noise level.

OK about the buffer transistor,diode and capacitor. Is the capacitor across the fan and how big is it ?

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

geoffr
Posts: 85
Joined: Wed Aug 22, 2012 11:25 am
Location: Melbourne, VIC

Re: picfan - A PWM cooling fan controller written in C

Wed Sep 11, 2019 9:47 am

PeterO wrote:
Wed Sep 11, 2019 9:34 am

Thanks for the answers/info Geoff.

The fan overlay was revised a few weeks ago as it was buggy but it works as advertised now :-) The fan-shim is quite quiet, it's about the same level as the USB hard disk I use so I hardly even notice it above the domestic ambient noise level.

OK about the buffer transistor,diode and capacitor. Is the capacitor across the fan and how big is it ?

PeterO
Maybe my cheap fan is noisy ;-) Although I would find some hard disks a bit noisy.
I'm using a 100nF ceramic cap across the fan. The flywheel diode is probably a bit overspecced, as I happened to have a few 1N4001s knocking about.

Return to “Other projects”