Introduction
This experiment demonstrates how to control an LED using an LDR (Light Dependent Resistor). The LED will turn on when the surrounding light level falls below a certain threshold and turn off when it is bright enough.
Components Needed
- Arduino (e.g., Uno, Nano)
- LDR (Light Dependent Resistor)
- 10kΩ resistor
- LED
- 220Ω resistor (for LED)
- Breadboard and jumper wires
Circuit Setup
- Connect one end of the LDR to 5V and the other end to analog pin A0.
- Place a 10kΩ resistor between the LDR's ground end and GND to create a voltage divider.
- Connect the anode (longer leg) of the LED to digital pin 9 through a 220Ω resistor.
- Connect the cathode (shorter leg) of the LED to the GND pin on the Arduino.
Code for LDR Light Sensor LED Control
Upload the following code to your Arduino to control the LED based on the light level sensed by the LDR:
// Define pins
const int ldrPin = A0; // LDR analog input
const int ledPin = 9; // LED digital output
// Define threshold
const int threshold = 500; // Adjust based on ambient light level
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// Read the LDR value (0 - 1023)
int ldrValue = analogRead(ldrPin);
// Print LDR value for calibration
Serial.println(ldrValue);
// Control LED based on light threshold
if (ldrValue < threshold) {
digitalWrite(ledPin, HIGH); // Turn on LED in darkness
} else {
digitalWrite(ledPin, LOW); // Turn off LED in bright light
}
delay(100); // Small delay for stable readings
}
Explanation
The LDR creates a variable voltage based on the ambient light level, which the Arduino reads as an analog input. Here’s how it works:
- The LDR value ranges from 0 (dark) to 1023 (bright light).
- By comparing the LDR value to a
threshold
, we decide when to turn the LED on or off. - When the light level is below the threshold (darker), the LED is turned on; otherwise, it remains off.
Tip: Use the Serial Monitor to adjust the threshold value based on your ambient light conditions.
Troubleshooting
- If the LED doesn’t turn on or off as expected, check the LDR connections and ensure it’s creating a voltage divider with the 10kΩ resistor.
- Adjust the
threshold
value in the code if the LED reacts incorrectly to light levels. - If the LED remains on or off continuously, use the Serial Monitor to observe the
ldrValue
and modify the threshold accordingly.