LDR sensor project help

I started building a project using-

  1. LDR sensor
  2. LED
  3. Arduino Nano
  4. Buzzer
  5. 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
*/

Glad that you put your code the way it must be!

But, for speedy assistance, it's worth posting also the schematics.

And, if you have not read it yet, take a look at "How to get the best out of this forum"

2 Likes
  • Common LEDs require a series current limiting resistor.

  • Always show us a good schematic of your proposed circuit.
    Show us good images of your β€˜actual’ wiring.
    Give links to components.

1 Like

A proposed topology for your schmatic (Fig-1).


Figue-1:

Why does the sketch contain so many lines for a task as simple as turning on the LED in darkness and activating the buzzer in light?

Try a simple sketch of your own.

1 Like

Doesn't look good.
You've pulled too much power somewhere.
Look for short circuits and keep within the power specifications for individual pins and the board as a whole. Check polarities.
Any higher power items should be powered separately, through a transistor or similar.

Without a 270 ~ 470 Ohm series resistor, you probably burned out the Nano's output transistor, same with the buzzer. What is the buzzer's rated current at 5 volts?

:+1:

2 Likes

I used a 220 ohms resistor sorry forgit to mention it

Have you provided a schematic diagram of your circuit, as I cannot see it.
As previously mentioned by people here, use 1K resistor in series with the LED and use a resistor in series with buzzer.

Typical pin output current is 40mA max (You dont want to be close to that) with your 220R its close to 20mA, 1k will give you 5mA.

The power pack your using for power, what is its voltage?, I suggest on the Nano, use the Vin pin. Max Vin I believe is up to 12V, but double check. (Better if its less.

You said earlier you nano smoked..... not a good sign. Or was it something else?.

It could be youve killed one of the output pins, drawing too much current.

Fit the resistors and use different pins. (post your schematic).
Keep us posted.

1 Like

Continuous rating of an IO pin of UNOR3 is 20 mA. A led needs about 10 mA at 2.5V. So, a 220R series resistor will limit the led current at ((5+4.6)/2 - 2.5)/220 ~= 10 mA.

40 mA is absolute maximum rating for a very short period, which is not a target value for a design.

here is the connection diagram

Before you go any further, you may want to learn how to use a breadboard

1 Like