Laser alarm
Photoresistors are simple but extremely effective components, capable of converting light variations into electrical signals, making them ideal for security projects such as a laser alarm. This project is very easy to build and low-cost.
Required components:
- Jumper wires (male–male)
- 2 resistors (220 Ω – 330 Ω)
- Red LED
- Green LED
- Active buzzer
- Laser module (5V)
- 10 kΩ resistor (required if not using an LDR module)
- LDR module (photoresistor) or a simple photoresistor
- Breadboard
- Arduino Nano (or any compatible microcontroller)
Conexiuni:
Connections:
📟 Photoresistor (LDR module)
-
VCC → 5V (Arduino Nano)
-
GND → GND
-
OUT → A0
📟 If using a simple photoresistor (LDR)
-
One pin → 5V
-
The other pin → A0
-
10 kΩ resistor between A0 and GND
🔦 Laser module
-
+ → 5V
-
− → GND
🔔 Active buzzer
-
+ → D8
-
− → GND
💚 Green LED
-
Anode (+) → D6
-
Cathode (−) → 220 Ω resistor → GND
❤️ Red LED
-
Anode (+) → D7
-
Cathode (−) → 220 Ω resistor → GND
Code:
const int ldrPin = A0;
const int buzzerPin = 8;
const int ledVerde = 6;
const int ledRosu = 7;
int prag = 100; // între 30 și 200
bool alarma = false;
unsigned long startAlarma = 0;
unsigned long ultimulBip = 0;
bool stareBip = false;
void setup() {
pinMode(buzzerPin, OUTPUT);
pinMode(ledVerde, OUTPUT);
pinMode(ledRosu, OUTPUT);
digitalWrite(buzzerPin, LOW);
digitalWrite(ledVerde, HIGH); // verde aprins inițial
digitalWrite(ledRosu, LOW);
Serial.begin(9600);
}
void loop() {
int valoare = analogRead(ldrPin);
Serial.println(valoare);
unsigned long acum = millis();
// declanșare alarmă
if (valoare > prag && !alarma) {
alarma = true;
startAlarma = acum;
ultimulBip = acum;
stareBip = false;
}
if (alarma) {
// bip-bip-bip
if (acum - ultimulBip >= 300) {
stareBip = !stareBip;
digitalWrite(buzzerPin, stareBip);
ultimulBip = acum;
}
// LED-uri pentru alarmă
digitalWrite(ledRosu, HIGH);
digitalWrite(ledVerde, LOW);
// oprire alarmă după 5 secunde
if (acum - startAlarma >= 5000) {
alarma = false;
digitalWrite(buzzerPin, LOW);
digitalWrite(ledRosu, LOW);
digitalWrite(ledVerde, HIGH);
}
}
else {
// stare normală (laser prezent)
digitalWrite(buzzerPin, LOW);
digitalWrite(ledVerde, HIGH);
digitalWrite(ledRosu, LOW);
}
}
