Light-Sensitive Night Lamp with Arduino

Arduino Experiment: Light-Sensitive Night Lamp

Difficulty Level: Beginner

Learn how to create a light-sensitive night lamp using an LDR and Arduino. This project automatically switches on an LED when it detects darkness, making it an ideal starter project.

1. Required Components

2. Wiring

Follow these steps to wire your circuit:

3. Code Explanation

The code below reads light intensity using the LDR and controls the LED based on a predefined light threshold. Key functions include:

4. Arduino Code

Copy the following code into your Arduino IDE:


// Define pins for the LDR and LED
const int ldrPin = A0;    // LDR connected to analog pin A0
const int ledPin = 9;     // LED connected to digital pin 9

// Variable to store LDR reading
int ldrValue = 0;

void setup() {
  // Set the LED pin as output
  pinMode(ledPin, OUTPUT);

  // Start serial communication (optional, for debugging)
  Serial.begin(9600);
}

void loop() {
  // Read the value from the LDR
  ldrValue = analogRead(ldrPin);

  // Print the LDR value to the serial monitor (optional)
  Serial.println(ldrValue);

  // Define a light threshold (adjust based on your environment)
  if (ldrValue < 500) {
    // If it's dark, turn the LED on
    digitalWrite(ledPin, HIGH);
  } else {
    // If it's bright, turn the LED off
    digitalWrite(ledPin, LOW);
  }

  // Add a short delay
  delay(100);
}
            

5. Upload and Test

  1. Connect your Arduino to your PC using a USB cable.
  2. Open the Arduino IDE and paste the code above into a new sketch.
  3. Select your Arduino board and the correct port in the IDE.
  4. Click the "Upload" button to program your Arduino.
  5. Observe the LED: It should turn on when the light level drops below the threshold and turn off in brighter conditions.

6. Additional Tips

7. Conclusion

Congratulations! You’ve built a light-sensitive night lamp using an Arduino and an LDR. This project is a stepping stone to more complex applications, such as automated lighting systems or smart home setups. Explore and expand!