Menu Close

Arduino – LDR (Light-Dependent Resistor)

This tutorial explains concept and how to use LDR (Light Dependent Resistor) with Arduino. An example is included in which a LED is controlled on the basis of LDR.

1. Introduction:

A step by step illustrated tutorial to use LDR on Arduino.

This tutorial is made on Original Arduino UNO and Robo India’s R-Board(UNO compatible).

1.1 LDR:

A LDR (Light Dependent Resistor) or a photo resistor is a photo conductive sensor. It is a variable resistor and changes its resistance in a proportion to the light exposed to it. It’s resistance decreases with the intensity of light.

2. Required Hardware

Following Hardware will be required to perform this LDR circuit.

S.No.ItemQuantity
1.R-Board with FTDI or Arduino Board1
2.Bread Board1
3.Male to male Jumpers 7
4.Indicator LED1
5.100 OHM Resistance&10 K Resistance11
6.LDR1

3. Building Circuit

Make following circuit with the help of above mentioned components. Some key points to understand about the circuit-

LDR is connected to a 10 Resistance in series. +5 Voltage is applied to this arrangement. As the light intensity changes LDR value changes thus the voltage drop on LDR will change and we are going to measure that voltage change.

3.1 You may go with Robo India’s R-Board(UNO Compatible)-

Here is the schematic of this circuit-

or

3.2 You may go with original Arduino UNO Board-

Here is the schematic of this circuit-

4. Programming:

Once we are done with circuit part, here is our programme to this circuit. Every command of the following programme is explained in the comment section. A few point to consider for this sketch.

1. It reads LDR value and prints them on Serial monitor. Once you upload this programme to your Arduino board open serial monitor and observe how values are changing with the change of Light intensity.

2. The attached LED glows in analog mode according to the LDR Values.

3. There is a condition of threshold; The attached LED remains OFF for all the values below Threshold limit. You can set your own threshold limit. In this programme we have given 800 as threshold. You can set t threshold to any value between 0 and 1023.

You may download this code (Arduino Sketch) from here.

// Robo India's Tutorial on LDR.
// http://roboindia.com/


int LDR = A0;            // LDR input at A0 pin.
int LED = 3;             // LED is connected to PWM Pin 3.
int LDRReading = 0;      // to store input value of LDR
int lEDBrightness = 0;   // to store the value of LED Brightness
int threshold_val = 800; // Check your threshold and modify it.

void setup(){
  Serial.begin(9600);     // initializing serail communication.
  pinMode(LED, OUTPUT);   // Defining LED pin as output.
}

void loop(){
  LDRReading = analogRead(LDR);    // Reading LDR Input.
  Serial.println(LDRReading);      // Printing LDR input value.
  
  if (LDRReading >threshold_val){                   // Condition to make LED ON.
  lEDBrightness = map(LDRReading, 0, 1023, 0, 255); // Converting LDR to LED Brightness.
  analogWrite(LED, lEDBrightness); // Writing Brightness to LED.
  }
  else{
  analogWrite(LED, 0);             // If LDR is below threshold make LED OFF.
  }
    
  delay(300);                      // delay to make output readable on serial monitor. 
}

5. output:

Here is the output of this tutorial-

6. Serial Output:

This sketch gives LDR Readings at serial monitor. Here is the Screenshot of the serial output.

If you have any query please write us at info@roboindia.com

Thanks and Regards
Content Development Team 
Robo India
http://roboindia.com

Leave a Reply