如何在python脚本中获取方波的频率 [英] How to get the frequency of a square wave in a python script
问题描述
我正在使用TSL235( http://www.ti.com /lit/ds/symlink/tsl235.pdf )光频转换器和Raspberry Pi.传感器的输出是方波(占空比为50%),其频率与光强度成正比.
I'm using the TSL235 (http://www.ti.com/lit/ds/symlink/tsl235.pdf) light-to-frequency converter and the Raspberry Pi. The output of the sensor is a square wave (50% duty cycle) with frequency directly proportional to light intensity.
所以我需要(在python脚本中)知道哪个频率到达Raspberry Pi的输入GPIO-Pin.
So I need to know (in a python script) which frequency gets to the Input GPIO-Pin of the Raspberry Pi.
我只找到了一个教程( http://playground.arduino.cc/Main/TSL235R),其中显示了C代码,但我不了解C ...我只使用python
I only found a tutorial (http://playground.arduino.cc/Main/TSL235R) which shows a C-code, but I do not understand C... I'm only working with python
到目前为止,读取GPIO输入并不困难:
Reading a GPIO Input isn't that hard so far:
#!/usr/bin/python
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.IN)
impuls_count = 0
# Do next lines for i.e. 1000ms:
GPIO.wait_for_edge(25, GPIO.FALLING)
impuls_count = impuls_count + 1
我认为我必须在一个时间间隔内对信号进行计数.但是如何?
I think I have to count the signals in a time intervall. But how?
推荐答案
使用时间模块.它具有对1微秒(1 MHz)敏感的时钟功能.查看您的传感器的数据表,它只能达到500 kHz,这应该足以在高光强度下获得准确的频率测量结果.
Use the time module. It has a clock function that is sensitive to 1 microsecond (1 MHz). Looking at your sensor's datasheet, it only goes up to 500 kHz, that should be sufficient resolution to get accurate frequency measurements at high light intensity.
只需计算一组输入周期的平均频率即可.
Just calculate an average frequency of a set of input cycles.
import time
NUM_CYCLES = 10
start = time.time()
for impulse_count in range(NUM_CYCLES):
GPIO.wait_for_edge(25, GPIO.FALLING)
duration = time.time() - start #seconds to run for loop
frequency = NUM_CYCLES / duration #in Hz
这篇关于如何在python脚本中获取方波的频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!