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 are some tutorials about how to do just that : tutorial 1 or tutorial 2. 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 "./input_uvc.so -y /dev/video0" -o "./output_http.so -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, 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 read_pin.py and run

    cd /directory where you put the file/
    chmod 777 read_pin.py 

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

    sudo python3 read_pin.py

    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 send_mail.py

    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 read_pin.py 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/send_mail.py") 

    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 read_pin_with_mail.py 
    sudo python3 read_pin_with_mail.py

    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: Conclustion

    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!

  • <p>Hello,</p><p>Where is the send email attachment to get the mail working?</p><p>Thank you</p>
    I think you can use read_pin_with_mail.py to send the mail. I think I forgot to rename it. I will check it out for sure this weekend.
    <p>Hello,</p><p>In that script i cannot find anywhere to place this information</p><p>Things you will need to change will be</p><ul><br><li>your_ip = &quot;;<li>your_ip_optional_port = &quot;:8080&quot;<li>port_to_camera = &quot;:8081&quot;<li>sending_email_username = &quot;your email username&quot;<li>sending_email_password = &quot;your password&quot;</ul><p>im assuming this line - sm_pid = os.spawnlp(os.P_NOWAIT, &quot;/usr/bin/python3&quot;,&quot;python3&quot;,&quot;/home/pi/Desktop/security/send_mail.py&quot;)</p><p>needs to access the send_mail script to generate the email output and that is where it should be placed? </p><p>Thank you for any help you can give me :)!</p>
    <p>So sorry! I forgot to upload it. It is part of step 10 now. Glad to hear you made it that far without any other notable issues. Where there any other areas that where unclear? Thanks!</p>
    Currently no issues ! I'll test that out in a few hours and see what I get if all goes well I should have the project finished tonight and let you know ! Thanks for the great instructions thus far !
    <p>hi there i have been looking for something similar to adapt to my <br>need. Im looking to develop a system that can be used for motorspot <br>timing. When the car breaks the beam a timer will start, again when it <br>crosses the line the timer will stop. Further down the line i would like<br> to incorporate a database or some kind of automatic results system. Any<br> tips or is this even possible?</p>
    <p>Hi,</p><p>Did you ever get a chance to download all the files i need the send mail python script to complete the project.</p><p>Thank you,</p>
    <p>Thanks for checking out the project! I think you could use this exact system to accomplish your goal. In my design I used a specific photodiode that was optimized for the laser that I was trying to detect. This reduced background noise such as lights turning off and on in the room, and in your case, it would reduce noise produced from the sun passing behind the clouds.</p><p> I don't know how much of this project you have looked at, but if you look at step 8, you could modify the read_pin.py file to log the time to a database (lines 19 through 21). Typical python code is below.</p><p>f = open('file.txt','w')</p><p><br>f.write(hr+':'+min+':'+sec+'\n') </p><p>f.close()</p><p>If the laser tripwire does not work out you could try some of these other sensors.</p><p>-PIR sensor : <a href="https://www.sparkfun.com/products/13285"> https://www.sparkfun.com/products/13285 </a> This will not give you very accurate times, but it is very reliable when it comes to detecting the car. It is designed for wide range motion sensing but you could just place it in a Styrofoam cup to focus the detection beam. You could plug this into the raspberry pi directly instead of building any circuitry.</p><p>-A short length of garden hose across the road with both ends capped and a pressure sensor tapped into the hose. When the car runs over the hose there will be a pressure spike that the pi could detect. This would be much more difficult to set up on the computing side, but would be very accurate on the timing.</p><p>- Induction loop sensor: <a href="https://en.wikipedia.org/wiki/Induction_loop">https://en.wikipedia.org/wiki/Induction_loop </a> This is what you see at large intersections to monitor traffic flow. You will have to amplify the signal before feeding it into the pi. I am not sure if the pi can read analog signal so you may need to introduce a conversion microcontroller to do the signal processing and then output digital to the pi.</p><p>Hopefully there is some helpful information here. Let me know if you have any questions.</p>
    <p>Great IoT project! I'd love to make something like this as long as I can keep the cat from setting it off. :)</p>
    <p>Hello,</p><p>did you ever download the files for this project? i need the send mail portion to complete it</p><p>Thank you,</p>
    <p>Thank you! I was thinking of setting the laser trip wire up next to the cat food as our house has such a low number of actual intruders. </p>
    <p>I would love to make something simular, but i dont understand programing part.</p><p>Oh would it be possible to put normal reflectoron other end so all would be on one side </p>
    <p>You could always just build the laser detection circuit and put a buzzer on the led signal output. That is covered in the first few steps. The programming part is mainly just to send the email and is a basis for incorporating this system into another program. </p><p>My first design was using a mirror to reflect the laser back to the sensor like you suggested. However, adjusting the reflection mirror is very tedious. A very small angle change in the mirror will have a massive impact on the reflection point. This problem will be amplified as the distance between laser source and mirror is increased. You may also find that as dust gathers on the mirror it will slowly degrade the laser strength until the tripwire is randomly triggered.</p>

    About This Instructable




    More by EliBuilds:Raspberry Pi Laser Security System 
    Add instructable to: