Arduino Laser Trip Wire





Introduction: Arduino Laser Trip Wire

For this dandy little project you'll need:

  • Arduino - I'm using an Arduino Uno
  • a 3 x 4 keypad
  • a 16 x 2 LCD screen
  • a laser module/sensor
  • a light dependent resistor (LDR)
  • a buzzer
  • 3 x 220 ohm resistors
  • 1 x 10K resistor
  • 1 x 10k potentiometer (optional, it's to adjust the LCD screen brightness)
  • an LED
  • and a ton of wires!

Step 1: LCD + Keypad

So, first thing I did was to get the LCD and keypad working together.

For this I pretended that it was some sort of arm/disarm (or entry/exit) thing.

My code for this part is at:

Getting the LCD wired up was tricky as most wiring diagrams for it don't show the last two pins wired up and these are required for the back light. Check my snazzy Fritzing wiring diagram to see how I wired up my LCD screen, pot (for adjusting brightness) and keypad. The pins for them all are also mentioned in my code.

NOTE: pin 1 isn't used as whenever I wired something to pin 1 I experienced weirdness. Not sure why. I expect it's something to do with the pins data uses?

If you don't have the keypad.h library you'll need to grab it from here:

Step 2: What's the Password?

I thought it’d be nice if the Arduino could take the keypad input and check it against a known password. Well, there just happens to be a handy dandy library called Password. You can grab it from:

Merging the code wasn’t going so well for me until I found this example: This is pretty much what I was wanting to achieve but with my 3 x 4 keypad.

I got stumped with why it wouldn’t accept my password. After much trial and error I found that in the line:

case ' ': guessPassword(); break;

the “case ‘ ‘“ statement is effectively the ‘enter’ key for the password. So, changing the ‘ ‘ to ‘#’ worked a treat. After that I tidied up the before and after screens.

Now it says ‘Enter code:’. You enter a four digit code and press the hash key (#) to finish. The LCD screen will either say ‘INVALID PASSWORD’, pause, and return to the ‘Enter code:’ screen, or it will say ‘VALID PASSWORD’, pause, display ‘Welcome!’, pause, then return to the ‘Enter code:’ screen.

My code thus far is at:


I’ve added to it using a simple IF/ELSE statement to do one of two things.

I’ve added an LED and an integer called ‘armed’. Initially ‘armed’ equals zero, and 'armedLed' equals A0. I’m now having to tweak the rules and use the analog pins for digital components as I’m quickly running out of pins!

Basically here’s what’s happening: if the password is correct and armed=0 (in other words: the system is off) then it will clear the screen, display ARMED!, change armed to equal 1 and light the red LED. Finally it will display “Code to disarm:”. Otherwise, armed must equal 1 (system is on) so display DISARMED!, make armed=0 and display “Code to arm:”.

After getting success with armed/disarmed I went and dug out a laser sensor from my cheapo 37-in-1 sensor box and a photoresistor (a Light Dependent Resistor, LDR for short). The LDR goes to A1 with a 220 ohm resistor with one leg and taking its other leg to ground. The laser sensor goes to the positive LED leg and to ground. Now, when the system is armed the laser flicks on and is made to point at the LDR.

To test that the LDR is actually registering anything I’ve introduced the serial code (using 9600 baud rate). In the main loop I have the code do an analog read from pin A1 and print the result to the IDE serial window. In other words, the number that the LDR is returning via it’s analog pin. With no laser and at room lighting the serial window shows a stream of numbers averaging 400-450. When I cover the LDR it drops to as low as 100-150. With the laser on, and pointing directly at the LDR, the serial output shoots up to a touch over 1,000. Almost the maximum it can reach. So, with the laser on I place an obstruction to the beam and the LDR registers the loss of the laser.

Source Code:

Step 4: The Great Finale!

We need the buzzer to play something and this is where we add another library called ‘pitches’. Rather than creating a new file and pasting in stuff I discovered a new way of adding a library. Click the little down arrow at the top right of the IDE window and choose to create a new tab. Paste your library text in there and voila! You’re done.

I acquired this ‘pitches’ text, and new tab idea, from:

I found (via the serial window) that the LDR registers about 600 when the LDR is not being hit by the laser. So, in my IF statement and in the main loop, I’m saying ‘if the system is armed and the LDR is registering less than 700 the laser must be broken so sound tone 3 via the buzzer’. I also added to the ‘wrong guess’ code:

tone(buzzer,notes[1],200); //boop! wrong code.

which just plays tone 1, a ‘boop’ noise, to let you know you got the code wrong.

You could of course use a repeating while statement to play several tones to have a more elaborate alarm/jingle.

Full code is at: With a demonstration of the system at:



    • Science of Cooking

      Science of Cooking
    • Pocket-Sized Contest

      Pocket-Sized Contest
    • Trash to Treasure

      Trash to Treasure

    We have a be nice policy.
    Please be positive and constructive.




    Can someone help me with code errors? I installed all the libraries and at the and when i try to upload the code i get this.( )

    Error Message.png


    I did the code right but when I went to download it I got an error message saying failure to comply

    Can you paste the exact error message? I'm hoping the error message give some help as to why it didn't compile and work for you.



    Great project... Could you please say how to make the alarm continue to run till a desired time limit even if the laser hits the photoresistor again once its broken and would stop only if the pass code is once again entered.... Thanks



    Could you run is with the arduin I keypad membrane which has more ports?


    I just noticed something. The I/O pins on the Atmega328 are rated abs. max. 40mA each. Be careful that the combined current of the LED and laser module does not exceed that. The laser module by itself will draw around 30mA @ 5V, but that is just an estimate and every laser module is a little different. The best thing to do wood bee to take an ammeter and measure the actual current coming out of the pin and adjust accordingly.

    My recommendation for a good, safe, cheap laser module is

    Looks like you ran out of digital I/O pins and had to use A0 as an output. I didn't even know you could do that. Total n00B

    Do u have all the correct coding for this arduino project

    As far as I'm aware the code is proper and working. I've not had any complaints about it.

    What makes you think the code isn't working properly? Let me know what your version is/isn't doing and I'll look over my/your code.