I have a IR photo gate sensor that I have attached to my Raspberry Pi, and it is working nicely as I test with WebIOPi and a C program using the wiringPi library.

I would like to use some other languages to access this device while making use of the same kind waitForInterrupt() functionality made available by wiringPi. I haven't found any tutorials that show how to block waiting for an interrupt in Python or (I know this one is a long shot) Erlang.

Does anyone know how to do this, or can you point me at instructions? I suppose I could always call out to C code to do that blocking, but I am hoping I can avoid that.

If it helps or you're curious, I am working on a project like this one: http://www.sparkfun.com/tutorials/131, and I don't think a polling approach will give me the kind of results I want.

  • It would help to know what other language? Some may offer native support and others may only be able to use a system call to the aforementioned C code. Nov 4, 2012 at 14:44
  • I am looking to use either erlang or python.
    – Ben
    Nov 4, 2012 at 19:35

2 Answers 2


In general, you cannot access interrupts directly from a userland program. Those interrupts need to be exposed via some kernel facility to do what you want.

The kernel exports gpio devices as /sys/class/gpio/gpio* devices, and GPIO pins can be exported for userland use. This facility also provides edge triggering for GPIO, and can be configured to trigger an edge either on the rising or falling edge of a signal. This edge triggering is used by poll():ing the opened gpio device. These are documented as part of the kernel. Check for Sysfs Interface for Userspace.

poll() can easily be used from both Python and Erlang, but I am not sure if any of the existing Raspberry Pi libraries wrap it in an easy to use manner. However, this is just a simple programming endeavour, and is not specific to the hardware or any of the GPIO internals.


Had a look at this myself last weekend and found some useful snippets. From my notes

At http://ryniker.ods.org/raspberrypi/MagPi/GPIO_interrupts there's some code which looks plausible but doesn't work out the box. With some tweaking, this works:

# Test interrupts.
import os
import select

# Pin to work with
pin_base = '/sys/class/gpio/gpio17/'

def write_once(path, value):
    with open(path, 'w') as f:

f = open(os.path.join(pin_base, 'value'), 'r')

write_once(os.path.join(pin_base, 'direction'),
write_once(os.path.join(pin_base, 'edge'),

po = select.poll()
po.register(f, select.POLLPRI)

while 1:
    events = po.poll(60000)
    if not events:
        print ('timeout')
        state_last = f.read()
        print 'Val: %s' % state_last

and changing po = select.poll() to select.epoll() also works and is meant to be a bit more efficient. The bit that needed tweak was to change a read(1) to a read() because the input is always 0\n or 1\n.

The quick2wire library seems to take the same tack as does wiringPi. If it's of use to anyone, the notes I made as I went along are at http://www.evernote.com/shard/s65/sh/69dcea85-29b3-487e-8008-2a51159539ed/a5b13779a9742398d35cb52a9fcf3fa5 but I'm very new to electronics so please ignore my blurb about pull up and down resistors. The event driven stuff is at the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.