How to detect changes in the raspberry pi GPIO input

Is there a way to detect change detection in a raspberry pi GPIO without using an infinite loop?

You can detect a rise or fall using this:

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

      

But you can set an event detector for falling or rising only. Is there a way to do this without validating the input in an infinite loop?

+3


source to share


3 answers


You can use streaming callbacks on event_detect

. By raspberry-gpio-python, you can use something like this.

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



If the event could be GPIO.RISING

, GPIO.FALLING

or GPIO.BOTH

, my_callback

is a regular python function that behaves like an ISR that is fired on a different thread.

Hope this helps.

+4


source


This link might be helpful raspberry-gpio-python Basically just use callbacks to do whatever you want on a rising or falling edge , not polling (which you described)



+1


source


If you get a simple MCP3004 or MCP3008 IC that is an analog to digital converter, you can do a lot more with the inputs. Here's some sample code to get you started. Read more about ADCs here and how to connect them to your 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 need to do some calculations to get the reading in terms of voltage

0


source







All Articles