1. Required Components
- 1 x Arduino board (e.g., Arduino Uno)
- 1 x Light Dependent Resistor (LDR)
- 1 x 10kΩ Resistor (for LDR)
- 1 x LED
- 1 x 220Ω Resistor (for LED)
- Breadboard and jumper wires
- USB cable to connect Arduino to PC
2. Wiring
Follow these steps to wire your circuit:
- Connect the LED’s long leg (anode) to digital pin 9 on the Arduino.
- Connect the LED’s short leg (cathode) to GND through a 220Ω resistor.
- Connect one end of the LDR to 5V.
- Connect the other end of the LDR to analog pin A0 and to GND through a 10kΩ pull-down resistor.
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:
analogRead()
: Reads the voltage from the LDR, which changes with light intensity.digitalWrite()
: Turns the LED on or off based on the light intensity.if
condition: Compares the light intensity to a threshold to determine the LED's state.
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
- Connect your Arduino to your PC using a USB cable.
- Open the Arduino IDE and paste the code above into a new sketch.
- Select your Arduino board and the correct port in the IDE.
- Click the "Upload" button to program your Arduino.
- Observe the LED: It should turn on when the light level drops below the threshold and turn off in brighter conditions.
6. Additional Tips
- Adjusting the Threshold: Change the threshold value (
500
) in the code to suit your lighting conditions. - Serial Monitor Debugging: Use the serial monitor to view real-time light intensity values for fine-tuning.
- Expand the Project: Add more LEDs, control other devices, or use a relay to manage larger lights.
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!