Arduino LDR Light Sensor LED Control Experiment

Arduino LDR Light Sensor LED Control Experiment

Introduction

This experiment demonstrates how to control an LED using an LDR (Light Dependent Resistor). The LED will turn on when the surrounding light level falls below a certain threshold and turn off when it is bright enough.

Components Needed

Circuit Setup

  1. Connect one end of the LDR to 5V and the other end to analog pin A0.
  2. Place a 10kΩ resistor between the LDR's ground end and GND to create a voltage divider.
  3. Connect the anode (longer leg) of the LED to digital pin 9 through a 220Ω resistor.
  4. Connect the cathode (shorter leg) of the LED to the GND pin on the Arduino.

Code for LDR Light Sensor LED Control

Upload the following code to your Arduino to control the LED based on the light level sensed by the LDR:


// Define pins
const int ldrPin = A0; // LDR analog input
const int ledPin = 9;  // LED digital output

// Define threshold
const int threshold = 500; // Adjust based on ambient light level

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

void loop() {
    // Read the LDR value (0 - 1023)
    int ldrValue = analogRead(ldrPin);

    // Print LDR value for calibration
    Serial.println(ldrValue);

    // Control LED based on light threshold
    if (ldrValue < threshold) {
        digitalWrite(ledPin, HIGH); // Turn on LED in darkness
    } else {
        digitalWrite(ledPin, LOW); // Turn off LED in bright light
    }

    delay(100); // Small delay for stable readings
}
            

Explanation

The LDR creates a variable voltage based on the ambient light level, which the Arduino reads as an analog input. Here’s how it works:

Tip: Use the Serial Monitor to adjust the threshold value based on your ambient light conditions.

Troubleshooting