Raspberry. Trip Laser Wire
Raspberry. Trip Laser Wire
Step
1 Introduction
In this resource you will use a laser pointer, a few electronic components, and a Raspberry Pi to make a laser
tripwire. Each time a stealthy intruder breaks the laser beam, the Raspberry Pi will sound the alarm via a buzzer or a
speaker.
This resource covers elements from the following strands of the Raspberry Pi Digital Making Curriculum:
Hardware
A 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
Software
Your first step will be to create a simple prototype on a breadboard to detect whether or not a beam of light is
hitting the light-dependent resistor (LDR).
It’s a good idea to shield your LDR to make sure that only light from the laser pointer will trigger your program. You
can do this with a small roll of paper, or even more easily with a section of a drinking straw.
Set up your circuit using a capacitor and an LDR wired to your Raspberry Pi. There’s an optional section below
which explains how RC timing circuits work. The other section details how to use Python to detect light levels.
Now you’re ready to write a script to detect when the laser beam is broken. You can use methods from the
gpiozero module to do this. One of your options is to use a while True loop containing the
wait_for_dark method. However, a better way might be to use the when_dark method. If you choose this
method, you need to either create a function to print 'INTRUDER', or you could use a lambda function.
If you need a little help, then have a look at the hints below.
Here’s an animation showing how to set up the circuit and write your Python program.
Step
4 Making some noise
There are two ways you can get you program to make some noise rather than just print ‘INTRUDER’. The first is to
use a buzzer that is switched on when the laser beam is broken. The second is to use the PyGame module to play a
sound through some speakers.
Have a look at the sections below that detail how to use buzzers and play sounds with PyGame. Choose the method
your prefer, and then have a go at making some noise when the laser beam is broken. If you want to have a go at
playing sounds through the speakers, then this site (http://soundbible.com/royalty-free-sounds-1.html) has
plenty of di"erent ones to choose from. This one in particular (http://soundbible.com/71-Dog-Growling-And-B
arking.html) might be useful.
Once the circuit has been tested, you can wire up the components directly to the Raspberry Pi as shown below.
1. Place one leg of the LDR and the long leg of the capacitor into a female-to-female jumper lead. Then tape it up
to secure the legs.
2. Place the remaining legs into jumper leads, then plug it all back into the Raspberry Pi.
You can place the Raspberry Pi and components in a housing to conceal them if you wish. Here we have used a
plastic box with a hole made in it for the straw:
1. Place your container near a doorway. Then a#x the laser pointer to the wall so the beam is focused down the
straw.
3. If you want to run your code as soon as the Raspberry Pi boots up, then have a look at the instructions below
for automating tasks with Cron.
Step
6 What next?
You could try and include other events that are triggered when the tripwire is broken. How about sending a
tweet (python-sending-a-tweet), or taking a picture (rpi-picamera-take-photo) of the intruder?
You could use a laser component that can be run directly from the Raspberry Pi’s GPIO pins. Here’s an example
of a laser (https://www.rapidonline.com/laserfuchs-70103984-red-class-2-laser-10m-1mw-3-12vdc-25
ma-650nm-wavelength-57-5868) you could buy.