# How can I get my hardware to generate interrupts?

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...

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.

``````#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import subprocess

GPIO.setmode(GPIO.BOARD)

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"

time.sleep(.5)
``````

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)