Introduction: Twittering Laser Tripwire With Webcam Capture

Picture of Twittering Laser Tripwire With Webcam Capture

This instructable will show you how to construct a laser tripwire that can twitter and grab an image from a webcam, as well as execute any command you can put in a bash script.
This instructable is actually quite simple and is even suitable as a beginner arduino project. It requires a GNU/linux (or possibly Mac) operating system with the arduino IDE and Processing IDE working properly. This project could also be implemented in Windows if you created a more complex processing application.

Hardware Requirements
Arduino Board
Laser pointer/pen
Light Dependant Resistor
100Kohm Resistor


Software Requirements
Arduino IDE
Processing IDE
Linux OS (I'm using Arch Linux for this project)
Webcam (part of xawtv)

Other Stuff:
Solder and Soldering Iron
Hot Glue Gun
Straw or Ballpoint Pen

Step 1: Connecting the LDR

Picture of Connecting the LDR

I needed the LDR and the laser to be in a position where they would not move around so I connected my LDR and resistor directly to the Arduino without a breadboard, but you can do it however you like, below are the photos for soldering and bending them into shape I also included a simple diagram.

I cut the end off of a ballpoint pen so that I could slip it over the LDR to prevent ambient light from affecting the reading, you could also use a straw.

Step 2: Connecting the Laser Pointer

Picture of Connecting the Laser Pointer

You can find these really cheap at some retail stores, I know that mine was under $3.

Unscrew the end and remove the batteries.

Use a hacksaw or sharp seraded knife to cut off 1 to 1/2 inch of the laser pointer, so that you can access the spring. You may not need to do this depending of what kind of laser pointer you found.

Connect and solder a wire to the spring ( I bent mine out to make it easier ) then rough up a small part of the outside of the laserpointer with a knife/sandpaper/pliers ect so that you can solder it to the outer casing. Then tape the button that activates the laser.

On my laser pointer the spring is + and the casing is - yours might be reversed. The best way to check is to take note of what position the batteries go in, the coin cells will have + marked on one side, if that side touches the spring then the spring is +.

Connect your + wire (Casing) to digital pin 13 on your arduino and you - wire (spring) to GND. 

Now upload the arduino sketch and ensure that you are getting readings from the LDR and that the laser is on.

To accomplish this you must uncomment this line:

and comment out this one:

If all is well you can position your Laser and LDR in a  fixed position, bounce the laser to the LDR with a mirror to see that its registering. When the laser is detected it should print a "1".  You may need to adjust the threshold values depending on your LDR.

Step 3: Everything in Place

Picture of Everything in Place

Now you are going to need to make sure that your LDR and laser are not going to be moving around while you're testing the rest of the software.

I used a hot glue gun and used a small amount of hot glue on each edge of my arduino board to secure it to a plastic cassette case, (a piece of wood and some screws would be ideal but I didn't have any) The hot glue should be removed easy and if it doesn't it's in an unimportant location.

I then hot glued my laserpointer onto the platic cassette case and rested it against the arduino's digital headers. The LDR doesn't move around so I didn't have to worry about it moving. Your set up may vary but you get the idea.

You'll then want to make sure the whole thing won't move around, I used sticky tack on the bottom of the cassette case and put a jar of change on top of the USB cable so that it would not move around while testing.

You'll also want to affix your mirror to something, it's up to you to figure this one out I used sticky tack to adhere the mirror to an old and heavy Kodak brownie camera and set it on a stool with a few books on top while testing everything out. Later I just affixed the mirror to my camera tripod.

Step 4: Processing

Picture of Processing

Load the Processing sketch and leave this line commented out:

Make sure that your laser is pointed at the LDR and run the sketch. The screen area should be green and when you block the laser it should turn red, then after 10 seconds it should turn back to green.

The 10 second hold is to ensure that you're not twittering the same message multiple times, you can change this value by modifying the "int threshold=10000" variable.

Now that everything is working it's time to get your scripts in place

put twitter.trip into:

Change these to your twitter username and password

open up a terminal and type:
chmod a+x /home/your_username/twitter.trip

then run it

go to your twitter page and you should see "laser tripped on..." as your last tweet.

Now time to get the webcam working, you can skip this step if you don't want to use a webcam or if you know how to grab a frame from a webcam in linux via script.

First you want to make sure that the program called "webcam" is installed, it's a part of xawtv.

Arch Linux: pacman -S xawtv
Fedora: yum install xawtv
Debian/Ubuntu: apt-get install xawtv

Then configure .webcamrc however you need, mine is below.
The best part about webcam is that it has ftp support so that the image can be uploaded to a webserver, if you don;t want FTP comment that part out.

.webcamrc in /home/your_username/

device = /dev/video0
text="%Y-%m-%d %H:%M:%S"
#infofile = filename
fg_red = 0
fg_green = 0
fg_blue = 0
fg_red = 255
fg_green = 255
fg_blue = 255
width = 320
height = 240
delay = 0
wait = 0
input = pac207
#norm = pal
rotate = 0
top = 0
left = 0
bottom = -1
right = -1
quality = 75
trigger = 0
once = 1
archive = /home/action-owl/webcam/%Y-%m-%d--%H:%M:%S.jpg

host =
user = username
pass = password
dir  = public_html/img/webcam
file = webcam.jpg
tmp  = uploading.jpg
passive = 1
debug = 1
auto = 0
local = 0
ssh = 0

Step 5: Using Open() in Processing

Picture of Using Open() in Processing

The "open()" function works a little differently depending on your environment so you may need to find out what works for you, the method below should work.


If you are using Nautilus or Thunar File Managers(XFCE and Gnome):
Navigate to twitter.trip and right-click it.
Select "Open with other application"
select "Use a custom command"
and type in "bash"

Processing is supposed to send executables to the shell to be run but after trying many methods this was the one that worked. This is also why the sketch has a .trip extension.

Step 6: Now Run It.

Now everything should be ready to go!

Run the processing sketch and check your twitter and webcam archive location for the result.

Something is up with the file downloads.
So here's the code, again.

# twitter.trip
trip=$( date +'%A %b %d, %l:%M%p' )
stat="laser tripped: "$trip
result=`curl -u $user:$pass -d status="$stat" $url`
#save webcam pic
date +'%A %b %d, %l:%M%p' > /home/username/someplace

// processing code
import processing.serial.*;
Serial myPort;
char inBuffer;
int wait, now, timeout = 10000;
boolean hold = false;

void setup() {
  myPort = new Serial(this, Serial.list()[0], 9600);

void draw() {
  while (myPort.available() > 0)
    inBuffer = myPort.readChar();  

      if ( !hold )
        wait = millis();
        hold = true;
  now = millis();
  if (now > (wait + timeout))
    hold = false;

// arduino code

int ledPin =  13;
int analogPin = 0;
int ldrVal = 0;
int threshold = 500;

void setup()
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);

void loop()                    
  ldrVal = analogRead(analogPin);
  if ( ldrVal > threshold )


dreamingforward (author)2016-01-26

I haven't made any projects for the Arduino. How does Processing cross-compile for the Arduino CPU?

Shashwat 56 (author)2015-01-31

Can it be made on Arduino Uno

Shashwat 56 (author)2015-01-31

Can it be made on Arduino Uno

Shashwat 56 (author)2015-01-31

Can it be made on Arduino Uno

Shashwat 56 (author)2015-01-30

Mdob, can u tell me your codes

Shashwat 56 (author)2015-01-30

Hey dude Mdob. Cam u plz tell me your codes?

Lotek5000 (author)2013-11-27

Nice project - I am setting one up to take a picture of whichever culprit keeps messing with the lab at work and post there image up on the board of shame.

I had to mod the code a little to get it to work on MAC (OSX) 10.6.8 so thought I should upload my code for others to use. Many thanks and here it is.

int ledPin = 13; // digital pin to which led is connected
int analogPin = 0; // analog pin to which ldr is connected
int ldrVal = 0; //variable to store ldr value
int threshold = 1000; //variable to store threshold in memory

void setup()
Serial.begin(9600); //set baud rate

void loop()
ldrVal = analogRead(analogPin);

if ( ldrVal > threshold )


DidyDoddie (author)2012-10-27

Will this code still work today? I heard that twitter had made it harder to tweet from an arduino.

P.s. Im am a complete new person to arduinos and programming so i have no idea!

Mdob (author)2011-05-17

Here is what I did. I modified some of it a bit. I used the processing code but made it open up a .bat file which used snapz to take a picture and then used a .vbs file to attach that picture and send and email to my gmail account. This was all using windows.This has been a fun project. Now, all I need to do is make it all self-contained. I could possibly use the "$25 Computer" ( with a camera and put it all in a project box with a laser module. That would be fun. If anybody wants the code that I used just ask me and I will host it.

EngineeringChannel (author)Mdob2012-03-27

yes please, followed by an instructable

Mdob (author)EngineeringChannel2012-04-02

I do have an instructable up if you look at my account. (Link) I didn't want to go through all of the laser stuff again so I made it activate on login. To adapt it to make it so that it will open up the Send.bat file (Which will start the process to take the picture and send the email) simply replace


in the processing code with the location of your files. eg.


That is all that you have to modify from my instructable. Also, don't follow the instructions where it says that you have to hide it or make it run on startup. I hope that helps, Good Luck!

wallBme (author)Mdob2012-05-28

Hey, I've tried doing what you did and I made twitter.trip into a .bat file. It runs just fine when I double-click it and/or run it from the command prompt, but open() doesn't seem to be running it. Do you have any ideas as to why it wouldn't be working? Thanks

Mdob (author)wallBme2012-05-29

I have very little experience with Processing so I really don't know. When I tried it (this was quite a while ago) it worked just fine. Maybe somebody who knows a little bit more about Processing could help you out.

wallBme (author)Mdob2012-05-29

ok, thanks

jparfoot (author)Mdob2011-10-15

Hi mate great idea would love it if i could get a copy of your source code. cheers Jordan

cpoplawski (author)2012-01-22

Can someone please modify this to not use processing? I am new to arduino and have been bashing my head in to make this completely stand alone (no pc needed). I am trying to make it so that it just tweets "laser has been tripped" after the alarm sounds. I have an Arduino Uno with Ethernet shield.

riddik001 (author)2011-12-01

"put twitter.trip into:
/home/your_username/" where do i find the /home/my-username? folder ? and which software i will use the arduino or processing to load the script thanks.

dudesurge (author)riddik0012011-12-24

you answered your own question, you find it in the HOME folder, as for the other question you can get the software from under downloads

Catch_22_ (author)2011-07-12

Basic auth is now unsupported by twitter. So you wont be able to use the code from twitter.trip.

User instead. Youre still using curl but acts as a proxy between you and twitter, allowing you to use basic auth. Twitter now uses oAuth which is a pain. For the code below, use your SuperTweet username and password, it is separate from your twitter login.

trip=$(date +'%A %n %d, %l:%M%p' )
stat="["$trip"] Door has been opened"
curl -u USERNAME:PASSWORD -d "status=$stat" --insecure

tranoxx (author)2011-06-13

when I use the Arduino code it does the opposite of what you said it would instead of making a 1 it stops making 1's, is this important?

polishdude (author)2011-02-19

can you do this with windows?

habzbah (author)2010-10-24

Nice tutorial, will try this soon!

ledfreak01 (author)2010-09-22

i tried to upload the code to my arduino and it gave me an error about the line
fill(#36ff00) how could i fix this im running windows btw

kenkoknz (author)2010-09-09

Excellent project, but it appears now twitter does not support basic authentication when you run curl...what need to be changed in the bash script?

action_owl (author)kenkoknz2010-09-19

They recently updated Twitter's authentication method, I'll have to rewrite the bash script sometime, but considering I have capstone this semester I may not get around to it :(

yoyojoeco (author)2010-09-11

Do you think it would be possible to send it to a google buzz or other account instead?

yoyojoeco (author)2010-09-09

Sweet but looks complicated.

sardines454 (author)2010-01-26

I can't seem to download the files in your 'ible.  Every time I click the link it downloads a file with some wierd name and a .tmp extensions.  Do you have anywhere else that I could download the code from?

action_owl (author)sardines4542010-02-01

That's weird, Something must be up on instructables end, I'm adding the code in plain text to step 6 right now.

mman1506 (author)action_owl2010-09-06

just rename the file extension to pde and it will work

I've had this problem before. Just rename the file to the extension you need. SIlly

Computothought (author)2010-08-04

Cool and awesome!! I would be tempted to just hook the ldr to my joystick port and cut out the arduino.. I would use freebasic to get info from the joystick port and then call twidge to do the twitter via the shell. Probably use an antique computer running Debian to do all this.

_soapy_ (author)2010-07-04

Looking at this, and your tweets, either you are working really late, or a 5am badger or moth is messing your sensor up. Might I suggest a small delay in the code so it triggers after (say) 1/4 of a second?

stanuszekj (author)2010-05-10

the ohms resistor is 100K about the LDR radioshack does not have them in stock throughout there website chain

yosama101 (author)2010-01-19

How do you open the twitter.trip file? and Im having a bit of trouble with your instuction, like the code.

action_owl (author)yosama1012010-02-01

Could you be a bit more specific? You can message me with detailed questions too.

yosama101 (author)action_owl2010-02-02

Oh, I dont need this anymore. I did it for a school assignment. It ended up not working, BUT we got an 85% on it, which is good lool for it not working lmao.

danahyatt (author)2010-01-03

Nice project. I want to try this one for my first. Where do I purchase the Arduino? I will mount the laser like in the plans. Can you expand on how you used the webcam, zawtv? Perfect choice of linux arch for the os.

action_owl (author)danahyatt2010-01-03

I usually get my arduinos from,  If you install zawtv it will also install the program called "webcam". If you have problems getting your webcam working start a thread in the Arch Linux forums and somebody will get you going, Or post back with specifics and I'll try to help your through any problems, good luck.

danahyatt (author)action_owl2010-01-04

Printed my parts list and read Arch Linux wiki & forums plus watched few Youtube how to build the Arch Kernel. Will order Arduino today from  Thanks!

netgrazer (author)danahyatt2010-01-13

Build a kernel? I'm not sure about Arch Linux, but I never had to mess with anything like that on Ubuntu. Painless installation, works "out of the box".

action_owl (author)netgrazer2010-02-01

I've never had to compile the kernel in Arch either?

danahyatt (author)action_owl2010-01-04

I am purchasing the "Aruuino USB Board" from sparkfun, is this the right one?

coolio49085 (author)2010-01-07

1+ for Django

Alpvax (author)2010-01-07

could require alot of code editing, (i dont know as im not an arduino user, although this could convince me otherwise) but could it be linked with just a webcam and used as an intruder alert, getting the webcam to photograph whoever entered your room??

awesome 'ible

jridley (author)2010-01-07

Wouldn't it be more useful to use an infrared laser?  The red laser is too easy to notice.

yosama101 (author)2010-01-05

How sensitive does the LDR have to be? WHat were the specifications of the LDR you used?

action_owl (author)yosama1012010-01-05

The one I used is this one or one very close to it. (it was in my box of random parts so not 100% sure) The details don't matter so much as long as you get light readings from it.
The laser will be powerful enough to activate it so any basic LDR/Photocell will work. Worst case scenario you may have to adjust the "int threshold=500;" line in the arduino.pde file.

renehug (author)2010-01-04

Thanks for posting this here! Do you have any idea what range you can cover with this setup?

action_owl (author)renehug2010-01-05

 The limiting factor is the range of the laser and how good you are at aiming the laser back at the LDR, in theory you could even use multiple mirrors and cover a lot of ground

About This Instructable




More by action_owl:Twittering Laser Tripwire with Webcam CaptureNintendo Keyless Entry SystemUnderwater Microphone (Hydrophone)
Add instructable to: