Created
October 9, 2016 04:53
-
-
Save rxseger/2b27e661221b6f350b859d13f256cf29 to your computer and use it in GitHub Desktop.
read RPM from a PC fan tachometer wired to GPIO
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/python -u | |
# tachfan.py - read RPM from a PC fan tachometer wired to GPIO | |
# | |
# references: | |
# http://electronics.stackexchange.com/questions/8295/how-to-interpret-the-output-of-a-3-pin-computer-fan-speed-sensor | |
# http://www.formfactors.org/developer/specs/REV1_2_Public.pdf | |
import RPi.GPIO as GPIO | |
import time | |
GPIO.setmode(GPIO.BOARD) | |
TACH = 36 # BCM 16 | |
GPIO.setwarnings(False) | |
GPIO.setup(TACH, GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
t = time.time() | |
def fell(n): | |
global t | |
dt = time.time() - t | |
if dt < 0.01: return # reject spuriously short pulses | |
freq = 1 / dt | |
rpm = (freq / 2) * 60 | |
print "%.f" % (rpm,) | |
t = time.time() | |
GPIO.add_event_detect(TACH, GPIO.FALLING, fell) | |
while True: time.sleep(1e9) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If the 'overhead' for each measure is consistent, then duration between measurements is consistent. So you will always be at the mercy of the total time to calculate the measure or the duration of overhead. If you forgo the calculation, and report just the raw frequency that is not as informational, but would be more accurate. Total time for the routine to run, will always be a given overhead, regardless of the complexity. That said, if you find that a given implementation of python does 'some' calculation faster, thus reducing the total time in fell() function that would be the most reasonable approach. For example python 3.9 is slow then python 3.10 for example, for the sake of argument. For even the pcode resulting is faster than some other, sure.
Changing the code, to avoid the print in the fell() routine is likely the best approach, IMHO, say write to global and have a completely different process read the global and report it, say only every 5 or 10 seconds. The frequency of printing, can be an issue, is really an issue, because print is slow. On a device like the Pi Pico that has two cores... have 1 core measure and 1 core report? Using say C or C#. That might be the most accurate. That is a bit tricky with python, because you can't force the issue or core assignment.
Here is another interesting issue related to the overhead of reporting/calculating, BIOS/ACPI on PC only in a very gross sense reports FAN RPM, I have seen the variance really great, for example, when I know a given fan at a given voltage/current should be X to have the BIOS report + or - X by a factor of 100s to 1000s, so is the actual RPM that critical? Or that the RPM proves the Fan is working (when compared to temperature delta achieved?). This seems odd, given any PC even 5 or more years old is going to have multiple cores, but the BIOS code monitoring the cores often only runs on CPU 0. Has you scratching your head on that one! :)