I started building a project using-
- LDR sensor
- LED
- Arduino Nano
- Buzzer
- 10k OHMs Resistor
So I started building - I connected Led negative to GND and positive to digital pin and LDR to A0 and GND ( with 10k OHMs resistor) I connected the buzzer to GND and digital pin the code was supposed to turn on the LED when LDR sensed darkness and when it detected light the buzzer is supposed to play a tune. After a few minutes of testing the Arduino randomly burned out none of the lights were switching on after a few days I tried switching it on again and the lights on the Arduino were switching on but it was not being detected in the Arduino IDE. (The only external power source I use was a 10000 MAH power bank) PLEASE give advice.
Here's the code-
/*
NIGHT LIGHT WITH BUZZER
- LED turns ON when dark
- Single beep when it gets dark
- Pleasant beeps when it gets bright
Wiring:
- 5V β Pot left pin
- A0 β Pot middle pin β LDR one leg
- GND β LDR other leg
- Pin 13 β [220Ξ©] β LED long leg (+), LED short leg (-) β GND
- Pin 8 β Buzzer (+), GND β Buzzer (-)
*/
const int ldrPin = A0;
const int ledPin = 13;
const int buzzerPin = 8;
const int threshold = 512;
const int hysteresis = 50; // Stability buffer
bool wasDark = false;
unsigned long lastChangeTime = 0;
const unsigned long debounceDelay = 2000; // Wait 2 seconds between transitions
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
Serial.println("Night Light with Buzzer");
Serial.println();
}
void loop() {
int lightLevel = analogRead(ldrPin);
Serial.print("Light Level: ");
Serial.print(lightLevel);
Serial.print(" | ");
if (lightLevel < (threshold - hysteresis)) {
// DEFINITELY DARK - LED ON
digitalWrite(ledPin, HIGH);
Serial.print("LED: ON (Dark)");
// Beep ONCE when entering dark (with time check)
if (!wasDark && (millis() - lastChangeTime > debounceDelay)) {
nightBeep();
Serial.println(" - BEEP!");
wasDark = true;
lastChangeTime = millis();
} else {
Serial.println();
}
}
else if (lightLevel > (threshold + hysteresis)) {
// DEFINITELY BRIGHT - LED OFF
digitalWrite(ledPin, LOW);
Serial.print("LED: OFF (Bright)");
// Beep pattern when entering bright (with time check)
if (wasDark && (millis() - lastChangeTime > debounceDelay)) {
lightBeeps();
Serial.println(" - Beep Pattern!");
wasDark = false;
lastChangeTime = millis();
} else {
Serial.println();
}
}
else {
// IN BETWEEN - maintain current state
if (wasDark) {
digitalWrite(ledPin, HIGH);
Serial.println("LED: ON (maintaining)");
} else {
digitalWrite(ledPin, LOW);
Serial.println("LED: OFF (maintaining)");
}
}
delay(200);
}
// Single beep for night mode
void nightBeep() {
digitalWrite(buzzerPin, HIGH);
delay(300);
digitalWrite(buzzerPin, LOW);
}
// Pleasant beep pattern for light mode
void lightBeeps() {
// Short-Short-Short-Long pattern
digitalWrite(buzzerPin, HIGH);
delay(100);
digitalWrite(buzzerPin, LOW);
delay(80);
digitalWrite(buzzerPin, HIGH);
delay(100);
digitalWrite(buzzerPin, LOW);
delay(80);
digitalWrite(buzzerPin, HIGH);
delay(100);
digitalWrite(buzzerPin, LOW);
delay(80);
digitalWrite(buzzerPin, HIGH);
delay(400);
digitalWrite(buzzerPin, LOW);
}
/*
HOW IT WORKS:
π When it gets DARK:
- LED turns ON
- Single beep (once)
- Stays quiet while dark
βοΈ When it gets BRIGHT:
- LED turns OFF
- Pleasant beep pattern (beep-beep-beep-BEEP)
- Stays quiet while bright
CALIBRATION:
- Open Serial Monitor
- Watch Light Level numbers
- Cover LDR β should trigger dark mode
- Uncover β should trigger bright mode
- Turn pot knob to adjust when it triggers
- Change 'threshold' in code if needed
TROUBLESHOOTING:
- If LED always ON: increase threshold (try 700)
- If LED always OFF: decrease threshold (try 300)
- If readings are 1000+: check LDR wiring
- Buzzer should only beep during transitions, not continuously
*/


