Welcome to Project 39. Let’s be honest: we all have that one roommate, child, or golden retriever with zero respect for personal boundaries—specifically, the boundaries surrounding the good snacks you hid on the bottom shelf of the pantry. You could try talking to them, but reasoning with a snack-thief is like trying to convince a blown capacitor to un-explode. It’s futile.

Instead, it’s time to channel your inner super-villain and build a James Bond-style laser tripwire.

Today, we’re taking a cheap KY-008 laser module, pointing it directly at a light-sensitive resistor, and writing a few lines of code to scream bloody murder (via a highly annoying piezo buzzer) the exact millisecond the beam is broken.

Will it stop a determined cat? Absolutely not. Will it scare the living daylights out of you when you forget you armed it at 2 AM? Guaranteed.

Let’s build it.


🛒 Gear You Need

If you don’t already have a box of parts, here are the direct links to the exact components you need for this build.


⚡ The Schematic: How It Works

The concept is beautifully simple. A photoresistor (LDR) changes its electrical resistance based on how much light hits it. When the bright red laser is shining directly on it, the resistance is low, and the Arduino reads a high analog value.

When a hand, foot, or furry tail breaks the beam, the LDR goes dark, the resistance spikes, and the analog value drops like a stone. Our code detects that sudden drop and triggers the buzzer.

Wiring Guide

  1. KY-008 Laser Module:
  2. Photoresistor (LDR):
  3. Piezo Buzzer:

💻 The Code

Copy this code into your Arduino IDE, upload it, and try not to look directly into the laser with your remaining good eye.

// Project 39: Laser Tripwire Security Alarm
// by Electron Parade

const int ldrPin = A0;      // Photoresistor connected to Analog 0
const int buzzerPin = 8;    // Piezo buzzer connected to Digital 8
const int threshold = 500;  // Adjust this value! Higher = more sensitive

void setup() {
  Serial.begin(9600);
  pinMode(buzzerPin, OUTPUT);
  pinMode(ldrPin, INPUT);
  
  // Give you 5 seconds to get out of the room before arming!
  Serial.println("Arming in 5 seconds...");
  delay(5000);
  Serial.println("System Armed!");
}

void loop() {
  int lightLevel = analogRead(ldrPin);
  Serial.print("Current Light Level: ");
  Serial.println(lightLevel);

  // If the light level drops below our threshold, the beam is broken!
  if (lightLevel < threshold) {
    Serial.println("INTRUDER DETECTED!");
    soundAlarm();
  }
  
  delay(100); // Small delay for stability
}

void soundAlarm() {
  // Beep wildly for 3 seconds
  for (int i = 0; i < 15; i++) {
    tone(buzzerPin, 2000); // 2000Hz tone
    delay(100);
    noTone(buzzerPin);
    delay(100);
  }
}

🔧 Calibration

Because every room’s ambient lighting is different, you will almost certainly need to adjust the threshold variable.

  1. Open the Serial Monitor (magnifying glass icon in the top right of the IDE).
  2. Look at the numbers printing when the laser is hitting the sensor (e.g., 850).
  3. Block the laser with your hand. Look at the numbers now (e.g., 300).
  4. Set your threshold variable somewhere in the middle (e.g., 500).

Now go set it up across your doorway and wait for the chaos.