Project 3: Laser Tripwire using Laser Module and LDR
Share
Step 1: Overview
This project demonstrates how to use an ESP32 with an LDR (Light Dependent Resistor) module, a laser module, and a buzzer to create a simple security system. The system detects changes in light intensity using the LDR and triggers an alarm when the light beam is interrupted.
Step 2: Components Required
-  ESP32 Development Board
• LDR Module (Light Dependent Resistor)
• Laser Module
• Buzzer or LED
• Breadboard & Jumper Wires
• Mirrors (optional for creating a laser mesh) 
Step 3: Circuit Connections

Connecting the LDR Module to ESP32
-  VCC of LDR Module → 3.3V of ESP32
• GND of LDR Module → GND of ESP32
• DO(Output) of LDR Module → GPIO 13 of ESP32 

Connecting the Buzzer/LED to ESP32
-  Buzzer/LED Pin → GPIO 18 of ESP32
• GND of Buzzer/LED → GND of ESP32 
Connecting the Laser Module to ESP32
-  S (Signal) → GPIO 4 of ESP32
• VCC → 5V of ESP32
• GND → GND of ESP32 

Step 4: Breadboard Wiring Guide
-  Place the ESP32 on the breadboard.
2. Connect the LDR module’s VCC to the ESP32’s 3.3V, and GND to ESP32’s GND.
3. Connect the DO pin of the LDR module to GPIO 22 and the AO pin to GPIO 13 of ESP32.
4. Connect the buzzer’s positive pin to GPIO 18, and its negative pin to GND.
5. Connect the laser module’s S (Signal) pin to GPIO 4, VCC to 3.3V, and GND to ESP32’s GND.
6. Ensure all components are securely connected. 
Step 5: Upload ESP32 Code
Copy and paste the following code into your Arduino IDE and upload it to your ESP32:
const int ldrPin = 13;   // Analog input pin for LDR
const int buzzerPin = 18; // Buzzer pin
const int laser = 4;      // GPIO pin for laser module
void setup() {
  pinMode(ldrPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(laser, OUTPUT);
  digitalWrite(laser, HIGH);  // Keep the laser on
  Serial.begin(115200);
}
void playAlarm() {
  int melody[] = {1000, 1200, 1500, 1200}; // Frequency pattern
  int duration[] = {200, 200, 300, 200};   // Duration pattern
  
  for (int i = 0; i < 4; i++) {
    tone(buzzerPin, melody[i]);
    delay(duration[i]);
    noTone(buzzerPin);
    delay(100);
  }
}
void loop() {
  int analogValue = analogRead(ldrPin); // Read light intensity
  Serial.print(" | Analog Value: ");
  Serial.println(analogValue);
  // Trigger alarm if light beam is broken
  if (analogValue > 3500) { 
    playAlarm();
  }
  delay(100);
}
Step 6: How It Works
-  The laser continuously shines on the LDR sensor.
2. When an object interrupts the laser beam, the light reaching the LDR decreases.
3. The ESP32 detects this change and triggers the buzzer alarm.
4. You can modify the threshold value (3500 in code) to suit different lighting conditions. 
Step 7: Testing and Enhancements
-  Test in a dark environment for better sensitivity.
• Use mirrors to create a laser mesh for added security.
• Add an IoT feature by sending notifications when the alarm triggers.
• Use a relay to activate higher-power sirens or lights. 
Step 8: Conclusion
This simple ESP32-based security system demonstrates how LDR and laser modules can be used to detect intrusions. By expanding the setup with mirrors and multiple sensors, you can create a robust security system.