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:
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:
The ADCpy.py 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.