Sunday 5 June 2016

How to make a laser tripwire alarm

I have been trying to come up with a project for an upcoming session of my company's branch of CoderDojo. This is a voluntary educational group who host classes to teach children about programming and computer hardware. It's pretty tough to come up with a project that a group of children (aged between 6 - 16) can complete in 2 hours and remain interested. Last session we make an augmented reality game using a webcam and the Scratch programming environment. It was really cool and the kids seemed to have fun. However this time we would like to do a project involving hardware too. Another colleague is investigating the Makey Makey and I am trying to come up with something using micro controllers.

An idea I came up with was to get the kids to make booby-traps to either construct an obstacle course or to create a mission impossible style trapped room to protect an artifact. In theory this shouldn't be too tricky as the various sensors are pretty easy to hook up to an arduino/photon and the code to control them is very simple. The obvious trap to start with (to me anyway) is a laser tripwire. This is among the more complicated of the traps to set up, but the outcome will be sweet.

So I spent yesterday putting together a laser tripwire that raised an alarm when tripped to see how it would come together. It took a couple of hours due to me not having made one before, but I'm pretty confident we could get a kid to complete it in under an hour.


What you will need


3V laser module
piezo buzzer



photo resistor
10kΩ resistor

Wiring circuit

I set the circuit up on a breadboard, but you would probably want to solder some of the components together if you wanted to use the tripwire for real. This is just to show you how it all connects.


Photo resistor

We use a photo resistor to detect whether the laser is currently pointing at it or not. The more light that falls on the resistor, the lower the resistance becomes, so we can use this to detect whether the laser is currently pointing at it or not. When set up inside a voltage divider we can translate the variation in resistance to a variation in voltage. We can then measure that voltage to decide whether to sound the alarm or not. We measure V(out) from the voltage divider with the green wire hooked up to analog pin A1. We hook up the input voltage to digital pin D6 which we will pull HIGH at the start of the program to provide a nice steady 3V.


Buzzer

The piezo buzzer is hooked up to 2 digital pins on the photon. We will control the pins from the photon program when we have detected the laser is not pointing at the photo resistor (i.e. someone has broken the beam). A piezo buzzer makes a click sound when you apply a voltage. The faster you generate clicks (by flipping the input between LOW and HIGH) the higher the frequency of the sound produced. I am using the code from the bitsbox component listing to generate the tone.


The Laser

For simplicity I have used a laser module that includes a driver to keep the voltage steady to produce a regulated beam. This makes the component more expensive, but also much easier to use as you just need to hook it up to a 3V power source and voila, frickin' lasers! I obviously have to mention the fact that you shouldn't point this laser into your eyes or the eyes of anyone else. While the laser beam is not strong enough to disintegrate your enemies, it will damage your eyes if you look directly down the beam. So don't. Hook up the laser to pin D6 to power it up and point it at the photo resistor. 

Make sure the laser is pointing directly at the photo resistor

Photon Code

The code we need for the tripwire is very simple. We need to read the value from the voltage divider, compare it to a value to decide whether we think the laser has been tripped and then sound the buzzer. Here is the code:


 int sounder_A = D2;  
 int sounder_B = D4;  
 int tripwire_power = D6;  
 int tripwire_input = A1;  
   
 void setup() {  
   
   pinMode(sounder_A, OUTPUT);  
   pinMode(sounder_B, OUTPUT);  
     
   pinMode(tripwire_power, OUTPUT);  
   pinMode(tripwire_input, INPUT);  
     
   digitalWrite(tripwire_power, HIGH);  
     
   Serial.begin(9600);  
 }  
   
 void loop() {  
     
   int tripwire = analogRead(tripwire_input);  
   Serial.println(tripwire);  
     
   if(tripwire > 1000) {  
     beep(100, 4000);  
   }  
 }  
   
 void beep(long duration, int freq) {  
     
   duration *= 1000;  //convert the duration to microseconds  
   int period = (1.0 / freq) * 1000000; //get the oscillation period in microseconds  
   long elapsed_time = 0;  
   while (elapsed_time < duration) {  
     digitalWrite(sounder_A, HIGH); //Piezo ports go hi/lo then lo/hi  
     digitalWrite(sounder_B, LOW); //to generate the tone  
     delayMicroseconds(period / 2);  
     digitalWrite(sounder_A, LOW);  
     digitalWrite(sounder_B, HIGH);  
     delayMicroseconds(period / 2);  
     elapsed_time += (period);  
   }  
     digitalWrite(sounder_A, LOW); //kill the output  
     digitalWrite(sounder_B, LOW);  
 }  

Play around with the values to generate whatever sound you want from the buzzer. If you're feeling fancy you can get it to play a tune. Anecdotally I found that the tripwire value read around 150 when the laser was hitting the photo resistor and around 1800 when tripped so I chose a trigger value of 1000 to activate the buzzer.

Break the tripwire and trigger the alarm



You can see the measurements from the serial output here:



No comments:

Post a Comment