Raspberry Pi - RPM/Tacho meter

Counting rising edges

 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
45
46
47
48
49
50
51
52
53
#!/usr/bin/python3

import RPi.GPIO as GPIO
import time

sensor = 21

GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor, GPIO.IN)

sample = 100
count = 0

start = 0
end = 0

def set_start():
    global start
    start = time.time()


def set_end():
    global end
    end = time.time()


def get_rpm(self):
    global count

    if not count:
            set_start()
            count = count + 1

    else:
            count = count + 1

            if count == sample:
                set_end()
                delta = end - start
                delta = delta / 60
                rpm = (sample / delta)
                print (round(rpm,0))
                count = 0
                return 

GPIO.add_event_detect(sensor,GPIO.RISING, callback=get_rpm)

try:
        while True:
            time.sleep(0.1)
except KeyboardInterrupt:
            print ("   Quit")
            GPIO.cleanup()

blogroll

social