3

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

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?

Atrotors
  • 765
  • 2
  • 7
  • 25

3 Answers3

5

You can use threaded callbacks on event_detect. As per raspberry-gpio-python, you can make use of something like this.

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.

Hope it helps.

dhruvvyas90
  • 1,135
  • 1
  • 15
  • 31
1

This link might be helpful raspberry-gpio-python Basically just use callbacks to do whatever you want at the rising or falling edge instead of polling (what you described)

Mahdi Chamseddine
  • 387
  • 1
  • 6
  • 20
0

If you get a simple MCP3004 or MCP3008 IC which is a Analog to Digital converter you can do much more with inputs. Here is some sample code to get you started. More info on ADC's here and how to hook them up 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 have to do some calculations to get the reading into terms of voltage

Mahallon
  • 60
  • 9