# Light Dependent Resistor (LDR)

## What is LDR
Light Dependent Resistor (LDR) is a resistor component whose resistance changes with light intensity. The stronger the light, the smaller the resistance.
### LDR Features
- **Variable Resistance**: Changes with light intensity
- **Strong Light**: Small resistance (several KΩ)
- **Weak Light**: Large resistance (several MΩ)
- **Slow Response**: Not suitable for rapidly changing light detection
## Working Principle
LDR resistance changes with light intensity:
- **Strong Light**: Small resistance (several KΩ)
- **Weak Light**: Large resistance (several MΩ)
### Why Does Resistance Change?
LDR contains photosensitive material inside. When light shines, carriers are generated, conductivity increases, resistance decreases.
## Connection Method
### Light-Controlled LED Circuit (Direct Visual Effect)

### Connection Instructions
According to the diagram above, connection order is:
```
VCC (5V) → LDR → Fixed Resistor (10KΩ) → GND (Voltage Divider Circuit)
LDR and Fixed Resistor Connection Point (Divider Point) → LED Anode → 220Ω Resistor → GND
```
**Working Principle**:
- **Strong Light**: LDR resistance small, after voltage division LED anode voltage high, LED bright
- **Weak Light**: LDR resistance large, after voltage division LED anode voltage low, LED dim
- Can observe LED brightness changes by blocking light
**Effect**: Block LDR with hand, LED dims; remove hand, LED brightens, visually see LDR resistance change effect
### Fixed Resistor Selection
- **10KΩ**: Most common, suitable for most LDRs
- **1KΩ**: Use when light is strong
- **100KΩ**: Use when light is weak
## Arduino Example Code
### Basic Example: Read Light Intensity
```cpp
const int ldrPin = A0; // LDR connected to A0
void setup() {
Serial.begin(9600);
pinMode(ldrPin, INPUT);
}
void loop() {
int value = analogRead(ldrPin); // Read 0-1023
Serial.print("Light Intensity: ");
Serial.println(value);
delay(100);
}
```
### Connection Method
- **VCC (5V)** → **LDR** → **Fixed Resistor (10KΩ)** → **GND** (Voltage Divider Circuit)
- **LDR and Fixed Resistor Connection Point (Divider Point)** → **Arduino A0**
### Code Explanation
- `analogRead(pin)`: Read analog value, range 0-1023
- Larger value indicates stronger light
- Can convert to other ranges using `map()` function
### Advanced Example: Light-Controlled LED
```cpp
const int ldrPin = A0;
const int ledPin = 9; // PWM pin
void setup() {
pinMode(ldrPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int lightValue = analogRead(ldrPin);
// LED bright when light is dim, LED dim when light is bright
int brightness = map(lightValue, 0, 1023, 255, 0);
brightness = constrain(brightness, 0, 255);
analogWrite(ledPin, brightness);
delay(10);
}
```
**Explanation**: Automatically adjust LED brightness according to light intensity, LED brighter when light is dim.
## Common Questions and Answers
### Q1: LDR Reading Unstable?
- Check if connection has good contact
- Add software filtering (read multiple times and average)
- Check if fixed resistor value is appropriate
### Q2: How to Choose Fixed Resistor Value?
- Usually use 10KΩ
- If reading always close to 0 or 1023, adjust resistor value
- Use large resistor when light is strong, small resistor when light is weak
### Q3: LDR Response Slow?
- LDR itself responds slowly, this is normal
- If fast response needed, consider using photodiode or phototransistor
## Safety Precautions
⚠️ **Important Notes**:
- Need to form voltage divider circuit with resistor
- Response speed is slow, not suitable for rapidly changing light detection
- Different models have different resistance ranges, need to select divider resistor according to actual situation
## Practical Applications
LDRs are commonly used for auto dimming, light-controlled switches, light monitoring, etc. In Arduino projects, commonly used to create auto-dimming LEDs, light-controlled alarms, light monitoring systems, etc.