Raspberry Pi GPIO PWM fan controller
A daemon that uses the hardware PWM of a Raspberry Pi's GPIO 18 pin (PWM channel 0) to properly control a fan. Full setup, docs and getting started, even the main program is still WIP.
/run
structure (will be changed at some point):
/run
├── rpi-fanctl
│ ├── pid # Daemon PID
│ ├── speed # Current duty cycle as number from 0 to 100
│ └── control # Unix Domain Socket to set manual fan speed
/etc
structure:
/etc
├── rpi-fanctl
│ └── config.toml # Config file with main settings and fan curve
I have two fans that support PWM and one that is just a fan™️, which I'll take as my reference. Note that I only have a Raspberry Pi 4, so I was not able to test any of this for the Raspberry Pi 5 and its dedicated JST fan connector.
Here is how you want to connect the fan pins:
- GND: Any Ground pin on the GPIO
- 5V: One of the two 5V pins on the GPIO
- Tachometer: Any GPIO data pin. (Actually, it has to be a specific one, but I will change that when I get there with my code.)
- PWM: One of the two PWM pins of
PWM0
.
Note the 5V and PWM in its name. I highly recommend this fan, since it can run on very low fan speeds and still cool very well while being very silent.
The standard 4-pin fan connector features power, dedicated PWM control and a tachometer. I used a cheap 4-pin fan cable extension, cut it and soldered 4 Dupont cables to it. I didn't want to cut the fan wire itself due to warranty, so its length is also somewhat an issue.
You might want to also add a 4.7kΩ resistor across GND and PWM. Otherwise when the Pi is booting or when has shut down but is still connected to power, the fan will ramp up to 100% since the PWM GPIO pin is essentially not connected anymore and the internal pull-up resistor starts the fan.
This fan came with one of GeeekPi's Low Profile Ice Tower coolers. It does not have a tachometer, but that's okay since this is very optional.
It has 3 pins: 5V, GND and PWM. Just connect then as stated above and it should just work. The RGB light will always stay on as long as the 5V rail is powered, but the fan will only turn on if the PWM pin is active.
I have three of those. One with automatic RGB from an Ice Tower cooler and two without RGB. All of them are 40mm fans with just 5V and GND.
I tried controlling then using an N-Channel Transistor after the fan itself. I also needed a Base resistor of about 1.8kΩ connected to PWM and a capacitor across Collector and Emittor. I tried many PWM speed signals as well as various capacitor values, but you can't really control this fan. Either it's too slow and even makes switching noise or it's already so fast that you can hear it.