Hi there,
I want to measure a fequency between 1 khz and about 20 khz.
Im using a raspberry pi 2 and an ATTEN function waveform generator.
The goal is to measure the frequency in the period of one second and print its value.
I created a program, that uses threading - it works quite god but there is one big problem.
the value is not stable enough for me. At 100hz I get readings from 99-101, at 1khz I get 950 to 1100.
Signal is: 50/50 duty square, amplitude is 3,3v.
My questions:
Are there any explicit designed GPIOs for this purpose?
Is there a problem with the code and is there a better solution for this?
The Code:
#!/usr/bin/env python
#coding: utf8
import threading
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
i = 0
def printit():
global i
print i
threading.Timer(1.0, printit).start()
i = 0
printit()
# continue with the rest of your code
# Ereignis Prozedur für Eingang HIGH
def doIfHigh(channel):
# Zugriff auf Variable i ermoeglichen
global i
# Wenn Eingang HIGH ist, Ausgabe im Terminal erzeugen
#print str(i)
# Schleifenzaehler erhoehen
i = i + 1
GPIO.add_event_detect(11, GPIO.RISING, callback = doIfHigh) # bouncetime = 1)