Skip to content

Instantly share code, notes, and snippets.

@alphachris alphachris/frequency.py
Last active Apr 10, 2018

Embed
What would you like to do?
alternate Python script for reading a frequency from GPIO Pin (Giessomat)
#required libraries
import RPi.GPIO as GPIO
import time
#GPIO mode (BOARD / BCM)
GPIO.setmode(GPIO.BCM)
#Define GPIO Pin to listen
GPIO_SENSOR_1 = 20
#Define direction of the GPIO Pin
GPIO.setup(GPIO_SENSOR_1, GPIO.IN)
#Samplerate
SAMPLE_RATE = 20
#Refresh-rate in ms
REFRESH_RATE = 1000.0
def frq_read():
start = time.time()
for impuls_count in range(SAMPLE_RATE):
GPIO.wait_for_edge(GPIO_SENSOR_1, GPIO.FALLING)
duration = time.time() - start #seconds to run for loop
frequency = SAMPLE_RATE / duration #in Hz
return frequency
if __name__ == '__main__':
try:
while True:
freq = frq_read()
print ("Measured frequency = %.1f Hz" % freq)
time.sleep(REFRESH_RATE / 1000.0)
#Reset on CTRL+C
except KeyboardInterrupt:
print("Interrupt by keyboard")
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.