Created
May 7, 2023 16:12
-
-
Save jamincollins/2c57d5b86d9a9a828cb8c3a38a77aac7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import RPi.GPIO as GPIO | |
import sys | |
import time | |
class Fan: | |
rpm = 0 | |
def __init__(self, tach, pulse=2): | |
self.tach = tach | |
self.pulse = pulse | |
def __str__(self): | |
return f"tach: {self.tach}, pulse: {self.pulse}, rpm: {self.rpm}" | |
# pin configuration per fan | |
FANS = { | |
24: Fan(24) | |
} | |
WAIT_TIME = 1 | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setwarnings(False) | |
# setup tach settings | |
for pin, fan in FANS.items(): | |
GPIO.setup(fan.tach, GPIO.IN, pull_up_down=GPIO.PUD_UP) # pull up to 3.3v | |
t = time.time() | |
# caculate pulse frequency and RPM | |
def fell(pin): | |
global t | |
dt = time.time() - t | |
if dt < 0.005: return # Reject spuriously short pulses | |
freq = 1 / dt | |
FANS[pin].rpm = (freq / FANS[pin].pulse) * 60 | |
t = time.time() | |
# add event to detect | |
for pin, fan in FANS.items(): | |
GPIO.add_event_detect(fan.tach, GPIO.FALLING, fell) | |
try: | |
while True: | |
for pin, fan in FANS.items(): | |
print(f"pin: {pin} - {fan.rpm} RPM") | |
fan.rpm = 0 | |
time.sleep(1) | |
except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt | |
GPIO.cleanup() # resets all GPIO ports used by this function | |
# vim: ts=4 sw=4 expandtab |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment