Advertisement
foxyBB

PWM Fan Raspbery PI

May 21st, 2025
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.22 KB | Source Code | 0 0
  1. import RPi.GPIO as GPIO
  2. import time
  3.  
  4. # --- Pin Configuration ---
  5. FAN_TACH_PIN = 24         # Fan's tachometer signal pin (output)
  6. PULSES_PER_REV = 2        # Noctua fans output 2 pulses per revolution
  7. MEASUREMENT_INTERVAL = 1  # Time between measurements [seconds]
  8.  
  9. # --- GPIO Setup ---
  10. GPIO.setmode(GPIO.BCM)
  11. GPIO.setwarnings(False)
  12. GPIO.setup(FAN_TACH_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # Enable pull-up to 3.3V
  13.  
  14. # --- Measurement Variables ---
  15. last_pulse_time = time.time()
  16. current_rpm = 0
  17.  
  18. # --- Pulse Falling Edge Callback ---
  19. def on_falling_edge(channel):
  20.     global last_pulse_time
  21.     global current_rpm
  22.  
  23.     now = time.time()
  24.     delta_time = now - last_pulse_time
  25.  
  26.     if delta_time < 0.005:
  27.         return  # Ignore spurious noise or bounce
  28.  
  29.     pulse_frequency = 1.0 / delta_time
  30.     current_rpm = (pulse_frequency / PULSES_PER_REV) * 60
  31.     last_pulse_time = now
  32.  
  33. # --- Attach Interrupt Handler ---
  34. GPIO.add_event_detect(FAN_TACH_PIN, GPIO.FALLING, on_falling_edge)
  35.  
  36. # --- Main Loop ---
  37. try:
  38.     while True:
  39.         print(f"{current_rpm:.0f} RPM")
  40.         current_rpm = 0
  41.         time.sleep(MEASUREMENT_INTERVAL)
  42.  
  43. except KeyboardInterrupt:
  44.     GPIO.cleanup()  # Reset all GPIO pins used
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement