Alternative to Light Dependent Resistor in the laser tripwire project

Hello to all the Raspberry Pi users out there!

Hopefully some of you have completed the laser tripwire project which is featured on the official Raspberry Pi website.
I completed this project and got it working, but the tripwire response time is a little slow for the project I’m working on.

I’m building a sport timer for a dog sport called ‘Flyball’. It would take a while to explain it so if you’re interested it’d be best to either Google it or watch a Flyball race on a YouTube video.

From what I’ve been reading online a photo diode or a photo transistor would have a much faster response time (in the nano seconds) compared to a light-dependent resistor.

My question:- how do I safely connect a photo diode or photo transistor to the Pi without frying it? Can anyone recommend parts to do this?

What resistors (if any) would I need… The LDR has no additional resistors, just a 1uF capacitor connected to it in series. As I am new to electronics, can anyone direct me on where to learn about resistors… understanding the ratings and the colour bands, and HOW to calculate what resistor you would need for a Raspberry Pi circuit (Would I be correct in assuming a GPIO pin is 3.3volts? I don’t mean the constant power 5volt and 3.3volt pins either)

Can anybody (hopefully the staff of Core Electronics) recommend the parts needed?

Final bit of information:- the laser emitter I’m using is a diunotech red light emitter. It has a wavelength of 650nm and its output power is <1mW

Sorry, but I just have to say something about this.
The Raspberry Pi example should use a Analog to Digital converter. Instead they use Python software to charge up a capacitor on the DIGITAL pin. Waiting for the state to change, which can then be related to the resistance of the LDR, which relates to the amount of light. (and they put this in the library)
**** This is the reason the trip response is slow. ****
(btw an Arduino UNO could do this so much easier, ADC is built in)

But that doesn’t help you.

Raspberry Pi GPIO pins are 3.3V.
A simple circuit like that below could be connected to the GPIO pin.
If there is too little light the pin will be at 3.3V. High level.
When light is high enough the pin will be at around zero. Low level.
The current through the diode will be less than 1mA with a 4K7 resistor. Depending on the photodiode used it maybe necessary to change this value. The device from Core Electronics needs more current I think. I would breadboard this to easily change resistors till it was working right.

Python program should be able to read this fast enough for what you want.

Included a couple of links that might help you in understanding electronics.


1 Like

The Raspberry Pi makes this task a little harder than it needs to be without having ADC.

A photodiode/transistor would be worth experimenting with, though to keep things as fast as possible it would be best to experiment with a way to work with digital voltage thresholds for logic high/low (rather than rely on ADC).

Let us know what you end up doing!

Link to a technical discussion of the GPIO pins on the Raspberry Pi.

I note the following :

“The input gate detects whether the input voltage level is low or high by comparing it to a threshold voltage. Normally the voltage threshold is about 1.8V, but it isn’t guaranteed; it can be anywhere between the maximum input low and minimum input high, that is, between about 0.8 and 2.0V.”

This means the time taken to charge the 1uF capacitor through the LDR can vary considerably. The software would continually read the GPIO pin until it detected a high level.

“you can safely sink up to 16 mA each into any number of GPIO pins”
" transient sink currents may make demands on the board’s bypass capacitors"

The GPIO pin driving a low across a charged 1uF may induce more than 16mA current, but for a very short period of time. Probably ok initially, but may not work longer term as the Pi ages.

“Do not drive capacitive loads. Do not place a capacitive load directly across the pin. Limit current into any capacitive load to a maximum transient current of 16 mA. For example, if you use a low pass filter on an output pin, you must provide a series resistance of at least 3.3V/16mA = 200 Ω.”

This condition should be ok. LDR’s typically vary 10k to 100K. But the problem is the capacitor is directly connected to the GPIO pin.

I replied here because the technical person in me was very concerned with the electrical engineering aspects of the Raspberry Pi Laser Tripwire. It is not the right way to treat a digital GPIO pin, although it probably works. I was astonished to see it was included in the GPIO library.

The Raspberry Pi was designed as an education device, it is a fantastic product for little cost. But as an education tool, it should help instill sound engineering practice and software skills. Someone new to electronics can easily build the Laser Tripwire and then, as Matt found out, it does not respond in a timely manner. Addition of an ADC Hat would make it work correctly with a LDR. The disadvantage being the cost of the Hat which can be more than the Pi.
The lack of analog pins on the Raspberry Pi limits what it can teach in this area.


Hi James,

Agreed, education is often met with levels of compromise, skimming over some details for the sake of activating interest and exploration is almost always worth it.

If you are looking for a mS accurate system, then some external hardware will be needed. The closer you can get to using digital logic voltage levels, the better. For example, biasing an NPN transistor to the point where there are no false triggers with the photodoide/transistor would create a clean-cut transition between states.

My innder 8-bit geek would lean to using a microcontroller for a task like this, along with external interrupts. I’d avoid ADC entirely, especially if mS/uS were important. Though that’s just me :slight_smile:

1 Like

Hello to both James and Graham,

Thank you for your replies. There is a lot here for me to digest, but if I stick at it hopefully I can get it all to work out!

Like any NOOB, I’ll probably experiment with the most simple ideas first. The one thing I DO NOT want to do is fry the Pi with excess voltage.

I am intrigued by what James said about the Arduino Uno boards with their built in ADC (by the way, what is the correct way to pronounce Arduino)? I assume ADC means ‘Analogue - Digital - Converter’, would that be correct?

The two main reasons I went with the Raspberry Pi to build my project:-

  1. I wanted to display a GUI on a small screen which displays timing data, along with a virtual control panel. With the use of some graphic software called Paint.NET, and with the use of the tkinter module from Python, I’ve been able to create a nice looking control panel complete with indicators for tripwire state, as well as a graphic representation of the ‘drag tree lights’. I have synchronized this to work with LEDs on the breadboard, so it looks pretty cool.
    I have a 7 inch touchscreen which plugs into a USB power source and connects directly to the Pi via the HDMI port. It is ideal for what I want in the final build… and as an added bonus the touchscreen works with mouse buttons in tkinter!

  2. The Pi has built in wifi and bluetooth. This leads to the possibility of further developing my timing rig to help run a flyball race between 2 teams. If I have the 2 lanes communicating with each other, the lane with the fastest time (and no crossing errors) would be the winner.

I had skimmed through a book about Arduino from the local library and it appears these boards use the C programming language. This brought back nightmares from a misadventure into a university course (it’s a LONG story), so it put me off.
I have found Python to be much more user friendly than C ever was, and after 4 months of doing A LOT of reading, going through online tutorials and writing out MANY different programs, I am finally up to the stage where it is time to learn about electronics.

Hi Matt,

The Pi is perfect for what you want to do and sounds like you have made an excellent system.
Arduino UNO would need many addons to achieve what you have with the Pi.

A tripwire is basically on and off, so works as a digital circuit.
I would forget about the LDR and analog to digital conversion it is only complicating things.

You could use a photodiode to trigger a Mosfet connected to the GPIO pin or connect it like I have shown in a previous post. But you MUST ensure the GPIO pin does not go above 3.3V or below 0V.

This is of interest to me, so I intend to do some testing. I may need this type of circuit in the future. You mentioned a diunotech red light emitter of 650nm, will try that with a photodiode. I’ll let you know the results.


I just checked out the link for the Electronics for Dummies book and it looks like something I really need!

I’ve just been on the libraries inter-library loan website and requested a loan copy. I’ve already been through the ‘Raspberry Pi for Dummies’ book and it was more than useful.
That book had me going off on different tangents; Python programming the video game Minecraft, music-composing of sorts with Sonic-Pi, but it did have some useful content to get started with using the GPIO pins.

1 Like

Hi Matt,

Researching this and considering the parts I can get easily, I found this was not as easy as I thought.

Today, I bought a Diunotech red light emitter, the same as you are using. Other parts came from what I already had in the workshop. The following circuit works nicely. The 100K 20 turn variable resistor allows adjustment for ambient light conditions. The LED in the output indicates when the Mosfet is triggered making adjustment easy. I used an LDR as the sensor because all of the other sensors available were for Infrared, 800nm to 1100nm.

With the desk lamp on (rather bright), the circuit can be adjusted so the LED turns off. Then shining the Laser on the LDR, the LED turns on. The GPIO pin voltage is 3.3V when off and 0.02V when on. Levels the Raspberry Pi will be happy with.

Some comments:
Using the Laser device makes alignment of the beam easy as it is visible light. The laser also has a greater range than the IR devices.
I have ordered the following item from Core Electronics and intend to investigate how it will work. It detects light over the visible spectrum, similar to the LDR but has a response time in microseconds rather than milliseconds. Being a transistor, the Mosfet wont be needed possibly making the circuit simpler.
This was just a basic proof of concept test, yet to set it up as a tripwire and write python code. Will do that after testing the Photo Transistor.
Anyway …


1 Like

Wow! That looks cool James.

I understand most of the diagram, but I’ll see if I can Google what the 2N7000 is myself. I’ve also ordered the same photo transistor you posted a link of, hopefully it will do the job.

I found this website called instructables, and on it there is a project where a Raspberry Pi home security system was made. Part of the project was laser tripwires, and Eli made his own receiver using a photo diode and a separate transistor:-

Eli includes pictures of his receiver both on a breadboard and as a finished ‘chip’. He says it uses 5 volts of power, but I can’t make out how he connects it to a GPIO input pin.

There are similar ‘laser receivers’ available from electronic sellers overseas… and they look a lot like his chip.

Also, this is a link that was shared with me by a forum member on the official Raspberry Pi website. It’s for a Ambient Light sensor using an Op-amp Comparator:-

Like your circuit, it makes use of a LDR. All of this is a lot for a NOOB like myself to take in, but hopefully I will be able to eventually come up with something that will work. Thank you again for all your help James.

Hi Matt,

Received the photo transistor yesterday. Built the circuit shown below and the Raspberry Pi responds nicely, when the beam is broken or turned off.

I used a Pi Zero and Python 3. The 3.3V, GND & GPIO pins came from the Pi 40 pin connector. (GPIO 17 & 27 were used, physical pins 11 & 13). GPIO pin is 3.1V when laser is on and 0.01 when the laser is off. I used a small piece of heatshrink to shield the transistor from ambient light and aimed the laser at this tube. The laser and transistor were about 12cm apart.

I decided to switch the laser on only when needed. The laser draws 10.3mA, IMHO this is too much for a Pi GPIO pin to provide. The GPIOs can safely provide up to 16mA individually and 50mA total but I thought it better to use a transistor in this case; as the laser will be on most of the time.


Notes: The links you provided use a 5V supply, which is fine for the Arduino but not for the Pi GPIOs. The circuits are built such that 5V will not be applied to the GPIO pin but what if components fail, Short or Open circuit. The link I provided to the technical discussion states you should only ever use a 3.3V supply. In my work with the Raspberry Pi I have only used 3.3V for the GPIOs and non have failed at this time. The Pi GPIO pins are not as robust as the Arduino when it comes to real world situations.

Advantages & Disadvantages of LDR. LDR uses more components but allows for adjustment related to ambient light conditions. LDR is slower (40ms, compared to 2us) and might be a problem. Just setting the laser on and reading the sensor took about 140us using the date time in software. Adding code to convert the time to a string and print it took about 4ms.

Hope this has been helpful and you have success with your project.

PS I originally responded to your post because the engineering of connecting a capacitor directly to the GPIO pin is wrong. IMHO it should be connected through a resistor to reduce current below the 16mA max. Having bought the laser diode and some sensors I have come up with another project. Will attempt to build a laser shooting gallery for my grand kids. Laser in a toy gun and they aim at a board with sensors on it which trigger light or sound etc.

1 Like

Hi Jim,

Thank you again for all your help. I’ve received the photo transistors, but I’m waiting on a resistor kit I ordered to arrive. Once I have that I can go about setting up the prototype… and your diagram will be a HUGE help to me.

The project you’re planning for your grandchildren sounds like it will be a lot of fun for them to play with. I hope they appreciate the work you put into it.