design a night lamp using a ldr and a led using arduino uno and find out the time delays associated in keeping on the lamp.
design a night lamp using a ldr and a led using arduino uno and find out the time delays associated in keeping on the lamp.
Answer: Designing a night lamp using an LDR (Light Dependent Resistor) and an LED with an Arduino Uno is a straightforward project. The LDR senses ambient light levels, and when it gets dark, the LED will turn on automatically. You can also add a time delay to control how long the LED stays on after it gets dark. Here’s a step-by-step guide to creating this project:
Components Needed:
- Arduino Uno
- LDR (Light Dependent Resistor)
- LED (any color)
- 220-330-ohm resistor
- Breadboard and jumper wires
- USB cable for Arduino
- Power source for Arduino (battery or USB connection to a computer)
Circuit Connections:
- Connect one leg of the LDR to the 5V pin on the Arduino.
- Connect the other leg of the LDR to one end of a resistor (220-330 ohms).
- Connect the other end of the resistor to one leg of the LED (longer lead, the positive side).
- Connect the other leg of the LED (shorter lead, the negative side) to the GND pin on the Arduino.
- Connect the junction between the LDR and the resistor to analog pin A0 on the Arduino.
Arduino Code:
const int LDRPin = A0; // LDR connected to analog pin A0
const int LEDPin = 13; // Built-in LED on pin 13
int threshold = 500; // Adjust this value to set the light threshold
int delayTime = 5000; // Delay time in milliseconds (5 seconds)
void setup() {
pinMode(LEDPin, OUTPUT);
}
void loop() {
int lightLevel = analogRead(LDRPin);
if (lightLevel < threshold) {
digitalWrite(LEDPin, HIGH); // Turn on the LED
delay(delayTime); // Keep the LED on for the specified delay
digitalWrite(LEDPin, LOW); // Turn off the LED
}
}
Explanation:
- The LDR is connected to analog pin A0 to read the light level.
- The code checks the light level using
analogRead()
and compares it to thethreshold
. If the light level falls below the threshold, it turns on the LED. - The LED remains on for the specified
delayTime
(in this example, 5 seconds) and then turns off.
Adjustments:
- You can change the
threshold
value to set the level of darkness at which the LED turns on. - Modify the
delayTime
to control how long the LED stays on after it gets dark. - Experiment with these values to suit your preferences.
Upload the code to your Arduino Uno, and you should have a night lamp that turns on when it gets dark and stays on for the specified time delay.