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
CartPrices 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.
- Place the top of the LDR inside one end of the drinking straw to shield it from ambient light.
- On the breadboard, connect one leg of the LDR to a 3.3V pin on your Raspberry Pi.
- Connect the other leg of the LDR in series with the 1µF capacitor.
- Connect the capacitor's positive leg to GPIO 4 on your Raspberry Pi.
- 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.
- Create a Python test script on your Raspberry Pi.
- Run the script to see the light value (between 0 and 1).
- Shine the laser pointer into the straw and note the value increase.
- If the ambient light value is too high, wrap the straw in electrical tape to improve shielding.
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.
- Create a new Python script that imports the LightSensor class and pygame.
- Initialize pygame for sound playback.
- Create a sound object pointing to your alarm sound file (.wav format).
- Use the when_dark method to trigger the alarm when the beam is broken.
- Save the script.
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.
- Connect the components directly to your Raspberry Pi using jumper leads (tape the connections for strain relief).
- Optionally, place the sensor assembly in a small box with a hole for the straw.
- Position the sensor on one side of a doorway.
- Mount the laser pointer on the opposite side so the beam enters the straw and hits the LDR.
- 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
- Remote Door OpenerBuild another security project with Arduino and an electromagnetic lock.
- More Raspberry Pi projectsExplore beginner and intermediate Raspberry Pi builds.
Stuck? Contact Vilros Support with your board model and what you see on screen.