Arduino LDR Interface: Light Sensor Wiring & Code Guide

LDR (Light Dependent Resistor)
An LDR (Light Dependent Resistor) is a light-sensitive device whose resistance decreases as the light intensity increases. It is commonly used to detect light levels in applications such as automatic lighting systems, light meters, and security systems. By connecting an LDR to an Arduino's analog input, you can measure light intensity and trigger specific actions based on the light levels.
Arrangement
1. LDR Pin 1 (Connected to 5V) 2. LDR Pin 2 (Connected to Analog Pin with a Resistor to GND)
Types of LDRs
Cadmium Sulfide (CdS) LDR
- Sensitive to visible light between 400-700 nm.
- Widely used for general light detection applications.
- Affordable and ideal for consumer electronics.
InGaAs Photocell LDR
- Operates in the near-infrared spectrum (800-2600 nm).
- Used in specialized applications like optical communication.
- More accurate but expensive compared to CdS LDRs.
Circuit Connections
Pin Configuration of LDR
LDR with Voltage Divider
- VCC: Connect one terminal of the LDR to 5V on Arduino.
- GND: Connect a resistor (e.g., 10kΩ) from the other LDR terminal to Arduino ground (GND).
- Analog Pin: Connect the junction of the LDR and resistor to an analog input pin on Arduino.
- This configuration forms a voltage divider circuit to measure light intensity.
LDR with Digital Input
- VCC: Connect one terminal of the LDR to 5V on Arduino.
- GND: Connect the other terminal to GND through a pull-down resistor (e.g., 10kΩ).
- Digital Pin: Connect the junction of the LDR and resistor to a digital input pin.
- Use this setup to detect light presence (HIGH/LOW) instead of precise intensity.
How Does an LDR Work
An LDR (Light Dependent Resistor) changes its resistance based on the intensity of light falling on it. In bright light, the resistance decreases, allowing more current to flow. In darkness, the resistance increases, reducing the current flow. When connected to an Arduino using a voltage divider circuit, the analog input pin reads voltage changes corresponding to light levels. The Arduino converts this analog value (0-1023) into a light intensity measurement, which can trigger actions such as turning on an LED or activating a relay when the light drops below a set threshold.
Applications of LDRs
Algorithm
Define the Pins
- Identify the analog input pin for the LDR (e.g., A0).
- Connect the LDR and resistor to form a voltage divider.
Initialize the Arduino
- Set up the serial communication in the setup() function.
- Configure the analog pin to read LDR values.
Read Light Intensity
- Use analogRead() to capture the voltage from the LDR.
- Map the analog value (0-1023) to a light intensity range.
- Display the light intensity on the Serial Monitor.
Trigger Actions
- Set a light threshold value (e.g., 500 for low light).
- Turn on an LED or activate a relay when the light is below the threshold.
Repeat the Process
- Continuously monitor light intensity and respond to changes.
- Adjust thresholds for specific applications.
Arduino Code
1// Arduino LDR Interface: Light Sensor Wiring & Code Guide
2// This code reads light intensity using an LDR and prints it to the Serial Monitor
3
4// Connect one end of the LDR to 5V
5// Connect the other end to analog pin A0 AND to GND via a 10kΩ resistor (voltage divider)
6
7// Define the analog pin connected to the LDR
8#define LDR_PIN A0
9
10void setup() {
11 // Initialize serial communication to display readings on Serial Monitor
12 Serial.begin(9600);
13}
14
15void loop() {
16 // Read the analog value from the LDR
17 int ldrValue = analogRead(LDR_PIN);
18
19 // Print the light intensity value (range: 0 to 1023)
20 Serial.print("LDR Value: ");
21 Serial.println(ldrValue);
22
23 // You can use this value to trigger actions
24 // For example, if it's too dark, turn on a light
25 /*
26 if (ldrValue < 300) {
27 // It's dark
28 Serial.println("It's dark - you can turn on an LED here.");
29 }
30 */
31
32 delay(500); // Wait half a second before taking the next reading
33}
34
Components Required
- Automatic Street Lights
- Light-Controlled Alarms
- Solar Tracking Systems
- Photographic Light Meters
- Home Automation
- Security Systems
Conclusion
Interfacing an LDR with Arduino enables light-based automation, such as controlling devices in response to changing light levels. By forming a voltage divider and reading analog inputs, the Arduino detects varying light intensities. This system is useful for applications like automatic lighting, solar tracking, and security. With the ability to adjust sensitivity through resistor selection and threshold settings, the Arduino-LDR combination offers a versatile solution for light-dependent projects.