DEV Community

Andrei Telteu
Andrei Telteu

Posted on

How to control a Noctua NF-A20 PWM fan from Raspberry Pi (OFF or 20%-100%)

demo gif

I am making my homelab and I need to control the Noctua NF-A20 PWM chromax.black.swap 200x200mm fan from Raspberry Pi, and this is how I've done it.

Parts you need:

Diagram:

diagram image

Let's start from the Pi:

1- Connect a wire from GPIO pin 16, or any simple gpio pin, to the 1k resistor, and then the other end of the transistor to the middle pin of the transistor.

2- A wire from the right pin of the transistor to any ground pin of the Pi. The face of the transistor is the flat one.

3- A wire from the left pin of the transistor to the "IN-" 5V negative input of the power convertor.

4- A wire from one of the 5V Pi pins (top right) to the "IN+" 5V positive input of the power convertor.

5- Important: adjust the converter to 12V or lower.
Do NOT connect the fan right now because you can permanently damage it because of high voltage.
    A- Turn on the converter by connecting a ground pin from the Pi directly to the Power Converter's "IN-" connector OR by runing the code from the next section and just calling speed(fan, 100) / time.sleep(180).
    B- Connect a multimeter to the converter's "OUT+" and "OUT-" and select voltage.
    C- Turn the small yellow screw until the multimeter shows 12V or a bit smaller to be safe. I have mine at 11.5V.

wer converter adjustment nob

    D- When you are done disconnect the multimeter and the ground wire, or turn off the script.

The pinout from the noctua fan I found here:
https://faqs.noctua.at/en/support/solutions/articles/101000081757-what-pin-configuration-do-noctua-fans-use-
noctua pinout image

5- Wire from power convertor "OUT-" (12V negative) to noctua pin 1

6- Wire from power convertor "OUT+" (12V positive) to noctua pin 2

5- Wire from noctua pin 4 to Pi GPIO pin 12. This pin has PWM capability.

The code:

I used the code from this tutorial published on the-diy-life.com by Michael Klements (original code), and I modified it to use the transistor to turn off the fan, because the fan PWM does not scale down to 0 RPM. The minimum rotational speed is 20% (350RPM).

#!/usr/bin/env python3
import RPi.GPIO as GPIO
import time
import signal
import sys

# The Noctua PWM control actually wants 25 kHz (kilo!), see page 6 on:
# https://noctua.at/pub/media/wysiwyg/Noctua_PWM_specifications_white_paper.pdf
PWM_FREQ = 25           # [Hz] PWM frequency
FAN_PIN = 12            # BCM pin used to drive PWM fan
TRAN_PIN = 16           # NPN Transistor

def speed(fan, percent):
    print("set fan to", percent)
    if (percent < 20):
        GPIO.output(TRAN_PIN, False)
        fan.ChangeDutyCycle(20)
    else:
        GPIO.output(TRAN_PIN, True)
        fan.ChangeDutyCycle(percent)

try:
    signal.signal(signal.SIGTERM, lambda *args: sys.exit(0))
    GPIO.setwarnings(True)
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(TRAN_PIN, GPIO.OUT)
    GPIO.setup(FAN_PIN, GPIO.OUT, initial=GPIO.LOW)
    fan = GPIO.PWM(FAN_PIN, PWM_FREQ)

    # do a few cycles just for testing
    speed(fan, 100)
    time.sleep(30)

    speed(fan, 50)
    time.sleep(30)

    speed(fan, 0)
    time.sleep(30)

    speed(fan, 20)
    time.sleep(30)

    speed(fan, 70)
    time.sleep(30)

    speed(fan, 100)
    time.sleep(30)

except KeyboardInterrupt:
    pass

finally:
    GPIO.cleanup()
Enter fullscreen mode Exit fullscreen mode

Done. Happy tinkering !

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (3)

Collapse
 
krazz profile image
Göran Karlsson

Nice write-up.
I am about to using a 12v Noctua fan myself when I stumbled across your article.
What got me confused though, is that you connect the PWM lead directly to the Pi.
As I understand it, in the Noctua 12v fans, the PWM lead is 5v and not 3.3v.
Wouldn't this destroy the GPIO on the Pi?

Collapse
 
andreitelteu profile image
Andrei Telteu • Edited

Thank you.
I don't know if it fries the gpio pin.
I used it in this configuration for about 50 hours without any issues.
AI says this:

To safely connect your Noctua PWM fan to the Raspberry Pi 4 Model B, follow these steps to protect the GPIO pins and ensure proper functionality:

  1. Voltage Level Adjustment:
    • Voltage Divider: Use a voltage divider with a 1kΩ and 680Ω resistor to step down the 5V PWM signal to 3.3V. This configuration ensures the GPIO pin receives a safe voltage.
    • Level Shifter: Alternatively, use a bidirectional level shifter module to convert the 5V signal to 3.3V, providing reliable voltage conversion without backfeeding.
  2. PWM Signal Considerations:
    • Verify that the PWM frequency from the fan (typically 20-30 kHz) is compatible with the Raspberry Pi's PWM capabilities.
  3. RPM Signal Handling:
    • If using the RPM signal, ensure it is connected with a pull-up resistor to 3.3V to match the Raspberry Pi's GPIO requirements.
  4. Software Configuration:
    • Use Python libraries like RPi.GPIO to control the fan's PWM signal, allowing you to adjust fan speed based on system parameters such as temperature.

How to connect the Resistors:

  1. Connect the 5V PWM signal from the fan to one end of the 1kΩ resistor (R1).
  2. Connect the other end of the 1kΩ resistor to one end of the 680Ω resistor (R2).
  3. Connect the other end of the 680Ω resistor to ground (GND).
  4. The junction between the two resistors (R1 and R2) is the output of the voltage divider. Connect this junction to the GPIO pin (e.g., GPIO 12) on your Raspberry Pi.

I will update this article if I make this modifications.

Collapse
 
krazz profile image
Göran Karlsson

I was thinking in reverse. The GPIO pin is in output and shouldn't get harmed sine we're controlling the fan.
The fan's PWM is indeed 5V but the 3.3V coming from the GPIO is enough for a trigger in the fan. 😉

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more