One Million Monkeys

…because 999,999 just isn't enough.

Raspberry Pi Laser Tripwire

So I’ve been playing around with my Raspberry Pi computer, especially with the general purpose input/output pins available for controlling external hardware. Before getting my Raspberry Pi, I had exactly no experience with electronics, but thanks to the various forums at the Raspberry Pi website, and especially the excellent tutorials provided by Adafruit, I am finding my way around both the Raspberry Pi as well as Arduino microcontrollers.

My latest project has involved setting up a laser tripwire that will do something when the laser beam is broken. Ultimately, I plan to wire up a motorized Nerf dart gun to fire a couple of darts when the beam is broken. For now, though, it sounds a buzzer. Here’s how it works:


Laser diode powered by 2 AA batteries. Beam is adjusted to shine on the LDR on the other side of the doorway.


Breadboard layout of LDR connected to Pi with MCP3008 ADC. The buzzer is connected to Pi pin 22.

The basic principle is straightforward. The laser beam, which is powered by two AA batteries, is adjusted so that it shines on a CdS light-dependent resistor (LDR). If the voltage across the LDR falls below some threshold value, determined by experimentation, then some action is initiated, in this case a buzzer is powered on for two seconds.

One small complication is that the Raspberry Pi only has digital IO pins, and reading the voltage on the LDR requires an analog input. The way I have dealt with this is to use an MCP3008 analog-to-digital converter. Adafruit sells these for $3.75, and has a good tutorial on using it with an analog temperature sensor. In this case just remove the temperature sensor, and connect one lead from the LDR to an ADC input pin, and the other LDR lead to the Pi’s 3.3 volt power. I also put a 10K Ohm pull-down resistor on the ADC input pin. I connected my LDR to two of the four wires inside an old telephone cable to give some extra space, and mounted the business end of the LDR inside a hole I drilled in a small block of wood. This helps block out some of the ambient light that might otherwise mask the breaking of the laser beam.

I’m also a big fan of Adafruit’s Pi Cobbler for connecting things on the breadboard to the Pi’s GPIO pins. If you don’t have one of these, do yourself a favor and spend the 8 bucks. For me, it had the added benefit of making me learn how to solder.

At this point the only consequence to breaking the laser beam is a small buzzer that sounds for two seconds. It’s a garden variety piezoelectric buzzer with a positive lead connected to one of the Pi’s digital pins, set as an output, and the ground lead connected to the Pi’s ground. The output pin is set to low, unless the LDR voltage falls below the threshold, at which point the buzzer output pin is set to high for two seconds, and then returned to low. The code is Python, and can be downloaded here:

git clone

The file is used to read the data from the ADC, and is modified from Adafruit’s MCP3008 tutorial code.

Let me know in the comments if you have any problems or questions. Enjoy!

Update: In response to a comment, here’s a couple of photos of the the LDR mounted in a small block of wood. The phone cable has four wires in it. Two are as soldered to the leads on the LDR. At the other end, I soldered a couple of male header pins for plugging into the breadboard.



Single Post Navigation

8 thoughts on “Raspberry Pi Laser Tripwire

  1. Pingback: Raspberry Pi Laser Tripwire #piday #raspberrypi @Raspberry_Pi « adafruit industries blog

  2. Pingback: Raspberry Pi Laser Tripwire #piday #raspberrypi @Raspberry_Pi | 공PCB닷컴

  3. Robert Boerner on said:

    Excellent write-up. Can you show some pictures of the LDR sensor mounted at the other end of the telephone cable?

  4. Hi, Love the idea, and thanks for sharing.
    I am trying to connect a 650nm, 3V (5mW) red dot diode laser. However I know very little about the correct electric setup. Can you help me out. You are using a very similar laser diode, so I was wondering if you can help with how to control the diode from the Pi?

    All help welcome

    • My little setup here doesn’t actually control the laser from the Pi. The laser is connected to two AA batteries and aimed at a CdS light sensitive resistor. This is connected to the Pi through an analog to digital converter chip (MCP3008, I think). The Pi reads the voltage off the resistor, and if it falls below the threshold value determined by trial and error by so done walking in front of it, the Pi turns on a buzzer connect ted to another GPIO pin.

      If you want to program the Pi to control the laser, just connect the black wire from the laser to the Pi’s ground pin, and the red to one of the GPIO pins. Then write your program to set that pin high when you want it to turn the laser on, and to low when you want to turn the laser off.

      I hope this helps. Try the tutorial section on for good lessons on the Pi. I know they have one on using the mcp3008 analog to digital converter with the Pi.

  5. I could use this to make a simple motion detection system 😀
    So in short, these are the required items:
    – laser beam
    – LDR
    – MCP3008
    – breadboard

    Do you mind posting the blueprint/schematic diagram as well?
    I know some programming, but on the other hand my electrical knowledge is very rusty.

    Thank you 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: