An LDR (Light Dependent Resistor), also called a photoresistor, is one of the simplest and most affordable sensors you can use with an Arduino. Whether you want to build an automatic night lamp, a street light controller, or a security system that triggers in the dark, understanding how to use an LDR with Arduino for darkness detection is an essential skill for every electronics hobbyist in India.
In this comprehensive guide, we will walk you through everything you need to know — from the working principle of an LDR to building a complete darkness detection circuit with Arduino code and real-world applications.
What is an LDR Sensor?
An LDR (Light Dependent Resistor) is a passive electronic component whose resistance changes depending on the intensity of light falling on it. In bright light, its resistance drops to a few hundred ohms. In darkness, its resistance can rise to several megaohms. This dramatic change in resistance is what makes it useful for detecting light and darkness.
LDRs are made from semiconductor materials like cadmium sulfide (CdS) and are sensitive to visible light as well as infrared light. They are inexpensive, easy to source in India, and work perfectly with Arduino’s 5V logic.
You’ll commonly find LDRs used in:
- Automatic street lights and garden lamps
- Burglar alarm systems (beam-break detection)
- Camera exposure metering circuits
- Solar tracker systems
- Smart home automation devices
How Does an LDR Work?
The working of an LDR is based on the principle of photoconductivity. When photons of light strike the semiconductor material, they excite electrons from the valence band to the conduction band, increasing the number of free charge carriers and thus decreasing the resistance.
A typical LDR has the following characteristics:
- In bright light (1000 lux): Resistance ≈ 50–150 Ω
- In dim light (10 lux): Resistance ≈ 10–20 kΩ
- In complete darkness: Resistance ≈ 1–10 MΩ
When used in a voltage divider circuit with a fixed resistor, this changing resistance produces a varying voltage at the Arduino’s analog input — which is how we read the light level in code.
Components Needed
To build a basic LDR darkness detection circuit with Arduino, you will need:
- 1 × Arduino Uno (or Nano/Mega)
- 1 × LDR (photoresistor)
- 1 × 10kΩ resistor (for voltage divider)
- 1 × LED (optional, for visual output)
- 1 × 220Ω resistor (for LED)
- 1 × Breadboard
- Jumper wires
- USB cable
Alternatively, you can use a ready-made LDR sensor module which includes the LDR, a fixed resistor, a comparator (LM393), and both analog and digital output pins — making it even easier to interface with Arduino.
LM35 Temperature Sensors
A popular analog sensor from Zbotic. Great companion to LDR projects requiring temperature-based light adjustment and environment monitoring.
Circuit Diagram and Connections
The LDR is used in a voltage divider configuration with a 10kΩ pull-down resistor. Here is how to connect everything:
Voltage Divider Circuit (Analog Reading)
- Connect one leg of the LDR to the 5V pin of Arduino
- Connect the other leg of the LDR to the A0 analog input pin of Arduino
- Connect a 10kΩ resistor between the A0 pin and GND
- Connect an LED anode through a 220Ω resistor to digital pin D13
- Connect the LED cathode to GND
The voltage at A0 will rise when the LDR is in bright light (its resistance drops) and fall in darkness (its resistance rises). The Arduino’s ADC reads this as a value between 0 and 1023.
Using a Ready-Made LDR Module
If you are using an LDR module with a comparator:
- VCC → Arduino 5V
- GND → Arduino GND
- AO → Arduino A0 (analog value)
- DO → Arduino D7 (digital HIGH/LOW based on threshold set by onboard potentiometer)
Arduino Code for Darkness Detection
Below is a complete Arduino sketch that reads the LDR value and turns on an LED when darkness is detected. The threshold can be adjusted to suit your lighting conditions.
Basic Analog Darkness Detection Code
// LDR Darkness Detection with Arduino
// Connect LDR + 10kΩ voltage divider to A0
// LED connected to pin 13
const int ldrPin = A0; // LDR analog input
const int ledPin = 13; // LED output
const int threshold = 400; // Adjust based on your environment
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println("LDR Darkness Detector Started");
}
void loop() {
int ldrValue = analogRead(ldrPin);
Serial.print("LDR Value: ");
Serial.println(ldrValue);
if (ldrValue < threshold) {
// Dark environment detected
digitalWrite(ledPin, HIGH);
Serial.println("Status: DARK - LED ON");
} else {
// Bright environment
digitalWrite(ledPin, LOW);
Serial.println("Status: BRIGHT - LED OFF");
}
delay(500);
}
Advanced Code with Relay for Mains Lighting
// LDR Auto Light Controller with Relay
// Relay module connected to pin 8 (active LOW)
const int ldrPin = A0;
const int relayPin = 8;
int threshold = 300;
void setup() {
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH); // Relay OFF initially (active LOW)
Serial.begin(9600);
}
void loop() {
int ldrValue = analogRead(ldrPin);
// Map LDR value to percentage (0 = dark, 100 = bright)
int lightPercent = map(ldrValue, 0, 1023, 0, 100);
Serial.print("Light Level: ");
Serial.print(lightPercent);
Serial.println("%");
if (lightPercent < 30) { // Less than 30% brightness = dark
digitalWrite(relayPin, LOW); // Turn relay ON
Serial.println("RELAY ON: Light activated");
} else {
digitalWrite(relayPin, HIGH); // Turn relay OFF
Serial.println("RELAY OFF: Sufficient light");
}
delay(1000);
}
Analog vs Digital LDR Modules
When working with Arduino and LDR sensors, you have two reading approaches:
Analog Mode (A0 pin)
Provides a value from 0 to 1023 using Arduino’s built-in ADC (Analog-to-Digital Converter). This gives you precise light level measurements and lets you set custom thresholds in code. Best for applications where you need proportional control.
Digital Mode (DO pin on module)
Returns only HIGH or LOW based on a threshold set by the potentiometer on the module. Simpler to use, no need for threshold logic in code. Best for simple ON/OFF applications like automatic lights.
| Feature | Analog Mode | Digital Mode |
|---|---|---|
| Output range | 0–1023 | HIGH / LOW |
| Threshold control | In code | Potentiometer on module |
| Use case | Precision monitoring | Simple ON/OFF control |
| Pins required | Analog pin | Digital pin |
Calibrating the LDR Threshold
One of the most common challenges with LDR darkness detection is finding the right threshold value for your environment. Here’s a systematic approach:
- Run the serial monitor with the basic code and observe LDR values in various lighting conditions: full daylight, room lighting, dim light, and complete darkness.
- Note the transition point — the LDR value where you consider the environment to be “dark enough” to trigger your action.
- Add hysteresis to avoid flickering. For example, turn the light ON when value drops below 350, but only turn it OFF when the value rises above 500. This prevents rapid on-off cycling at the transition point.
// Hysteresis example
const int onThreshold = 350; // Turn ON below this
const int offThreshold = 500; // Turn OFF above this
bool lightOn = false;
void loop() {
int ldrValue = analogRead(ldrPin);
if (!lightOn && ldrValue < onThreshold) {
lightOn = true;
digitalWrite(ledPin, HIGH);
} else if (lightOn && ldrValue > offThreshold) {
lightOn = false;
digitalWrite(ledPin, LOW);
}
delay(500);
}
DHT11 Temperature & Humidity Sensor Module
Combine with your LDR project to build a complete environment monitor — track light, temperature, and humidity with Arduino simultaneously.
Real-World Project Ideas
Once you have the basics working, here are some exciting LDR-based projects you can build:
1. Automatic Street Light / Night Lamp
The classic LDR project. Connect the digital output of your LDR module to a relay that switches a mains-powered lamp. The potentiometer on the module lets you set the exact light level at which the lamp turns on. This is used extensively in home automation and DIY street lighting.
2. Alarm System with Laser Beam
Point a laser module at an LDR. When the laser beam is broken (by an intruder walking through it), the LDR reading drops suddenly. Trigger a buzzer or alert system when this happens. This is a popular project for security demonstrations.
3. Solar Tracker
Use two or four LDRs placed at angles to detect which direction has the strongest light source. Drive servo motors to tilt a solar panel towards the brightest direction, maximizing energy collection.
4. Automatic LCD Backlight Control
Read the ambient light level with an LDR and use PWM to adjust the brightness of an LCD or LED backlight — dimmer in low light, brighter in bright environments. Saves power and improves readability.
5. Smart Curtain / Blind Controller
Combine an LDR with a stepper motor to automatically open curtains when it’s bright outside and close them when it gets dark or when direct sunlight hits the sensor.
Troubleshooting Common Issues
LDR always reads maximum value
Check that your 10kΩ resistor is properly connected to GND. Without the pull-down resistor, the A0 pin will float high. Also verify the LDR leads are not oxidized or broken — LDRs can fail if bent sharply at the leads.
LDR readings are unstable or noisy
Add a small 0.1µF capacitor between A0 and GND to filter high-frequency noise. Also ensure you are averaging multiple readings in code: for (int i=0; i<10; i++) { sum += analogRead(ldrPin); } avg = sum / 10;
LED doesn’t respond to light changes
Your threshold value may be wrong for your environment. Open Serial Monitor, cover and uncover the LDR with your hand, and observe the actual values. Adjust the threshold constant in code accordingly.
LDR module’s DO pin always stays HIGH
The onboard potentiometer needs adjustment. Use a small screwdriver to turn the trimmer pot clockwise to increase sensitivity (trigger in brighter light) or counter-clockwise to decrease it (trigger only in darkness).
BMP280 Barometric Pressure Sensor Module
Add weather sensing to your LDR automation projects. Monitor pressure and altitude alongside light levels for complete environment awareness.
Frequently Asked Questions
What is the difference between LDR and photodiode?
An LDR changes its resistance based on light, making it simple and cheap to use with a voltage divider. A photodiode converts light directly into current with much faster response times, making it suitable for high-speed applications like IR communication. For most Arduino automation projects, an LDR is sufficient and more affordable.
What resistor value should I use with LDR?
A 10kΩ resistor is the standard choice for most LDR modules and works well across typical indoor and outdoor lighting conditions. If your LDR operates in very bright conditions (outdoor sunlight), a higher resistor like 47kΩ will give you better sensitivity at higher light levels.
Can I use LDR with 3.3V Arduino (like Arduino Due or ESP32)?
Yes. The LDR and voltage divider circuit works at any supply voltage. For a 3.3V system, connect the LDR to 3.3V instead of 5V, and ensure your microcontroller’s ADC reference is also 3.3V. The code logic remains the same, but the max ADC value may be 4095 (12-bit ADC on ESP32) instead of 1023.
How do I prevent false triggers from flickering lights?
Use hysteresis in your code (separate ON and OFF thresholds) and add a debounce delay — only trigger if the darkness condition persists for at least 2–3 seconds. This prevents fluorescent tube flickering or car headlights from triggering your system.
What is the maximum current an LDR can handle?
Most small LDRs (like the GL5528) are rated for a maximum of 100mA continuous current and should be operated well below this — typically just a few milliamps in a voltage divider circuit. Never connect an LDR directly to a motor or relay without appropriate current limiting.
Can I use multiple LDRs with one Arduino?
Yes. Arduino Uno has 6 analog input pins (A0–A5), so you can connect up to 6 LDRs simultaneously, each with its own 10kΩ pull-down resistor. This is useful for solar tracker projects that need to compare light levels from multiple directions.
Ready to Build Your LDR Project?
Zbotic.in has everything you need for your Arduino sensor projects, delivered fast across India. From LDR modules and DHT sensors to relay boards and complete starter kits — browse our sensor collection and get started today.
Conclusion
The LDR light sensor is one of the most versatile and beginner-friendly sensors available for Arduino projects. Its simple voltage-divider interface, analog and digital output flexibility, and extremely low cost make it ideal for home automation, security systems, and educational electronics projects in India.
By following this guide — understanding the working principle, wiring the circuit correctly, writing robust Arduino code with proper calibration and hysteresis — you can build reliable darkness detection systems that work consistently in real-world conditions.
Start with the basic LED toggle project, then work your way up to relay-controlled mains lighting or the laser beam alarm system. The skills you learn with an LDR translate directly to working with other analog sensors like soil moisture sensors, thermistors, and gas sensors.
Add comment