7

I'm interested in adding a fan to my Raspberry Pi 4, but I would like to be able to control when it is on/off programatically. However, what I see is that most of the fans suitable for Raspberry Pi come with 2 pins to connect to 3.3/5V and ground pin, which means you cannot control them unless you add some additional circuitry.

Is it possible to use a fan that has 4-pins, and not add any additional circuitry at all?

goldilocks
  • 60,325
  • 17
  • 117
  • 234

4 Answers4

6

Got a Noctua NF-A4x10 5V PWM hooked up to the 3.3v pin (pin 1), ground (pin 6), and the PWM signal line to GPIO 18 (pin 12) on a Raspberry Pi 4.

Added dtoverlay=pwm,pin=18,func=2 (pwm/pwm-2chan docs) in my /boot/config.txt & rebooted to route hardware PWM channel 0 to GPIO 18 & enable the Linux kernel PWM driver sysfs interface.

At that point /sys/class/pwm/pwmchip0 existed and I 'exported' PWM channel 0 via:

# echo 0 > /sys/class/pwm/pwmchip0/export 

...which created /sys/class/pwm/pwmchip0/pwm0/:

$ ls /sys/class/pwm/pwmchip0/pwm0/
capture  duty_cycle  enable  period  polarity  power  uevent

Looking at Noctua's PWM specsheet they want a 25 kHz PWM signal, which translates into (1 / 25000) seconds = 40000 nanoseconds.

Dropped that value (40000 ns) into period, 50% (20000 ns) into duty_cycle, and 1 into enable:

# echo 40000 > /sys/class/pwm/pwmchip0/pwm0/period
# echo 20000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
# echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable

...and got a (much) quieter fan!

Despite being a 5v fan the NF-A4x10 seems perfectly happy (albeit slower) with both 3.3v power and 3.3v PWM signaling.

Those values can be set at startup via a tmpfiles.d file (I called mine /etc/tmpfiles.d/pwmfan.conf):

w /sys/class/pwm/pwmchip0/export             - - - - 0
w /sys/class/pwm/pwmchip0/pwm0/period        - - - - 40000
w /sys/class/pwm/pwmchip0/pwm0/duty_cycle    - - - - 25000
w /sys/class/pwm/pwmchip0/pwm0/enable        - - - - 1

And finally you can tweak the EEPROM config (sudo -E rpi-eeprom-config --edit) to power off the PWM outputs at shutdown so the fan isn't left spinning:

WAKE_ON_GPIO=0
POWER_OFF_ON_HALT=1
genpfault
  • 161
  • 1
  • 5
5

You can typically simply connect the PWM input to a GPIO providing a 25kHz PWM signal. I don't agree with the other answers, which all mention to keep Vcc within the logic level of the Pi, which from my experience is not neccessary! Fan controllers in 4-pin fans have an open collector input for PWM control and thus are made to be controlled by typical logic levels. In their newer PC fan specification Intel suggests that fans should be made compatible with 3.3V logic and manufacturers seem follow this direction as many modern fans of reputable brand are explicitly compatible with 3.3V logic.

There are many products on the market and to be absolutely sure if your individual fan is fine to work with in this way, you should connect PWM to ground through a large resistor (10kOhm) and measure the voltage at the PWM pin when the fan is connected to 12V and GND. If the voltage on the PWM pin is within you logic level you're fine.

Also, unlike mentioned in a comment, you don't need an ADC to read the tacho signal: it's a pulse signal (also open collector signal) that generates to drops to GND per revolution. All you need to control a 4-pin fan is 1 (or maybe 2) pullups of around 10kOhm.

Sim Son
  • 681
  • 4
  • 13
3

Yes, if you find a fan which supports 3.3V levels on speed control pin, there is no reason it wouldn't work from a GPIO. I'd expect 5V fans also work in this way if you power them with 3.3V, albeit with a reduced max speed.

Personally, I invested some time into finding a simple 2-pin fan which is really quiet, and simply keep it running all the time. Pi 4 consumes 2.7W doing absolutely nothing, and while you don't strictly need active cooling at 2.7W, it's not useless either. On the other hand, Pi 4 SoC consumption maxes out at 6.4W, so you don't need a huge airflow to keep the temperature in check.

Dmitry Grigoryev
  • 28,277
  • 6
  • 54
  • 147
2

Erm,... Yes you can, but you shouldn't.

There are a few 3V fans that fit that description, even one by some Chinese firm that could be powered by the GPIO pin. Do not expect it to create a significant airflow though.

If you're afraid of soldering, there is a PWM controlled fan hat. Or you can use female jumper wires.

---EDIT--- There are a number of four wire fans available that use 5V for power and 3.3V for the PWM input. Some newer boxed Intel processors have them (older seem to use 5V for the PWM input)

You would then use

pin1 - gnd
pin2 - +5V
pin3 - sense
pin4 - pwm

But also that is difficult to realize without soldering:

  • the connector will probably not fit over the header connector
  • you should protect the GPIO pins (sense and pwm) with a 3.3V zener diode
Ljm Dullaart
  • 2,539
  • 11
  • 16