Skip to main content

Build. Learn.
Innovate.

Laser-Based Intrusion Detection System

Written By:

Published on:

Problem:

Many homes, classrooms, and small offices lack affordable, real-time intrusion detection systems. Existing commercial systems can be expensive and may not offer customizable features or local control. There is a need for a simple, low-cost, educational system to detect unauthorized access to a restricted area — such as a doorway, hallway, or cabinet — and provide instant alerts via a web interface or buzzer.

Parts Used

Component Purpose
ESP32 Dev Board Microcontroller to run the system and host web server
Laser Module (5V) Acts as the detection beam
Photoresistor (LDR) Detects the presence or interruption of laser
10kΩ Resistor Forms voltage divider with LDR for ADC input
Buzzer or LED (Optional) Audible or visual alarm
Breadboard & Jumper Wires For prototyping and easy connections
Wi-Fi Network For remote access to the web interface

from machine import ADC, Pin
import network, socket
from time import sleep

# Wi-Fi credentials
ssid = 'YOUR_WIFI_SSID'
password = 'YOUR_WIFI_PASSWORD'

# Connect to Wi-Fi
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while not wlan.isconnected():
pass
ip = wlan.ifconfig()[0]
print('Web server running on:', ip)

# Initialize LDR
ldr = ADC(Pin(34))
ldr.atten(ADC.ATTN_11DB)
ldr.width(ADC.WIDTH_10BIT) # Range: 0–1023

# Optional: Buzzer/LED
alarm = Pin(27, Pin.OUT)
alarm.off()

# Threshold for laser detection (adjust to your setup)
THRESHOLD = 500

# Web HTML page
html = """<!DOCTYPE html>
<html>
<head>
<title>Laser Intrusion System</title>
<style>
body { font-family: Arial; background: #111; color: white; text-align: center; padding-top: 40px; }
.status { font-size: 28px; padding: 20px; border-radius: 10px; display: inline-block; min-width: 300px; }
.safe { background-color: #2e8b57; }
.alert { background-color: #b22222; }
</style>
</head>
<body>
<h1>Laser Intrusion Detection</h1>
<div id="statusBox" class="status">Loading...</div>
<script>
async function updateStatus() {
const res = await fetch('/status');
const data = await res.json();
const box = document.getElementById('statusBox');
box.textContent = data.message;
box.className = 'status ' + (data.status === 'alert' ? 'alert' : 'safe');
}
setInterval(updateStatus, 1000);
updateStatus();
</script>
</body>
</html>
"""

# Web server
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)

# Current status
status = 'safe'

while True:
light = ldr.read()
if light < THRESHOLD:
status = 'alert'
alarm.on()
else:
status = 'safe'
alarm.off()

cl, addr = s.accept()
request = cl.recv(1024).decode()

if 'GET /status' in request:
msg = 'INTRUSION DETECTED' if status == 'alert' else 'Safe'
response = '{{"status": "{}", "message": "{}"}}'.format(status, msg)
cl.send('HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n')
cl.send(response)
else:
cl.send('HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n')
cl.send(html)

cl.close()
sleep(0.1)

Benefits

  • Real-time intrusion alerts: Immediately detect unauthorized access when the laser beam is broken.
  • Web dashboard: View live status updates using a simple browser interface — no app required.
  • Educational value: Introduces key concepts in IoT, analog-to-digital conversion, and web-based control.
  • Low cost & DIY: Easily replicable for under $10–15 using common electronics components.
  • Customizable: Expandable to multiple sensors or integrated with smart home systems.

Solution

The system works by aligning a laser beam directly onto a photoresistor (LDR). As long as the beam remains unbroken, the LDR receives strong light and reports a high analog value to the ESP32. When someone crosses the path and interrupts the beam, the value drops below a set threshold, triggering:
A change in web dashboard status (Safe → INTRUSION DETECTED)

  • An optional buzzer or LED alert
  • Logging or further integration possibilities (if extended)

The ESP32 continuously reads the LDR, processes logic in MicroPython, and serves a real-time interface that can be accessed from any browser on the same Wi-Fi network.

Project Code

Projects
ShowCase

Real-World Projects
with Code & Hardware

Insights, Ideas
& How-Tos

Help, Support, and
Common Questions

What types of projects can I find on your website?

You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.

You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.

You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.

You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.