Hostingheaderbarlogoj
Join InMotion Hosting for $3.49/mo & get a year on Tuts+ FREE (worth $180). Start today.
Advertisement

How to Build a Laser Tripwire With Arduino

by
Gift

Want a free year on Tuts+ (worth $180)? Start an InMotion Hosting plan for $3.49/mo.

Introduction

Every mad scientist's lab, or seven year-old child's room, needs advanced protection against intrusion by rogue agents or siblings. 

This tutorial shows you how to construct a laser tripwire that plays an alarm when the laser is interrupted. It uses a photoresistor to sense the presence of a laser beam and use a standard piezo element to play sound straight from an Arduino without an amplifier.

Gather the Components and Tools

Components

Tools

  • Soldering Iron
  • Solder
  • Third hand tool
  • Computer with Arduino IDE for programming
  • USB cable appropriate for the Arduino (Mini B for the Nano)

Prepare the Laser and Piezo Element

Both the laser and the piezo element come with small wire leads that are not at all breadboard friendly. Because you are building the project on a breadboard you'll need to prepare them. 

To make the wires breadboard friendly, solder them to a short hookup wire that will allow you to attach them to the breadboard. 

Perform the following steps for both leads from both components. Try to match the color of the hookup wire with the color of the lead from the component for consistency (red to red, black to black).

  1. Tin the lead from the component by clasping it in the helping hand tool and melting a bit of solder on the end of the lead
  2. Tin the end of the hookup wire by clasping it in the other clamp of the helping hand tool and melting a bit of solder on it
  3. Solder the lead and the wire together by using the helping hand tool to touch them together and melt the solder. While there are more robust ways to splice wires this will suffice for this project

Repeat the steps for all four leads.

Laser Safety

Lasers are not toys. Ensure that you know how to handle a laser safely by reading the information on laser safety. This will give you precautions on different classes of lasers. 

The laser used in this project is a Class IIIa laser. It can cause damage to an eye given enough time. You have to be pretty negligent to let this one hurt you, but as with any technology, know how to deal with it safely.

Assemble Breadboard

Follow the diagram below to assemble the breadboard.  Here are a few things to note about the circuit.

  1. The main power buses on the breadboard are regulated. Unregulated power from the power adapter goes into the power regulator on the Arduino
  2. The switch on the breadboard will bring digital pin 2 high when it is on. This will allow the sketch code to detect if the tripwire alarm is armed or not. It will not play the sound if the switch is off. This will allow you to stealthily position the tripwire and verify that it is aligned properly before you enable the alarm
  3. The photoresistor is in series with another 10K ohm resistor while analog pin A7 reads the voltage value between the resistors creating a voltage divider circuit. This circuit allows the Arduino to safely and predictably read the analog voltage level that changes when the photoresistor changes resistance. The photoresistor's resistance decreases the more light it receives. The result is that the value read by pin A7 reduces when there is more light. I saw values as low as 14 when I was testing the circuit
  4. The laser is always on when the power is on so be careful and do not look directly into the laser. While it is only a Class IIIa laser it can cause damage to an eye given enough time. Check out the laser safety page to read up on precautions for different classes of lasers. You have to be pretty negligent to let this one hurt you, but as with any technology, know how to deal with it safely
Laser Tripwire Circuit

Program the Arduino

Push the code to the Arduino with the following steps. Ensure the switch on the breadboard is set to off so the device does not start bombarding you with alerts as soon as the code is uploaded.

  1. Attach the Arduino to the computer with the USB cable
  2. Set the board type and serial port from the Tools menu
  3. Download the source code from the package attached to this tutorial or the github repository
  4. Open the source code in the Arduino IDE
  5. Ensure that you have both the lasertripwire.ino and pitches.h files loaded in the sketch
  6. Verify and upload the sketch
  7. Unplug the programming cable
  8. Plug in the power supply and attach the cable to power up the device

Test the Tripwire and Finish Assembly

Test that the tripwire circuit is behaving as it should by pointing the laser at the top of the photo-resistor. The built in LED on pin 13 of the Arduino should light when the laser is centered on the photo-resistor. The sketch lights this LED when the resistance value drops below the defined threshold.  This will allow you to align the laser and mirror before you arm the device. 

Keeping the laser aimed at the photo-resistor, flip the switch to arm the device and use a finger to break the laser beam (this sounds more exciting than it is). You should be rewarded with the alarm melody. The melody for this project should be recognizable to most of mankind as an ominous alert that something nefarious is occurring.

The melody is played on the piezo element by using the tone function of the Arduino. The tone function generates a square sound wave at a given frequency on the specified pin for the indicated duration. Using this method, it is simple to setup an array of pitches and durations to play as the alarm. The pitches.h file helps make this even simpler by setting up defines for frequencies that correspond to notes on a musical staff.

Once you’re happy that the circuit is operating properly power off the device by unplugging the power cable. Assemble the laser in the mounting bracket and tighten the screws. This particular bracket will hold the laser level when freestanding on most horizontal surfaces.  

Bend the leads of the photo-resistor so that the top of it points over the side of the breadboard, horizontal with the ground. This will allow you to setup the laser and mirror to reflect the light onto the top of the photoresistor horizontally across a doorway.

Setup the Tripwire

Now that the device is assembled and tested install the tripwire across the doorway to your top secret laboratory (please pronounce this lah-BOR-uh-tory for the purposes of this tutorial).

  1. Disarm the alarm by turning the switch, on the breadboard, off
  2. Put the tripwire device on one side of the secure doorway
  3. Plug the power supply into a nearby outlet and connect it to the breadboard
  4. Aim the laser across the doorway to a location where you can set the mirror
  5. Set the mirror on the opposite side of the doorway from the laser and carefully target the laser beam back onto the photo-resistor. Some trial, error, and adjustment may be needed to get the alignment just right.  ou will be able to tell that the alignment is right when the indicator LED on the Arduino lights up. If you feel so inclined, you can permanently attach the mirror, laser, and breadboard to their surfaces with screws or double sided tape.
  6. Arm the tripwire by flipping the switch to the on position
  7. Relax in your lair secure in the knowledge that you will be forewarned if anyone attempts to enter

Using the mirror to reflect the laser allows you to keep all the wires and power connectors on one side of the door. It is possible to put the laser and sensor on either side of the door, but this requires powering both. This solution solves for that issue and keeps the project simpler.

Summary

You have now assembled and armed your laser tripwire. In the process you have learned how to use a photo-resistor to sense the presence and absence of a laser beam. You have also learned about a really simple and easy way to play sounds from your Arduino using a piezo element. 

There are many great ways to build on this project and make it even more cool while learning about electronics. Try changing the melody that plays when triggered or building a case for the device that allows you to mount it to the wall. 

I'm going to try adding remote control to this project by using a remote toggle switch and key fob. After all, you don't want to be caught in your own trap.

Advertisement