Skip to content

Instantly share code, notes, and snippets.

@btwelch
Created April 29, 2021 22:31
Show Gist options
  • Save btwelch/d695e56457734e3c3b5de2eed73695a0 to your computer and use it in GitHub Desktop.
Save btwelch/d695e56457734e3c3b5de2eed73695a0 to your computer and use it in GitHub Desktop.
Hall Effect Tachometer - Rasberry Pi
import time, datetime, sys
import RPi.GPIO as GPIO
sense_pin = 7
LED_pin = 36
GPIO.setmode(GPIO.BOARD)
GPIO.setup(sense_pin, GPIO.IN)
GPIO.setmode(LED_pin, GPIO.OUT)
last_time = time.time()
this_time = time.time()
RPM = 0
def EventsPerTime(channel):
global RPM, this_time, last_time
GPIO.output(LED_pin, True) # LED on
this_time = time.time() # Get current real time
RPM = (1/(this_time - last_time)) * 60 # Revs/min real time diff
print('Current RPM = ', '{:7.1f}'.format(RPM)) # Instantaneous RPMs, not average
last_time = this_time # Store current time for next measurement; Requires multiple, contigious interrupts
GPIO.output(LED_pin, False) # LED off (flash when reading)
return()
# Interrupt: on input change, run EventsPerTime function
GPIO.add_event_detect(sense_pin, GPIO.RISING, callback=EventsPerTime, bouncetime=1)
# MAIN ROUTINE
timedate = datetime.datetime.now().strftime('%H:%M %Y%m%d %a')
print('System active @ ', timedate)
print('Ctrl-c to quit')
try:
for x in range(0, 1200): # Loop x times
time.sleep(0.5) # Time delay before looping
except:
time.sleep(2) # Allow settling before quitting
GPIO.output(LED_pin, False)
GPIO.remove_event_detect(sense_pin)
GPIO.cleanup()
print('Done')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment