sorry if this question is inappropriate, but here goes.

I'm trying to build a small circuit that uses a photoresistor (this one) to detect when a diode puts light on it. The light will be short and potentially happen several times per second.

Usually I would use a capacitor and measure the time it takes for it to charge but that doesn't seem feasible with this setup.

What I would really like is to get the circuit to generate an interrupt but I can't figure out how or if it's at all possible.

I assume I would need some sort of circuit that can let the current flow from the GPIO pin to ground (which as I understand it is what's necessary for interrupts to happen/work). I've looked at op-amps and transistors but I can't figure out what I need and how to wire it.

So, my question is: 1) Is it possible to generate interrupts using a photo cell, and 2) what do I need?

I really need an electronics 101...

2 Answers 2


I've hooked up an LDR to a pi's GPIO and ground pin directly. I set up the internal pull-up resistor on the broadcom chip. This created essentially a voltage divider, that would make the pin would read low if a led shined on it, and high is not.

Since you only need a yes/no answer there is no need to measure the time to charge a cap, just like analog-to-digital converters do. You just need the voltage to be either above or below the tripping point of the GPIO for either state of the led. The tripping point for the gpio seems to be around 1.2v (http://www.youtube.com/watch?v=Wr49ia3oID4). So under normal lighting condition the voltage should be above 1.2v. When the led is shining on the LDR, the voltage should be above 1.2v.

Just note that in my setup I've taped the LDR to the led with black tape, so the LDR is in complete darkness. You probably have to have some external pull-up resister that better matches the resistance of the LDR. Some variable resistor (pot) would be a cheap and handy way to calibrate.

My sample test script for reading the gpio pin.

import RPi.GPIO as GPIO
import time
import subprocess


GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
    state = GPIO.input(26)

    if state == 0 :
        print "LDR: 0"
    else :
        print "LDR: 1"


As for the interrupts. Not sure if that was really the question. You could do something like.

channel = 26
def my_callback(channel):
    print('This is a edge event callback function!')
    print('Edge detected on channel %s'%channel)
    print('This is run in a different thread to your main program')

GPIO.add_event_detect(channel, GPIO.RISING, callback=my_callback)  # add rising edge detection on a channel

More info at http://code.google.com/p/raspberry-gpio-python/wiki/Inputs

Or just ask me later, as I'm currently not near the pi of mine that uses gpio interrupts for triggering.


Assuming the LED is turned on/off and is sufficiently bright it should work, although I would probably use a schmitt trigger to ensure a clean input. If you want to use interrupts this is possible by setting the gpio pins to edge triggered mode. There is a good article in MagPi iss 7 showing how this can be done in Python.


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.