简体   繁体   English

如何检测GPIO输入树莓派的变化

[英]How to detect change in GPIO input raspberry pi

Is there a way to detect to detect change in raspberry pi GPIO without using a infinite loop? 有没有办法检测,以检测覆盆子pi GPIO的变化,而不使用无限循环?

You can detect rise or fall by using this: 您可以使用以下方法检测上升或下降:

GPIO.add_event_detect(channel, GPIO.RISING, callback=my_callback)

But you only can set event detector for either falling or rising at a time. 但您只能将事件检测器设置为一次下降或上升。 Is there any way to do it without checking the input in a infinite loop? 如果没有在无限循环中检查输入,有没有办法做到这一点?

You can use threaded callbacks on event_detect . 您可以在event_detect上使用线程回调。 As per raspberry-gpio-python , you can make use of something like this. 根据raspberry-gpio-python ,你可以使用这样的东西。

GPIO.add_event_detect(channel, GPIO.RISING, callback=my_callback)

Where event could be GPIO.RISING , GPIO.FALLING or GPIO.BOTH , my_callback is a normal python function which behaves like an ISR which is run in a different thread. 事件可能是GPIO.RISINGGPIO.FALLINGGPIO.BOTHmy_callback是一个普通的python函数,其行为类似于在不同线程中运行的ISR。

Hope it helps. 希望能帮助到你。

这个链接可能会有所帮助raspberry-gpio-python基本上只是使用回调来做你想要的上升或下降而不是轮询(你所描述的)

If you get a simple MCP3004 or MCP3008 IC which is a Analog to Digital converter you can do much more with inputs. 如果你得到一个简单的MCP3004或MCP3008 IC,它是一个模数转换器,你可以用输入做更多的事情。 Here is some sample code to get you started. 这里有一些示例代码可以帮助您入门。 More info on ADC's here and how to hook them up to your pi 关于ADC的更多信息以及如何将它们连接到你的pi

import spidev

#this fucntion can be used to find out the ADC value on ADC 0
def readadc_0(adcnum_0):
    if adcnum_0 > 7 or adcnum_0 < 0:
        return -1
    r_0 = spi_0.xfer2([1, 8 + adcnum_0 << 4, 0])
    adcout_0 = ((r_0[1] & 3) << 8) + r_0[2]
    return adcout_0

reading= readadc_0(0))

depending on the resolution of your ADC you will have to do some calculations to get the reading into terms of voltage 根据ADC的分辨率,您必须进行一些计算才能读取电压

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM