带Beaglebone Black和Python的光-频转换器TSL235R

2024-10-02 22:28:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用TSL235R(https://www.sparkfun.com/products/9768)和Beaglebone Black(BBB)。不幸的是,我找不到一个教程如何使用BBB和python传感器,并编写了我自己的脚本,但要么接线不正确,要么脚本有错误-当我改变光强度时,输出数据不会改变。我需要得到每秒的辐照度值(平均值为100)。这是我的剧本:

import Adafruit_BBIO.GPIO as GPIO
import time

GPIO.setup("P8_11", GPIO.IN)

# area of TSL235R in cm2
area = 0.0092 

while True:     
    for n in 100:
        f = []
        start = time.time()
        for i in range(100):
            GPIO.input("P8_11")
        duration = time.time() - start                                                                                       
        frequency = 100 / duration                                                                                                       
        irradiance = frequency / area
        f.append(irradiance)
    avg = sum(f)/len(f)
    print " irradiance is {}".format(avg)
    time.sleep(1)

接线:

^{pr2}$

如有任何意见和建议,我们将不胜感激!在


Tags: inimport脚本forgpiotimeareastart
1条回答
网友
1楼 · 发布于 2024-10-02 22:28:01

你好像根本没有测量频率

start = time.time()
for i in range(100):
    GPIO.input("P8_11") # reads the input state, but discards the result
duration = time.time() - start                                                                                       
frequency = 100 / duration                                                                                                       
irradiance = frequency / area

也许你是想做点什么(未经测试):

^{pr2}$

注意:BBB可能太慢,无法测量设备输出的更高频率,因此您可能会在某些光照水平下得到错误的读数。它是否适合您的应用将取决于输入读取的速度以及到达传感器的最大光照强度。你可以考虑减少进入传感器的总光线(比如说通过针孔)使其回到可用范围。在

编辑

另外,您是否将1-GND和2-VDD引脚连接到BBB上的接地和正极,或者它们也使用GPIO线路?在

编辑2

下面是一个测量多个脉冲以提高精度的例子

num_pulses = 10 # make this longer for more accuracy
state = GPIO.input("P8_11") # read current input state
while GPIO.input("P8_11") == state:
    pass # wait for input to switch
start = time.time()
for i in range(num_pulses):
    while GPIO.input("P8_11") != state:
        pass # wait for input to switch again
    while GPIO.input("P8_11") == state:
        pass # wait for input to switch again
duration = time.time() - start # this is the duration of num_pulses pulses
period = duration / num_pulses

相关问题 更多 >