Skip to content

Laser Tripwire with Raspberry Pi

MacGyver a laser tripwire alarm using Raspberry Pi, a light-dependent resistor, and a laser pointer to detect intruders.

  • Raspberry Pi
  • Beginner
  • 30–45 minutes
  • 4 steps

Shop this project

Cart

Prices from vilros.com. Tax and shipping at checkout.

Before you start, you'll need

  • Raspberry Pi (any model with GPIO)
  • Breadboard
  • 1µF capacitor
  • Light-dependent resistor (LDR)
  • 3 female-to-male jumper leads
  • 3 female-to-female jumper leads
  • Laser pointer
  • Drinking straw

Build the light sensor

About 10 minutes

Create a shielded light sensor using the LDR and straw.

  1. Place the top of the LDR inside one end of the drinking straw to shield it from ambient light.
  2. On the breadboard, connect one leg of the LDR to a 3.3V pin on your Raspberry Pi.
  3. Connect the other leg of the LDR in series with the 1µF capacitor.
  4. Connect the capacitor's positive leg to GPIO 4 on your Raspberry Pi.
  5. Connect the capacitor's negative leg to a GND pin on your Raspberry Pi.

You should see

The LDR is shielded by the straw and the circuit is complete.

Test the light sensor

About 5 minutes

Verify the sensor can detect the laser beam.

  1. Create a Python test script on your Raspberry Pi.
  2. Run the script to see the light value (between 0 and 1).
  3. Shine the laser pointer into the straw and note the value increase.
  4. If the ambient light value is too high, wrap the straw in electrical tape to improve shielding.
python
from gpiozero import LightSensor

ldr = LightSensor(4)
print(ldr.value)

This assumes your LDR is connected to GPIO 4. Adjust if using a different pin.

You should see

The script prints a value between 0 and 1. The value increases significantly when the laser hits the LDR.

Create the alarm script

About 10 minutes

Write a Python script that triggers when the beam is broken.

  1. Create a new Python script that imports the LightSensor class and pygame.
  2. Initialize pygame for sound playback.
  3. Create a sound object pointing to your alarm sound file (.wav format).
  4. Use the when_dark method to trigger the alarm when the beam is broken.
  5. Save the script.
python
from gpiozero import LightSensor
import pygame

pygame.init()

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

ldr.when_dark = lambda: my_sound.play()

# Keep the script running
import signal
signal.pause()

Replace 'path/to/my/soundfile.wav' with the actual path to your alarm sound file. PyGame comes preinstalled on Raspberry Pi.

You should see

The script runs without errors.

Set up the physical tripwire

About 10 minutes

Position the laser and sensor across a doorway.

  1. Connect the components directly to your Raspberry Pi using jumper leads (tape the connections for strain relief).
  2. Optionally, place the sensor assembly in a small box with a hole for the straw.
  3. Position the sensor on one side of a doorway.
  4. Mount the laser pointer on the opposite side so the beam enters the straw and hits the LDR.
  5. Run your alarm script and test by walking through the doorway.

You should see

Walking through the beam triggers the alarm sound.

It didn't work
  • Alarm triggers constantly or never triggers.

    Adjust the positioning so the laser beam enters the straw cleanly. Check that the LDR is properly shielded from ambient light. You may need to adjust the sensitivity in the gpiozero library.

  • No sound plays.

    Verify the sound file path is correct and the file is in .wav format. Check that your Raspberry Pi's audio output is working (test with aplay).

What to do next

Stuck? Contact Vilros Support with your board model and what you see on screen.