This Cyber Monday Envato Tuts+ courses will be reduced to just $3. Don't miss out.
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
- Quarter sized breadboard
- Arduino (this tutorial uses Nano, but most models should work)
- 5V General purpose USB charger
- USB to 2.1mm barrel connector cable
- Breadboard-friendly 2.1mm DC barrel jack
- Breadboard friendly switch
- Laser chess game mirror
- 10K Ohm photoresistor
- 10K Ohm resistor (Brown-Red-Orange) (A good set of resistors is useful for your kit)
- Laser! (The exclamation point is necessary when talking about lasers)
- Enclosed piezo element
- Breadboard hookup wire kit (only a few needed for this tutorial but the set is good for an electronics kit)
- Laser mounting stand
- Soldering Iron
- 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).
- 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
- 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
- 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.
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.
Follow the diagram below to assemble the breadboard. Here are a few things to note about the circuit.
- The main power buses on the breadboard are regulated. Unregulated power from the power adapter goes into the power regulator on the Arduino
- 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
- 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
- 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
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.
- Attach the Arduino to the computer with the USB cable
- Set the board type and serial port from the Tools menu
- Download the source code from the package attached to this tutorial or the github repository
- Open the source code in the Arduino IDE
- Ensure that you have both the
pitches.hfiles loaded in the sketch
- Verify and upload the sketch
- Unplug the programming cable
- 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).
- Disarm the alarm by turning the switch, on the breadboard, off
- Put the tripwire device on one side of the secure doorway
- Plug the power supply into a nearby outlet and connect it to the breadboard
- Aim the laser across the doorway to a location where you can set the mirror
- 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.
- Arm the tripwire by flipping the switch to the on position
- 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.
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.