Jump to content

Light Dependent Resistor (LDR) with Arduino Uno

From cognifront

Interfacing LDR with Arduino UNO for Automatic LED Control

Interfacing LDR with Arduino UNO for Automatic LED Control demonstrates how a Light Dependent Resistor (LDR) can be interfaced with an Arduino UNO board to detect surrounding light intensity and automatically control an LED.

An LDR is a light-sensitive electronic component whose resistance changes according to the intensity of light falling on its surface.

In this experiment, the LDR is connected to analog pin A0 of the Arduino UNO, while an LED is connected to digital pin 2.

When the surrounding light intensity decreases below a predefined threshold, the Arduino UNO turns ON the LED. When sufficient light is detected, the LED is turned OFF.

Light Intensity → LDR Sensor → Arduino UNO → LED Control

Theory

A Light Dependent Resistor (LDR) is a passive electronic component whose resistance varies according to the intensity of incident light.

The LDR works on the principle of photoconductivity.

When light falls on the LDR, the number of charge carriers inside the semiconductor material increases, resulting in a decrease in resistance.

Similarly, when the intensity of light decreases, the resistance of the LDR increases.

The resistance characteristics of an LDR are:

  • Bright Light: Low resistance.
  • Low Light: High resistance.
  • Darkness: Very high resistance.

The Arduino UNO cannot directly measure resistance. Therefore, the LDR is connected in a voltage divider circuit to generate an analog voltage.

The analog voltage is supplied to the Arduino UNO analog input pin A0.

The Arduino UNO uses the analogRead() function to read the analog value.

Light → LDR Resistance Change → Analog Voltage → Arduino UNO

LDR Sensor

An LDR generally has two terminals:

Pin Connection Function
Terminal 1 5V Supply connection
Terminal 2 Analog voltage divider point Light sensing output

Note: A fixed resistor is required to form a voltage divider circuit with the LDR.

Working Principle

The LDR changes its resistance according to the surrounding light intensity.

A voltage divider circuit converts this resistance variation into a voltage that can be measured by the Arduino UNO.

The Arduino UNO reads the analog voltage using the analogRead() function.

The Arduino UNO has a 10-bit ADC, which provides analog readings ranging from 0 to 1023.

In this experiment:

  • When the LDR reading is less than or equal to 150, the condition is considered night time and the LED turns ON.
  • When the LDR reading is greater than 150, the condition is considered daytime and the LED turns OFF.

The threshold value can be adjusted according to the surrounding light conditions and voltage divider configuration.

Applications

LDR sensors are commonly used in:

  • Automatic street lighting systems
  • Automatic garden lighting
  • Light intensity monitoring
  • Smart home automation
  • Night lamps
  • Security lighting systems
  • Energy-saving lighting systems

Connections

The LDR is connected to analog pin A0 through a voltage divider circuit.

The LED is connected to digital pin 2 of the Arduino UNO through a current-limiting resistor.

Device Pin / Terminal Arduino UNO Function
LDR Terminal 1 5V Supply connection
LDR Terminal 2 Analog voltage divider point Light sensing
10K Resistor One terminal LDR and A0 junction Voltage divider
10K Resistor Other terminal GND Ground connection
LED Anode (+) Digital Pin 2 through 220 ohm resistor LED control
LED Cathode (−) GND Ground

Voltage Divider Connection:

       5V
        |
       LDR
        |
        +---------- A0
        |
       10K
      Resistor
        |
       GND

LED Connection:

Arduino UNO D2
      |
   220 Ohm
   Resistor
      |
    LED (+)
    LED (-)
      |
     GND

Note: The above voltage divider configuration is used to obtain lower ADC readings in brighter light and higher readings in darkness. Therefore, the threshold logic in the reference program may need adjustment if the voltage divider is connected differently.

Algorithm

The following algorithm is used for automatic LED control based on light intensity:

  1. Start the Arduino IDE.
  2. Connect the Arduino UNO board to the computer using a USB cable.
  3. Declare digital pin 2 as the LED output pin.
  4. Declare analog pin A0 as the LDR input pin.
  5. Initialize serial communication at 9600 baud rate.
  6. Configure the LED pin as OUTPUT.
  7. Configure the LDR pin as INPUT.
  8. Read the analog value from the LDR using the analogRead() function.
  9. Compare the LDR reading with the threshold value of 150.
  10. If the LDR reading is less than or equal to 150, turn ON the LED.
  11. Display the message "It's Night Time, Turn on the LED" along with the LDR reading.
  12. Otherwise, turn OFF the LED.
  13. Display the message "It's Daytime, Turn off the LED" along with the LDR reading.
  14. Display a separator line in the Serial Monitor.
  15. Provide a delay of 1000 milliseconds.
  16. Repeat the process continuously.

The overall operation is:

Read LDR → Compare with Threshold → Night: LED ON / Day: LED OFF

Working

The Arduino UNO continuously monitors the surrounding light intensity using the LDR sensor.

The working sequence is:

  1. The Arduino UNO initializes serial communication and the input/output pins.
  2. The analog voltage from the LDR voltage divider is read through analog pin A0.
  3. The analog value is stored in the variable ldr.
  4. The Arduino UNO compares the reading with the predefined threshold value of 150.
  5. If the reading is less than or equal to 150, the Arduino UNO identifies the condition as night time.
  6. The LED connected to digital pin 2 is turned ON.
  7. If the reading is greater than 150, the Arduino UNO identifies the condition as daytime.
  8. The LED is turned OFF.
  9. The corresponding message and LDR reading are displayed on the Serial Monitor.
  10. A delay of 1000 milliseconds is provided before the next reading.

LDR Sensor → Analog Pin A0 → Arduino UNO → Threshold Comparison → LED

Observation

Automatic LED Control Using LDR

The following program is used to control an LED automatically based on the light intensity detected by the LDR sensor.

const int ledPin = 2;
const int ldrPin = A0;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(ldrPin, INPUT);
}

void loop() {
  // Read from LDR
  int ldr = analogRead(ldrPin);
  digitalWrite(ledPin, LOW);

  // LED Control Based on LDR
  if (ldr <= 150) { // Adjust threshold as needed
    digitalWrite(ledPin, HIGH);
    Serial.print("It's Night Time, Turn on the LED : ");
    Serial.println(ldr);
  } else {
    digitalWrite(ledPin, LOW);
    Serial.print("It's Daytime, Turn off the LED : ");
    Serial.println(ldr);
  }

  Serial.println("-----------------------------");
  delay(1000);
}

Program Explanation

  • The variable ledPin is assigned to digital pin 2.
  • The variable ldrPin is assigned to analog pin A0.
  • The Serial.begin(9600) function initializes serial communication at 9600 baud rate.
  • The pinMode() function configures the LED pin as OUTPUT and LDR pin as INPUT.
  • The analogRead(ldrPin) function reads the analog value from the LDR sensor.
  • The reading is stored in the integer variable ldr.
  • The digitalWrite(ledPin, LOW) statement initially turns OFF the LED.
  • The if condition checks whether the LDR reading is less than or equal to 150.
  • If the condition is TRUE, the LED is turned ON.
  • The message "It's Night Time, Turn on the LED" and the LDR reading are displayed on the Serial Monitor.
  • If the condition is FALSE, the LED remains OFF.
  • The message "It's Daytime, Turn off the LED" and the LDR reading are displayed on the Serial Monitor.
  • The delay(1000) function provides a delay of 1000 milliseconds before the next reading.

Observation Table

Sr. No. Light Condition LDR Reading LED Status
1 Night / Low Light Less than or equal to 150 ON
2 Daytime / Bright Light Greater than 150 OFF

Note: Actual LDR readings depend on the surrounding light intensity, LDR characteristics and the voltage divider resistor value. The threshold of 150 should be calibrated experimentally.

Serial Monitor Output

The Serial Monitor displays the LDR reading and corresponding LED status.

Example output:

It's Night Time, Turn on the LED : 120
-----------------------------
It's Night Time, Turn on the LED : 135
-----------------------------
It's Daytime, Turn off the LED : 250
-----------------------------
It's Daytime, Turn off the LED : 450
-----------------------------

Result

The Arduino UNO successfully interfaces with the LDR sensor and automatically controls an LED based on the surrounding light intensity. When the LDR reading is less than or equal to the threshold value of 150, the LED turns ON, indicating night time. When the LDR reading is greater than 150, the LED turns OFF, indicating daytime. Thus, an automatic light control system is successfully implemented using Arduino UNO and an LDR sensor.