Build an Arduino laser tripwire alarm that sounds a latching buzzer when anyone breaks the beam, with auto-calibration, an armed LED and a one-button arm and reset.
Need it ready-made? Buy Ready Project
This Arduino laser tripwire alarm guards a doorway, corridor or cupboard with an invisible-in-daylight line of light. A small KY-008 laser module shines across the gap onto an LDR. The moment someone walks through and breaks the beam, the Arduino latches the alarm: the buzzer pulses and the red LED flashes until you press the reset button.
It is a great first security project for students and hobbyists. It uses only cheap parts, needs no libraries, and the code calibrates itself to the room light every time you arm it, so it works in a bright room and in the dark.

How It Works
- Beam and sensor: the LDR and the 10 kΩ resistor form a voltage divider on A0. When the laser hits the LDR its resistance falls and the A0 reading goes up; when the beam is blocked the reading drops.
- Auto-calibration: when you press the button to arm, the Arduino turns the laser off, reads the room light, turns it back on, reads again and sets the trip threshold half way between the two. If the gap is too small (the laser is not aimed at the LDR) the red LED blinks three times and the alarm stays disarmed.
- Glitch filter: the reading must stay below the threshold for 30 ms before the alarm trips, so a flicker or an insect does not set it off, but a person walking through always does.
- Latching alarm: once tripped the alarm stays on even if the person steps out of the beam. Only the button resets it.
- One-button control: press once to arm (green LED on and a short beep), press while armed to disarm, and press during an alarm to silence and reset it.
Components Required
- Arduino Uno (or Nano) with USB cable – 1
- KY-008 650 nm laser diode module – 1
- LDR (light dependent resistor, 5 mm) – 1
- 10 kΩ resistor (LDR divider) – 1
- 5V active buzzer – 1
- Red LED (alarm) and green LED (armed) – 1 each
- 220 Ω resistors for the LEDs – 2
- Tactile push button – 1
- Breadboard, jumper wires and a small black tube or straw to shade the LDR – 1 set
Wiring Table
| Part / pin | Arduino pin | Notes |
|---|---|---|
| KY-008 laser S | D7 | Arduino turns the laser on and off for calibration |
| KY-008 laser – | GND | The middle pin is not needed |
| LDR leg 1 | 5V | Either leg, the LDR has no polarity |
| LDR leg 2 | A0 | Also connect a 10 kΩ resistor from A0 to GND |
| Active buzzer + | D8 | Buzzer – to GND |
| Red LED (alarm) | D9 through 220 Ω | Cathode (short leg) to GND |
| Green LED (armed) | D10 through 220 Ω | Cathode to GND |
| Push button | D2 and GND | Uses the internal pull-up, no resistor needed |
Connection Diagram
The illustration above shows the build on a breadboard. It is a layout guide only. Follow the pin-accurate drawing below, which matches the wiring table exactly.
Code
Open the sketch in the Arduino IDE, choose Arduino Uno (or Nano) and the right COM port, and upload. No extra libraries are needed. Open the Serial Monitor at 9600 baud to see the light readings and the threshold while you set it up.
/*
Arduino Laser Tripwire Security Alarm
Arduino Project Hub - https://ardiunoprojecthub.online
A KY-008 laser shines on an LDR. When someone breaks the beam the
LDR reading drops, the alarm latches ON (buzzer + red LED) and stays
on until you press the button. Press the button again to re-arm.
Wiring:
KY-008 laser S -> D7 (the Arduino switches the laser on/off)
LDR + 10k divider -> A0 (LDR from 5V to A0, 10k from A0 to GND)
Active buzzer + -> D8
Red LED (alarm) -> D9 through 220 ohm
Green LED (armed)-> D10 through 220 ohm
Push button -> D2 to GND (internal pull-up)
*/
const byte LASER_PIN = 7;
const byte LDR_PIN = A0;
const byte BUZZER_PIN = 8;
const byte RED_LED = 9;
const byte GREEN_LED = 10;
const byte BUTTON_PIN = 2;
const unsigned long BREAK_TIME_MS = 30; // beam must be broken this long (filters flicker)
const int MIN_MARGIN = 60; // minimum ADC gap between beam ON and beam OFF
enum State { DISARMED, ARMED, ALARM };
State state = DISARMED;
int threshold = 0;
unsigned long brokenSince = 0;
// Wait for a clean button press (with debounce) and release
bool buttonPressed() {
if (digitalRead(BUTTON_PIN) == LOW) {
delay(25);
if (digitalRead(BUTTON_PIN) == LOW) {
while (digitalRead(BUTTON_PIN) == LOW) {}
delay(25);
return true;
}
}
return false;
}
// Average a few readings to remove noise
int readLdr() {
long sum = 0;
for (byte i = 0; i < 8; i++) sum += analogRead(LDR_PIN);
return sum / 8;
}
// Measure the light level with the laser on and off and put the
// threshold half way between. Returns false if the beam is not aimed.
bool calibrate() {
digitalWrite(LASER_PIN, LOW);
delay(200);
int dark = readLdr();
digitalWrite(LASER_PIN, HIGH);
delay(200);
int lit = readLdr();
Serial.print(F("Beam off: ")); Serial.print(dark);
Serial.print(F(" Beam on: ")); Serial.println(lit);
if (lit - dark < MIN_MARGIN) return false;
threshold = (lit + dark) / 2;
Serial.print(F("Threshold: ")); Serial.println(threshold);
return true;
}
void setOutputs(bool green, bool red, bool buzzer) {
digitalWrite(GREEN_LED, green);
digitalWrite(RED_LED, red);
digitalWrite(BUZZER_PIN, buzzer);
}
void arm() {
if (calibrate()) {
state = ARMED;
brokenSince = 0;
setOutputs(HIGH, LOW, LOW);
digitalWrite(BUZZER_PIN, HIGH); // short confirm beep
delay(80);
digitalWrite(BUZZER_PIN, LOW);
Serial.println(F("ARMED"));
} else {
// Beam not reaching the LDR: blink red three times
Serial.println(F("Aim the laser at the LDR and press again"));
for (byte i = 0; i < 3; i++) {
setOutputs(LOW, HIGH, LOW); delay(150);
setOutputs(LOW, LOW, LOW); delay(150);
}
digitalWrite(LASER_PIN, HIGH); // keep laser on so you can aim it
}
}
void setup() {
Serial.begin(9600);
pinMode(LASER_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
digitalWrite(LASER_PIN, HIGH); // laser on for aiming
setOutputs(LOW, LOW, LOW);
Serial.println(F("Laser tripwire ready. Aim the beam, then press the button to arm."));
}
void loop() {
switch (state) {
case DISARMED:
if (buttonPressed()) arm();
break;
case ARMED:
if (readLdr() < threshold) {
if (brokenSince == 0) brokenSince = millis();
if (millis() - brokenSince >= BREAK_TIME_MS) {
state = ALARM;
Serial.println(F("ALARM! Beam broken"));
}
} else {
brokenSince = 0;
}
if (buttonPressed()) { // press while armed = disarm
state = DISARMED;
setOutputs(LOW, LOW, LOW);
Serial.println(F("DISARMED"));
}
break;
case ALARM: {
// Latched alarm: flash red LED and pulse the buzzer until reset
bool on = (millis() / 250) % 2;
setOutputs(LOW, on, on);
if (buttonPressed()) {
state = DISARMED;
setOutputs(LOW, LOW, LOW);
Serial.println(F("Alarm reset. Press again to re-arm."));
}
break;
}
}
}
Setting Up and Testing
- Place the laser module on one side of the doorway and the LDR on the other, both at the same height (knee height works well).
- Slide a short black straw or tube over the LDR so room light does not reach it from the side. This makes the beam much easier to detect.
- Power the Arduino. The laser turns on so you can aim the red dot right at the LDR.
- Press the button. The green LED and a short beep confirm the alarm is armed. If the red LED blinks three times instead, re-aim the laser and press again.
- Walk through the beam: the buzzer pulses and the red LED flashes. Press the button to reset, and press once more to re-arm.
- For a longer gap, bounce the beam with one or two small mirrors to cover a zig-zag path.
Safety: never look into the laser or point it at anyone’s eyes. Keep the beam below eye level, especially around children and pets.
Troubleshooting
| Problem | Fix |
|---|---|
| Red LED blinks three times when arming | The beam is not reaching the LDR. Re-aim the laser, shorten the distance, or shade the LDR with a tube. |
| Alarm trips by itself | Sunlight or a lamp is changing the LDR reading. Shade the LDR better, or raise BREAK_TIME_MS to 80. |
| Alarm never trips | Check the 10 kΩ resistor goes from A0 to GND (not 5V) and watch the Serial Monitor readings while you block the beam. |
| Laser does not turn on | The S pin must go to D7 and – to GND. Some modules label the pins differently; check the module print. |
| Buzzer is silent | Make sure it is an active buzzer (it beeps on plain 5V). A passive buzzer needs tone() instead. |
| Button does nothing | Use two legs that are on opposite sides of the button, or rotate it 90 degrees on the breadboard. |
Ideas to Extend
- Add an HC-05 Bluetooth module or swap the Uno for an ESP32 to send a phone alert when the beam is broken.
- Count how many people pass through with a 7-segment display instead of sounding an alarm.
- Drive a 5V relay from D8 to switch on a light or siren when the alarm trips.
1 thought on “Arduino Laser Tripwire Security Alarm with LDR, Buzzer & Reset Button”