forked from DriftKingTW/Raspberry-Pi-PWM-Fan-Control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pifan-monitor.py
executable file
·44 lines (33 loc) · 1 KB
/
pifan-monitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/python
# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import time
TACH = 24 # The fan's tachometer output GPIO pin number.
PULSE = 2 # The number of pulses per revolution, most fans seems to use two.
WAIT_TIME = 1 # [s] The 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.002:
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