Introduction: Raspberry Pi Laser Security System

Thanks for checking out my instructable. By the end of this instructable your will construct the raspberry pi laser tripwire system with email alert functionality that is shown in the video.

To complete this instructable your will need to be familiar with general circuitry and soldering, know how to use a breadboard, and be comfortable using the terminal on the pi. Having experience in python will be helpful as well.

This project consists of three main systems. The raspberry pi, the laser tripwire circuit, and an observing computer. Our goal is to use the raspberry pi to detect a voltage change on the laser tripwire circuit, and then alert an observing computer in the form of an email and video stream.

Let's begin.

Step 1: Round Up Required Components

To complete this project you will need the following items.

  1. At least one operational Raspberry Pi. In this build I used a model B style Pi running raspbian wheezy. I am sure this tutorial will work just fine if you are running raspbian jessie.
  2. An external power source for the raspberry pi. We will be drawing a significant amount of amperage once all this is hooked up. I spent a lot of time diagnosing a crappy power supply as my source of trouble during my first build. Make sure you have a beefy power able to source at least 2 amps.
  3. A cheapo laser pointer. I found mine next to the cash register at a gas station. If you want the exact laser you can find it here. The label says 630-680 nm for the wavelength. Whichever laser you get will determine some of the other components in the build. So make sure that its label identifies its wavelength.
  4. A usb style webcam. This webcam has an adjustable foot that lets you position the camera nicely. I found that the usb cable that came with the webcam was too short for my application so I found an extension on monoprice.

  5. Some sort of breakout kit for the raspberry pi so you can have easy access to the pins on the processor.

  6. A breadboard.
  7. A soldering iron.
  8. Jumper wires for the breadboard.
  9. Circuit Components. Multiply by the amount of trip wires you want.

I will attach the kicad files for the circuit board but will not go over the manufacturing of the board in this instructable.

Throughout the course of this project I found the following optional tools to be handy:

  1. Installing synergy on the raspberry pi. In case you are not familiar, this software will allow you to control the raspberry pi's mouse and keyboard with another computer. This is nice if you are like me and are more comfortable on a different computer. Also this is software that everybody should have.
  2. A usb hub. For every webcam you wish to use you will need one port.
  3. Voltage meter or oscilloscope if you have it.
  4. Using MobaXterm to control the raspberry pi and manipulate files through ssh. It is a must have for anyone raised on windows. See this tutorial to get set up to use it.

Step 2: Get the Pi Up and Running.

  • The pi with minimal components attached

  • Before we can shoot any lasers, there are several layers of software that need to be constructed. To have the best chance of success it is good practice to start with a clean slate. To do this disconnect any unneeded items from the Pi. This will decrease the chances of having interference from a connected device.

    Next, make sure your Raspberry Pi is up and running with the latest software by entering the following in the terminal

    sudo apt-get update

    With the raspberry pi up to date it is time for the next step

    Step 3: Create the Laser Detection Circuit.

    Place the components on the breadboard as shown. Power the breadboard using the 5v output of the pi or a benchtop power supply. In figure 1 the 5v supply is connected to the positive breadboard rail in the lower left of the image, and the ground to the upper left. This circuit uses the photodiode as as switch to trigger the transistor. The transistor converts the small voltage change in the photodiode to a somewhat digital signal that the pi can read. For all subsequent steps, the point between the 100ohm and signal led will be where we sample the circuit with the pi.

  • Fig 1 :Circuit breadboard layout top

  • Fig 2 :Circuit breadboard layout alternate view

  • If you want a cleaner look I attached the kicad files for the board that is shown below.

  • Fig 3 :Sensor board top view

  • Fig 4 :Sensor board ISO

  • Step 4: Modify the Laser for External Power.

    In the following steps you will build the laser pointer assembly.

    - Disassemble the laser pointer. When doing so, note the orientation of the battery terminals with respect to the laser emitter.

    - Next, desolder the battery connection spring and momentary push button from the laser.

    - Solder a piece of wire between the terminals where the button was. Now when power is supplied to the laser it will turn on automatically.

    - Now solder the positive and negative leads to the laser. If you are using the same laser from the parts list you can use the picture below to see the solder points. If you are not, you may need to find the solder points yourself. You can use the battery orientation as a hint for the positive or negative lead. To find the last solder point you can use a 5v power supply and probe the circuit with two test leads. When you have created the proper circuit, you have found the solder point, and the laser will light up.

  • The orange wire is replacing the momentary push button

  • Showing the positive and negative leads

  • - The final step is to make a directional mounting post for the laser so it can be easily adjusted to aim at the laser receptacle. I have found that most lego bionicle kits are an excellent source for cheap ball in socket joints. Glue one piece of the ball joint to the laser pointer using super glue. Now you can mount the socket joint to any surface and snap the laser in.

  • Ball joint

  • Socket joint

  • Ball and socket assembled

  • Step 5: Power Up the Laser and Test the Detection Circuit

    Plug the laser into your breadboard. The positive will need to be connected to 5v and ground the negative wire. If the laser turns on great, if not,double check you have the proper voltage with a multimeter. If it still doesn't work try swapping the leads in case you hooked it up backwards. If it still doesn't work you may not have soldered to the right pads, go back to the last step.

  • Working laser with a healthy beam

  • Once you have the laser beaming it is time to test the detection circuit. Aim the laser at the photodiode. The LED should turn off signaling that the beam is active. Wave you hand through the beam and the LED should turn on signaling a trip.

  • Beam is not broken and the led is off

  • Intruder breaks the beam causing the led to light up

  • Step 6: Connect Your Webcam and Test Its Functionality

    Connect your webcam to the usb port. In order to stream the video we need to get the utility mjpg-streamer. Here is a great tutorial about how to do just that. Once you have mjpg-streamer installed properly. Start streaming video by entering the following command into the terminal.

    cd /where you installed mjpg-streamer/mjpg-streamer
    ./mjpg_streamer -i "./ -y /dev/video0" -o "./ -w ./www -p 8081"

    This command will set up a stream on port 8081 using the video0 source. If you want multiple webcams enter the above command again but change video0 to video1 and 8081 to 8082. I have streamed up to 3 webcams this way and the pi was not being overloaded.

    Test that the connection is working by opening a browser on another computer and enter ip-address-of-pi:8081 or on the pi you can just enter localhost:8081. If you do not know your ip of the pi enter the following in the terminal


    If all is working you should see the mpg-streamer homepage. Click on the stream tab to see your webcam stream. Below is a screenshot of the interface.

  • mjpg-streamer sample output

  • In doing this tutorial for the second time I was able to access the mjpg-streamer home page but did not see a stream. To fix this I read this post (link now broken), specifically the part about YUYV pixel format, if you encounter a blank stream you should consult it as well.

    Step 7: Intermission

    Okay, so at this point we should have our raspberry pi up and running. Our laser has been modified for external power. Our detection circuit has be constructed and you have confirmed that when the laser beam hits the photodiode the led turns off and when you break the beam the led should turn on. Lastly you should have your webcam connected and functionally streaming.

    Now it is time to put it all together. Let's start out simple by trying to detect the laser beam tripping with software.

    Hook up the detection circuit to the breadboard as shown. Make sure you connect the sensor signal wire to pin 25 in preparation for the next step. The pull down resistor is not optional. I had a 10k laying around, but any resistor should do.

  • Breadboard layout with PiCobler

  • Step 8: Monitor the Laser With Software.

    Let's write a short program to listen to the laser signal and post that output to the terminal. You can download the code as an attachment.

    You will probably need to install the dependencies for this script. Python and the gpio module. To install these enter

    sudo apt-get install python-rpi.gpio python3-rpi.gpio

    Once you have a successful install of python, download the attached python script called and run

    cd /directory where you put the file/chmod 777 

    The chmod command will give the script permission to run. Then test that everything is fine and dandy by running

    sudo python3

    If all goes well you should see a console output as show below. To exit the python script enter


  • Correct Console Output (shown using MobaXterm)

  • Wave your hand in front of the laser beam and you should see some output on the on the console alerting you that the beam has been broken. Feel free to play around with the script to get your system to perform in different ways.

    Step 9: Create a Webpage to Display Your Stream.

    Now we need to set up a webcam viewing interface. For this step you will need to install apache web server. There are plenty of tutorials out there on setting one up. But here is the short list with minimal configuration.

    sudo apt-get install apache2

    once installed you should be able to type in your browser on the raspberry pi


    You should see the welcome page from apache. If this is your first time setting up a web server you will probably want to configure your pi to have a static ip address so that your router will always assign the proper ip if you do a power cycle. If you want to view your security system from another location you will have to setup port forwarding on your router. It has been a while but I also think you will need to forward any ports that have a webcam on them as well. There are plenty of tutorials out there that cover these procedures. Be aware that anyone can see your webcam if you choose to setup port forwarding on your router.

    The attached files are sample web pages that you can use to setup your website. You will need to change the file extension on the html file as they wouldn't let me upload it. Next place the files in your www folder called out in your apache config file. The default location is

    cd /var/www

    If you would like to change the location where the web server looks for your html files you can edit your sites enabled file by entering

    sudo nano /etc/apache2/sites-enabled/000-default

    Change any instance of /var/www/ to the location you desire. I put mine in /home/pi/Desktop/www/

    Once you have the html files in the web folder enter localhost in the browser or the ip address of the pi from another computer. You should see something like this.

  • Screenshot of the attached files in operation


    Step 10: Set Up Email Alerts

    Good things are happening! Lets press our luck by attempting to send an email alert from the pi to a predefined email address. The attached python script will build your email that you wish to send, connect to an email provider such as gmail, and send the email using the email provider. This is achieved using the smtp package of python. I suggest you make a dummy email address just for you security system as we will have to reduce the security on your gmail account in order for the smtp package to work.

    You will have to modify the script in several places in order to get a successful transmission. It is heavily commented and will produce a lot of outputs on the terminal for your convenience.

    Things you will need to change will be

    • your_ip = ""

    • your_ip_optional_port = ":8080"

    • port_to_camera = ":8081"

    • sending_email_username = "your email username"

    • sending_email_password = "your password"

    your_ip_optional_port will most likely be empty unless your apache server is running on a port other than the default 80. I already had a server running on 80 so I set my pi to run on 8080. The port_to_camera will be the port you specify your camera to run on using mjpeg streamer.

    Once you have changed these variables at the beginning of the script run the program by entering the following in the terminal.

    cd /folder where you put the script/
    sudo python3

    If the stars are aligned properly and all the variables are correct you should get an email similar to the one below.

  • Screenshot of the attached files in operation

  • There are many places where this script can fail. Do not get frustrated if it does not work the first time. Use the debugging points in the script to narrow down the problem areas and then focus on one problem at a time.

    Step 11: Trigger Email Script When Laser Is Tripped.

    Now that the pi is able to send emails, let's automate it so that each time the laser is tripped we get an email. Download the attached file which is a modified version of which contians a new function to lauch the mail script. The major additional line is the following

    sm_pid = os.spawnlp(os.P_NOWAIT, "/usr/bin/python3","python3","/home/pi/Desktop/security/") 

    This line will launch the send mail script in parallel with the laser sensing script. This is desirable because the send mail script takes a few seconds to complete and would block the laser sensing script from running until the email was sent. This is not a problem for a single camera system, but if you had multiple cameras you would want to detect a laser trip on camera 1 even if camera 2 has triggered an email event. The sm_pid variable will contain the pid of the process that is launched by this command. We check for this id if the email trigger is called again if it exists the email is still sending so we ignore the event. If it doesn't exist this is likely a new event and an email is sent.

    Test all is working by running

    cd /directory where you put the file/
    chmod 777 
    sudo python3

    When you break the laser beam you should get an email with the snapshot taken from the webcam.

    Step 12: Create a Master Script to Initiate the System.

    At this point the project is mostly done. One last step is to simplify the startup of the system with one last script. It will launch all the subprograms and set up the webcam with one script. The attached file can be launched at startup by editing your /etc/rc.local file. You need to change the file extension and may have to edit the script to include the proper paths if you put your scripts in a different location.

    Step 13: Conclusion

    Well that is it. I hope you enjoyed this instructable! Let me know in the comments if you have any questions or need more detailed information in any areas. Keep tinkering!

  • Circuits Contest 2016

    Participated in the
    Circuits Contest 2016

    IoT Builders Contest

    Participated in the
    IoT Builders Contest