Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import RPi.GPIO as GPIO
- import time
- # --- Pin Configuration ---
- FAN_TACH_PIN = 24 # Fan's tachometer signal pin (output)
- PULSES_PER_REV = 2 # Noctua fans output 2 pulses per revolution
- MEASUREMENT_INTERVAL = 1 # Time between measurements [seconds]
- # --- GPIO Setup ---
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- GPIO.setup(FAN_TACH_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Enable pull-up to 3.3V
- # --- Measurement Variables ---
- last_pulse_time = time.time()
- current_rpm = 0
- # --- Pulse Falling Edge Callback ---
- def on_falling_edge(channel):
- global last_pulse_time
- global current_rpm
- now = time.time()
- delta_time = now - last_pulse_time
- if delta_time < 0.005:
- return # Ignore spurious noise or bounce
- pulse_frequency = 1.0 / delta_time
- current_rpm = (pulse_frequency / PULSES_PER_REV) * 60
- last_pulse_time = now
- # --- Attach Interrupt Handler ---
- GPIO.add_event_detect(FAN_TACH_PIN, GPIO.FALLING, on_falling_edge)
- # --- Main Loop ---
- try:
- while True:
- print(f"{current_rpm:.0f} RPM")
- current_rpm = 0
- time.sleep(MEASUREMENT_INTERVAL)
- except KeyboardInterrupt:
- GPIO.cleanup() # Reset all GPIO pins used
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement