5

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
  • 58,859
  • 17
  • 112
  • 227
terett
  • 159
  • 1
  • 1
  • 3
  • 3
    The fans you refer to (4-pins) is it the PC fans? If yes, then the FANS is 12 volt and then is some electronic interface/driver necessary. – Mats Karlsson Nov 09 '20 at 12:03
  • 4
    You'll always need additional circuitry to drive a fan on a Raspberry Pi (except if you hard-wire it to the 5V rail). That is because you cannot drive a fan from a GPIO pin. That would fry your Pi quite rapidly. You need at least a transistor as current amplifier. – PMF Nov 09 '20 at 15:20
  • PMF is correct, you cannot power a fan from a GPIO; at a minimum in order to use PWM you'd have to use a transistor on one of the power lines. However, a four pin fan may not require that (three pin ones do); it appears they use a control chip and an additional signal line. The third one, though, is still probably an analog input to measure the speed (or not!, see Sim Son's answer), so you'd need an ADC. I've edited this question to make it fit better with our no shopping restrictions. – goldilocks Nov 09 '20 at 16:50
  • @PMF What you mean by hardwire to the 5V rail? – terett Nov 14 '20 at 13:17
  • @terett : Just connect the 5V fan to the +5V (and GND) pins of the Pi. These deliver enough power to drive a fan. Of course, there won't be any regulation like this. But see the other answers, my comment may not be really precise when it comes to 4-Wire fans. – PMF Nov 14 '20 at 15:18

4 Answers4

4

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
  • 671
  • 3
  • 12
4

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
  • 141
  • 4
  • 1
    This is fantastic. Also, per Noctua's spec sheet "External pull-up is not necessary as the signal is pulled up to 3,3V/5V inside the fan." However, reading the RPM signal is 5V from what I can tell so a voltage level shifter will be needed. – Matthew Cordaro Jan 31 '23 at 22:55
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
  • 27,928
  • 6
  • 53
  • 144
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,491
  • 9
  • 15
  • 2
    Do you know any PWM controlled fan hat that is as small as possible? Or any guide that shows how to do it with female jumpers? I just want to avoid soldering. – terett Nov 09 '20 at 17:54
  • 1
    We generally do not give shopping advice, but the design on https://howchoo.com/g/ote2mjkzzta/control-raspberry-pi-fan-temperature-python is doable with jumpwires and https://www.waveshare.com/fan-hat.htm gives a fan-hat. I never tried them, so it is just what I found googleing. – Ljm Dullaart Nov 10 '20 at 07:50
  • In most cases, the level of Vcc is irrelevant because the PWM input typically is a open collector input compliant with TTL levels. So you can indeed simply connect Fan-Vcc to 12V and PWM to a GPIO without problems (that's the case for all fans I worked with so far) – Sim Son Nov 15 '20 at 11:06
  • With 5V, you can use the power supply of the Pi; with 12V you need a separate power supply. – Ljm Dullaart Nov 16 '20 at 16:29