An Arduino-based photoresistor system that automatically toggles an LED based on ambient light levels, serving as a prototype for automatic night lights or darkness-activated devices.
This project uses an LDR (Light Dependent Resistor) to detect ambient light levels and automatically control an LED. When darkness is detected (below threshold), the LED turns on; when sufficient light is present, the LED turns off. The system continuously monitors light levels and outputs readings via serial communication for calibration and debugging.
- Automatic light detection using photoresistor (LDR)
- Threshold-based LED control with configurable sensitivity
- Real-time serial monitoring of light levels (0-1023 ADC range)
- Simple voltage divider circuit for analog reading
- Low-latency response with minimal code overhead
- Ternary operator logic for efficient control flow
- Arduino Uno
- LDR (Light Dependent Resistor / Photoresistor)
- LED (any color)
- 10kΩ Resistor (for voltage divider with LDR)
- 220Ω Resistor (for LED current limiting)
- Breadboard
- Jumper wires
5V ----[LDR]----+----[10kΩ]---- GND
|
A0 (analog read)
D2 ----[220Ω]----[LED]---- GND
| Component | Arduino Pin |
|---|---|
| LED (Anode via 220Ω) | D2 |
| LDR (via voltage divider) | A0 |
-
LDR Setup:
- Connect one leg of LDR to 5V
- Connect other leg to both A0 and one leg of 10kΩ resistor
- Connect other leg of 10kΩ resistor to GND
-
LED Setup:
- Connect D2 to 220Ω resistor
- Connect resistor to LED anode (longer leg)
- Connect LED cathode (shorter leg) to GND
The LDR forms a voltage divider with the 10kΩ resistor. As light intensity changes, the LDR's resistance changes, producing different voltage levels at A0:
- Dark environment: LDR resistance ↑ → Voltage at A0 ↓ → Low ADC value (~109)
- Bright environment: LDR resistance ↓ → Voltage at A0 ↑ → High ADC value (~800)
valueLDR < 300 ? digitalWrite(2, HIGH) : digitalWrite(2, LOW);- If ADC reading < 300 → Dark → LED ON
- If ADC reading ≥ 300 → Light → LED OFF
The threshold value (300) is environment-specific and should be adjusted based on your lighting conditions:
- Upload code to Arduino
- Open Serial Monitor (9600 baud)
- Observe ADC values in different lighting conditions:
- Note the value in darkness
- Note the value in normal light
- Set threshold between these two values in code
- Re-upload and test
Example readings:
- Darkness: ~109
- Normal indoor light: ~800
- Suggested threshold: 300-500 (adjust as needed)
- Clone this repository:
git clone https://github.com/yourusername/ldr.git
cd ldr-
Open
ldr.inoin Arduino IDE -
Connect hardware according to circuit diagram
-
Upload to Arduino Uno
-
Open Serial Monitor (Tools → Serial Monitor, 9600 baud) to view readings
- Power on the Arduino
- The system will automatically:
- Read ambient light levels every loop cycle
- Output ADC value to Serial Monitor
- Turn LED on when dark (< threshold)
- Turn LED off when bright (≥ threshold)
- Cover the LDR to simulate darkness
- Shine light on LDR to simulate daylight
setup(): Initializes serial communication at 9600 baud and configures LED pin as outputloop(): Continuously reads LDR value, outputs to serial, and controls LED based on threshold- Uses ternary operator for compact conditional logic
- ADC Resolution: 10-bit (0-1023 range)
- Sampling Rate: Limited only by Serial.println() and loop overhead (~1ms per cycle)
- Voltage Divider: R1 (LDR) and R2 (10kΩ) create variable voltage at A0
- Pull-down Resistor: 10kΩ resistor prevents floating pin when LDR resistance is very high
Change 300 to your calibrated value:
valueLDR < YOUR_VALUE ? digitalWrite(2, HIGH) : digitalWrite(2, LOW);if (valueLDR < 280) digitalWrite(2, HIGH);
if (valueLDR > 320) digitalWrite(2, LOW);valueLDR < 300 ? (digitalWrite(2, HIGH), digitalWrite(3, HIGH))
: (digitalWrite(2, LOW), digitalWrite(3, LOW));- Automatic night lights
- Darkness-activated security systems
- Photography light meters
- Solar panel sun tracking
- Greenhouse automation
- Energy-efficient lighting systems
LED always on/off:
- Check threshold value via Serial Monitor
- Verify LDR is not damaged (measure resistance with multimeter)
- Ensure voltage divider is wired correctly
Erratic readings:
- Add small capacitor (0.1µF) across LDR for noise filtering
- Keep wires short to reduce interference
- Shield LDR from direct LED light (prevent feedback loop)
Serial Monitor shows no data:
- Verify baud rate is set to 9600
- Check USB connection
- Ensure Serial.begin(9600) is in setup()
- Add EEPROM storage for threshold values
- Implement smoothing/averaging for stable readings
- Add PWM control for gradual dimming
- Include RTC module for time-based overrides
- Multi-zone control with multiple LDRs
MIT License - feel free to modify and use for your projects.
Built as a learning project for Arduino analog sensing and automatic control systems.
