Skip to content

atasayugras/ldr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

LDR - Automatic Light Control

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.

Demo

Overview

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.

Features

  • 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

Hardware Requirements

  • 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

Circuit Diagram

LDR Voltage Divider

5V ----[LDR]----+----[10kΩ]---- GND
                |
                A0 (analog read)

LED Connection

D2 ----[220Ω]----[LED]---- GND

Pin Configuration

Component Arduino Pin
LED (Anode via 220Ω) D2
LDR (via voltage divider) A0

Wiring Instructions

  1. 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
  2. LED Setup:

    • Connect D2 to 220Ω resistor
    • Connect resistor to LED anode (longer leg)
    • Connect LED cathode (shorter leg) to GND

How It Works

Light Level Detection

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)

Threshold Logic

valueLDR < 300 ? digitalWrite(2, HIGH) : digitalWrite(2, LOW);
  • If ADC reading < 300 → Dark → LED ON
  • If ADC reading ≥ 300 → Light → LED OFF

Calibration

The threshold value (300) is environment-specific and should be adjusted based on your lighting conditions:

  1. Upload code to Arduino
  2. Open Serial Monitor (9600 baud)
  3. Observe ADC values in different lighting conditions:
    • Note the value in darkness
    • Note the value in normal light
  4. Set threshold between these two values in code
  5. Re-upload and test

Example readings:

  • Darkness: ~109
  • Normal indoor light: ~800
  • Suggested threshold: 300-500 (adjust as needed)

Installation

  1. Clone this repository:
git clone https://github.com/yourusername/ldr.git
cd ldr
  1. Open ldr.ino in Arduino IDE

  2. Connect hardware according to circuit diagram

  3. Upload to Arduino Uno

  4. Open Serial Monitor (Tools → Serial Monitor, 9600 baud) to view readings

Usage

  1. Power on the Arduino
  2. 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)
  3. Cover the LDR to simulate darkness
  4. Shine light on LDR to simulate daylight

Code Structure

  • setup(): Initializes serial communication at 9600 baud and configures LED pin as output
  • loop(): Continuously reads LDR value, outputs to serial, and controls LED based on threshold
  • Uses ternary operator for compact conditional logic

Technical Notes

  • 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

Customization

Adjust Threshold

Change 300 to your calibrated value:

valueLDR < YOUR_VALUE ? digitalWrite(2, HIGH) : digitalWrite(2, LOW);

Add Hysteresis (Prevent Flickering)

if (valueLDR < 280) digitalWrite(2, HIGH);
if (valueLDR > 320) digitalWrite(2, LOW);

Control Multiple LEDs

valueLDR < 300 ? (digitalWrite(2, HIGH), digitalWrite(3, HIGH)) 
               : (digitalWrite(2, LOW), digitalWrite(3, LOW));

Applications

  • Automatic night lights
  • Darkness-activated security systems
  • Photography light meters
  • Solar panel sun tracking
  • Greenhouse automation
  • Energy-efficient lighting systems

Troubleshooting

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()

Future Improvements

  • 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

License

MIT License - feel free to modify and use for your projects.

Author

Built as a learning project for Arduino analog sensing and automatic control systems.

About

Arduino automatic light control using LDR photoresistor - turns LED on/off based on ambient light threshold

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages