Mee
Mee

Reputation: 191

Can I get RPM pulses from an Aux 12V fan on RPi zero?

I have been checking the forums for this question, but didn't find my answer. I would appreciate if you could please help me with.

I have a 3-wire 12V DC fan connected to RPi Zero W via a relay module. The thrid wire of the fan is producing RPM pulses (mentioned in the Datasheet) which I want to use as an indicator to check the status of the fan, the fan is on or not.

I did a search and found it that I shouldn't connect the third wire directly to the GPIOs because might have a similar voltage as input voltage of the fan. Is this correct? if so, how would be the safest way to connect to read the pulses?

Many thanks

Upvotes: -1

Views: 23

Answers (1)

Allen Chak
Allen Chak

Reputation: 1960

enter image description here

Remember that, the example provided information for Fan using DC5V / 12V / 24V.

import RPi.GPIO as GPIO
import time

# Pin configuration
TACH = 24       # Fan's tachometer output pin
PULSE = 2       # Noctua fans puts out two pluses per revolution
WAIT_TIME = 1   # [s] Time to wait between each refresh

# Setup GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(TACH, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Pull up to 3.3V

# Setup variables
t = time.time()
rpm = 0

# Caculate pulse frequency and RPM
def fell(n):
    global t
    global rpm

    dt = time.time() - t
    if dt < 0.005: return # Reject spuriously short pulses

    freq = 1 / dt
    rpm = (freq / PULSE) * 60
    t = time.time()

# Add event to detect
GPIO.add_event_detect(TACH, GPIO.FALLING, fell)

try:
    while True:
        print "%.f RPM" % rpm
        rpm = 0
        time.sleep(1)   # Detect every second

except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt
    GPIO.cleanup() # resets all GPIO ports used by this function

Reference: https://blog.driftking.tw/en/2019/11/Using-Raspberry-Pi-to-Control-a-PWM-Fan-and-Monitor-its-Speed/

Upvotes: 1

Related Questions