LDR Sensor Working Principle, Circuit and Code
Learn how an LDR detects light, how its resistance changes, how to connect it with Arduino, and how to build a simple automatic light project.
What Is an LDR Sensor?
LDR means Light Dependent Resistor. It is a simple light-sensitive component whose resistance changes according to the amount of light falling on it.
In normal use, an LDR has higher resistance in darkness and lower resistance when more light reaches its surface. This makes it useful when an Arduino project needs to detect whether an environment is bright or dark.
How Does an LDR Work?
The basic idea is simple: light changes the electrical resistance of the photoresistive material inside the component. As light increases, resistance generally falls. As light decreases, resistance rises.
Arduino does not directly measure the LDR's resistance. Instead, the LDR is normally combined with another resistor to create a voltage divider. The changing resistance then produces a changing voltage that an analog input can read.
LDR in Light vs Darkness
| Condition | Typical LDR Behavior |
|---|---|
| Bright light | Resistance decreases |
| Normal room light | Intermediate resistance |
| Low light | Resistance increases |
| Darkness | Resistance can become very high |
Remember: LDRs are excellent for detecting relative changes in light, but a basic LDR circuit should not be treated as a precision lux meter.
Why Do We Need a Resistor?
The Arduino analog input reads voltage rather than resistance. A fixed resistor and LDR are therefore connected as a voltage divider.
When the LDR resistance changes, the voltage at the A0 connection changes. Arduino can then convert that analog voltage into a numerical reading.
Components Required
| Component | Quantity |
|---|---|
| Arduino Uno | 1 |
| LDR / Photoresistor | 1 |
| 10KΩ resistor | 1 |
| Breadboard | 1 |
| Jumper wires | Several |
LDR Arduino Wiring
Connect one side of the LDR to 5V. Connect its other side to A0. Then connect the 10KΩ resistor between A0 and GND.
The LDR is not polarized, so its two legs can be inserted either way. What matters is the complete voltage-divider arrangement.
Arduino LDR Code
This simple sketch reads the sensor and displays the value in the Serial Monitor.
const int ldrPin = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
int lightValue = analogRead(ldrPin);
Serial.print("LDR Value: ");
Serial.println(lightValue);
delay(200);
}
Upload the sketch and open the Serial Monitor at 9600 baud. Shine a light on the LDR and then cover it with your hand. The reading should change.
What Does analogRead() Do?
analogRead() reads the voltage on an analog input and converts it into a number that the program can use. On the Arduino Uno, the analog input uses a 10-bit ADC, giving readings from 0 to 1023.
The exact LDR values depend on the sensor, resistor, wiring, supply voltage and surrounding lighting. That is why copying a threshold from another project may not work perfectly on your circuit.
Automatic Night Light With LDR
Once the sensor is working, you can use its reading to control an LED. The following example turns the LED on when the measured value falls below a chosen threshold.
const int ldrPin = A0;
const int ledPin = 13;
int threshold = 500;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
int lightValue = analogRead(ldrPin);
Serial.println(lightValue);
if (lightValue < threshold) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
delay(100);
}
Important: 500 is only an example threshold. Check your own Serial Monitor readings and adjust the value according to your circuit.
How to Calibrate Your LDR
Calibration is easy. First record the value when the LDR is in bright light. Then record the value when it is dark. Choose a threshold between those readings and test the circuit several times.
For example, if your circuit gives a reading around 850 in bright conditions and 250 in darkness, a value somewhere between them could be used as a starting point.
Common LDR Problems
Reading Does Not Change
Check the connection between the LDR, 10KΩ resistor and A0. Make sure the A0 wire is connected to the voltage-divider junction.
Reading Goes the Opposite Way
If the LDR and fixed resistor positions are reversed, the voltage behavior can also reverse. The circuit can still work; adjust your program accordingly.
Reading Jumps
Room lighting can naturally fluctuate. If you need a steadier decision, average several readings in software instead of reacting to a single measurement.
LED Never Turns On
Check the threshold. Do not assume that 500 will work for every LDR circuit.
LDR Applications
- Automatic night lights
- Street-light prototypes
- Day and night detection
- Light-sensitive alarms
- Garden lighting projects
- Basic automation
- Arduino learning projects
- Brightness detection
LDR vs Digital Light Sensor
| LDR | Digital Light Sensor |
|---|---|
| Simple and inexpensive | Usually more feature-rich |
| Excellent for relative light detection | Better for measured light data |
| Uses a voltage-divider circuit | Often communicates digitally |
| Great for beginner projects | Useful for more controlled measurements |
Safety Tips
- Use a suitable resistor with an LED.
- Check wiring before powering the Arduino.
- Do not connect mains electricity directly to a breadboard or Arduino.
- Use properly rated and safely isolated hardware for real high-voltage lighting systems.
- Remember that an LDR is normally a relative light sensor, not a precision lux instrument.
Frequently Asked Questions
What does LDR stand for?
LDR stands for Light Dependent Resistor. It is also called a photoresistor or photocell.
Does LDR resistance increase or decrease with light?
For a typical LDR, resistance decreases when more light reaches the sensor and increases as the environment becomes darker.
Why use a 10KΩ resistor?
The resistor forms a voltage divider with the LDR, allowing Arduino to detect the LDR's resistance change as a voltage change.
Can Arduino directly measure LDR resistance?
No. The analog input measures voltage. The voltage-divider circuit converts the resistance change into a measurable voltage.
Can an LDR measure exact lux?
A basic LDR circuit is better suited to relative brightness detection. Accurate light measurement generally requires a dedicated light sensor.
Which Arduino pin is used for the LDR?
On an Arduino Uno, an analog input such as A0 can be used to read the voltage from the LDR divider.
Conclusion
An LDR is one of the easiest sensors to start with because its concept is simple but teaches an important electronics principle. Light changes the LDR's resistance, the voltage divider converts that change into a voltage, and Arduino reads the voltage through an analog input.
After learning this basic circuit, you can build automatic night lights, light alarms, day/night detectors and many other Arduino automation projects.
Technical References
Technical concepts were checked against Arduino documentation and educational photoresistor/voltage-divider references.

