Placeholder Intruder Alert!  MacGyver a Laser Tripwire with Raspberry Pi, a Laser –

Intruder Alert!  MacGyver a Laser Tripwire with Raspberry Pi, a Laser Pointer, and a Straw

Intruder Alert!  MacGyver a Laser Tripwire with Raspberry Pi, a Laser Pointer, and a Straw

You can use an invisible beam to sound an alarm whenever it’s broken by someone crossing its path.  In this project, inspired by Marc Scott of the Raspberry Pi Foundation, we will create a laser tripwire using Raspberry Pi and a few simple components.

Here’s what you’ll need:

  • Raspberry Pi
  • A breadboard
  • A 1µF capacitor
  • A light-dependent resistor (LDR)
  • 3 female-male jumper leads (for testing)
  • 3 female-female jumper leads
  • A laser pointer
  • A drinking straw

Building your Light Sensor

First, you’ll want to use the breadboard to make a prototype to test whether the LDR is picking up a signal from the laser pointer.  To ensure that other light sources won’t activate it, you can shield the LDR using a piece of the drinking straw (or even a rolled-up piece of paper will do the trick).  Simply place the top of the resistor inside one end of the straw.

Next, set up the circuit by connecting one leg of the LDR to one of the Raspberry Pi’s 3.3V pins through the breadboard, and the other in series with the 1µF capacitor, whose positive leg will in turn be connected to any of the Raspberry Pi’s standard GPIO pins and its negative leg to a ground (GND) pin.  It should look something like this:

You can use the following simple Python script for the Raspberry Pi to check the level of light hitting your light sensor (in this example code, it’s assumed that your LDR is wired to GPIO 4 as in the example image above.):

from gpiozero import LightSensor

ldr = LightSensor(4)


It will return the amount of light being registered by the LDR as a floating point value between 0 and 1.  If the value is already too high, it will be more difficult for the sensor to distinguish between the ambient light in the room and the beam of the laser pointer, so use this to check whether your light shield made from the section of straw is working well enough...if not, try wrapping the straw in electrical tape.

Now you can create a script to detect when the beam of the laser detector is broken by something crossing its path.  It’s similar to the test script above—start by importing the LightSensor class from gpiozero, and then creating an object called ‘ldr’ for the GPIO pin to which you’ve connected your photoresistor—but replace the last line with a method from the gpiozero module that will run when the light source to the LDR is interrupted.  You could use a while True loop with the wait_for_dark method, or just the when_dark method using a lambda function, like this:

ldr.when_dark = lambda: print(“INTRUDER”)

Sounding the Alarm

If you’d like your laser tripwire to set off an alarm instead of just printing “INTRUDER” to your display, there are a couple of different methods you can use.  You can connect a buzzer to your circuit and program it to switch on when the beam is broken, or you can play a sound through speakers using the PyGame module.

Since PyGame comes preinstalled on the Raspberry Pi, this method may be the easiest.  Simply import and initialize the PyGame module, create an object for your sound file and provide it with the path, and then set it to play using the same light detection method from the previous section.  Your code might look something like this:

from gpiozero import LightSensor

import pygame


ldr = LightSensor(4)

my_sound = pygame.mixer.Sound('path/to/my/soundfile.wav')

ldr.when_dark = lambda:

Setting Up the Laser Tripwire

Now that you’ve tested your circuit, you’re ready to set up your tripwire!  Start by wiring the components directly to your Raspberry Pi as follows:

  1. Put one of the LDR’s legs and the capacitor’s long leg into one end of a female-to-female jumper lead, securing them with electrical tape.
  2. Place each component’s remaining leg into its own jumper lead, then connect all three leads back to the Raspberry Pi.

If you’d like, for concealment you can put your light sensing device in a housing, such as a box with a hole for the straw.  Then just place the device to one side of a doorway, and attach the laser pointer to the wall on the opposite side so the beam enters the straw and hits the LDR.  Now run your script and try tripping the alarm by walking across the path of the laser beam to test if everything is set up properly.

Voilà!  You’ve got a home-rigged alarm system!  Watch out though, if you’ve set it up too low your dog or cat may set it off too!  Now see what else you can program your tripwire to trigger when tripped...maybe snapping a photo of the intruder and sending it out in a tweet?

For more ideas of things to try using Raspberry Pi and Arduino boards, check back here on our Vilros Projects blog regularly for more fun electronics projects!