How to operate LDR with Arduino: 2 Different Inputs

Step-by-step guide on calibrating LDR with Arduino using two different input methods for precise light measurement and enhanced project accuracy.How to operate LDR using Arduino UnoA light-dependent resistor (LDR) is the light-sensitive device most frequently used to measure the intensity of light. Light-dependent resistors are also known as photoresistors whose resistance increases as the intensity of light decreases which helps to identify the presence and absence of light.

If the resistance is high that means it’s dark (less light) but if some light falls on LDR then this resistance reduces rapidly which describes that there is enough brightness (more light). Hence photoresistors are used as the light detector in applications such as Automatic Street lighting systems, security systems, light intensity meters, burglar alarm systems, and camera shutter control.

I have made two advanced-level projects using LDR which are a How To Build a Smart Plant Watering System Using Arduino Uno and a Upgrade Your Locker Security with Arduino Uno. Check them out and explore.

In this article I will explain to you, how can you use a light-dependent resistor with Arduino Uno using digital and analog inputs. In the first example, I will explain to you how an LED can be operated with the help of the light-dependent resistor with the help of analog input pins.

In the second example, I will explain to you the same project with digital input pins. I will use the same circuit connection (except for the inputs) for both examples which will help you to understand the concept in a very easy way.

How to operate LED using LDR with Arduino Uno (using analog input)

LDR is used to sense the intensity of the light. In this example, I will try to turn on the LED whenever LDR senses darkness (low value of LDR). The LED will turn off whenever LDR senses enough brightness (high value of LDR).

I have compared the real-time measured LDR value with the threshold value ( I used 300 as the threshold value it can be changed according to your need).

If the measured LDR value is less than or equal to the threshold value that means it’s dark so the LED should turn on. if the measured LDR value is greater than the threshold value that means it’s bright so the LED should turn off. 

Required components

Connection diagram

How to operate LED using LDR with Arduino Uno (using analog input)

Circuit description

In this example, I have used the A2 analog pin of the Arduino Uno board to provide input. LDR has no polarities so it can be connected in any direction. As shown in the connection diagram I have connected the 3.3-volt power supply pin of the Arduino Uno board with one pin of the LDR.

Another pin of the LDR is connected with one terminal of 10k ohm resistor and analog input pin A2 of the Arduino Uno board. The other terminal of the 10 k ohm resistor is connected to the ground pin of the Arduino Uno. 

10 k ohm resistor is used along with LDR to make a voltage divider circuit. As the light intensity increases, the resistance of LDR decreases which allows the high voltage to pass through LDR to the analog pin, which as a result achieved a higher LDR value.

LDR gives an analog voltage as an output voltage at analog pin A2 which may be any value between o to 5 volts which is converted into a digital value between 0 to 1023 with the help of the inbuilt analog-to-digital converter of the Arduino Uno board. Hence the analog pin A2 reads the value between 0 to 1023.

LED

LED has two legs one is shorter and the other one is longer. The longer leg is known as the anode (positive leg) and the shorter leg is known as the cathode (negative leg). As shown in the connection diagram the shorter leg is connected to the ground and the longer leg is connected with a 220-ohm resistor.

If we place the LED directly to the Arduino board then an unrestricted current may burn the LED so to avoid this, it’s better to connect a series resistor with an LED. Therefore I have used a 220-ohm resistor in series with the LED. The other end of the 220-ohm resistor is connected to pin 5 of the Arduino Uno board as an output pin.

Serial monitor output

Here I am providing screenshots of the serial monitor which shows the two types of output. If the LDR value is less than or equal to 300 that means it’s dark and if the LDR value is greater than 300 that means it is bright. The serial monitor displays information according to the code and real-time values observed by the LDR.

serial monitor output
IMG 20190124 170026 HDR How to operate LDR with Arduino: 2 Different Inputs

Code

/*
*How to operate LDR using Arduino uno (with analog input)
*This code discribes that how an Light dependent resistor can be operated with analog input using Arduino Uno.
*
*Copyright (C) 2007 Free Software Foundation, Inc. <[email protected]>
*
*for more detail please visit:https://www.arduinounomagic.com/2019/01/how-to-operate-ldr-using-arduino-uno.html
*
*for more projects based on Arduino uno please visit: https://arduinounomagic.com/
*/



#define LDR A2 //Light dependent resisotr (LDR) connected to analog pin A2 
#define LED 5 //LED connected to digital pin 5
int LDR_value; //variable to store result value read by LDR

void setup()
{
Serial.begin(9600); //intialize the serial port for communicaion
pinMode(LED, OUTPUT); // set LED as output
pinMode(LDR, INPUT); //set light dependent resistor as input

}

void loop()
{

 LDR_value=analogRead(LDR); //read the data from analog pin A2 and store in LDR_value

if (LDR_value<=300) //if LDR_value value is less than or equal to 300 that means its dark outside so LED should be turn on

{
   digitalWrite(LED, HIGH);//turn on the LED
   
  
  Serial.println("its dark turn on the LED");// display "its dark turn on the LED" on serial monitor and set the cursor to the new line
  Serial.println("LDR value:"); //display "LDR value:" on serial monitor and set the cursor to the new line
  Serial.println(LDR_value); // print the value of sensor
 
}
else //if LDR_value value is greater than 300 that means its bright outside so LED should be turn off
{
    digitalWrite(LED, LOW); //turn off the LED
  
 
    Serial.println("its bright turn off the LED"); // display "its bright turn off the LED" on serial monitor and set the cursor to the new line
    Serial.println("LDR value:"); //display "LDR value:" on serial monitor and set the cursor to the new line
    Serial.println(LDR_value); // print the value of sensor
}
delay(200); //delay

}

Output

Check out the output video which will help you to understand the working of the code according to serial monitor displayed values.

How to operate LDR using Arduino (with analog input)

How to operate LED using LDR with Arduino Uno (using digital input)

In this example, I will try to turn on the LED whenever LDR senses darkness (0 value of LDR). The LED will turn off whenever LDR senses enough brightness (1 value of LDR). I have compared the real-time measured LDR value with the threshold value ( I used LOW as a threshold value).

If the measured LDR value is equal to the threshold value that means it’s dark so the LED should turn on. if the measured LDR value is not equal to the threshold value that means it’s bright so the LED should turn off.

Required components

Connection diagram

How to operate LED using LDR with Arduino Uno (using digital input)

Circuit description

In this example, I have used digital pin 12 of the Arduino Uno as the input pin. The one terminal of LDR is connected to a 3.3-volt power supply pin of the Arduino Uno board. The other terminal of LDR is connected with digital pin 12 and 10 k ohm resistor’s leg.

The other terminal of the 10 k ohm resistor is connected to the ground pin of the Arduino Uno. The LED’s shorter leg is connected to the ground and the other longer leg is connected to the 220-ohm resistor. The other terminal of the 220-ohm resistor is connected to pin 5 of the Arduino Uno board.

I have tried to use a similar connection circuit for both examples to make things easier for you to understand. In the second example’s connection circuit only the input pin 12 is used instead of analog pin A2 (pin A2 was used as the input pin in the first example) left whole connection is similar to the first example.

Serial monitor output

Here I am providing screenshots of the serial monitor which shows the output of two types. if the LDR value is 0 which means its dark and if LDR value is 1 that means it’s bright. The serial monitor displays information according to the real-time values calculated by LDR as mentioned in the code.

How to operate LED using LDR with Arduino Uno (using digital input) serial output
How to operate LED using LDR with Arduino Uno (using digital input) serial output

Code


/*
*How to operate LDR using Arduino uno (with digital input)
*This code discribes that how an Light dependent resistor can be operated with digital input using Arduino Uno.
*
*for more detail please visit:https://www.arduinounomagic.com/2019/01/how-to-operate-ldr-using-arduino-uno.html
*
*Copyright (C) 2007 Free Software Foundation, Inc. <[email protected]>
*for more projects based on Arduino uno please visit: https://arduinounomagic.com/
*/


#define LDR 12 //Light dependent resistor connected to digital pin 12
#define LED 5 //LED connected to digital pin 5
int LDR_value; //variable to store result value read by LDR

void setup()
{
Serial.begin(9600); //intialize the serial port for communicaion
pinMode(LED, OUTPUT); // set LED as output
pinMode(LDR, INPUT); //set light dependent resistor as input

}

void loop()
{
 
 LDR_value=digitalRead(LDR); // Read the data from digital pin 12 and store in LDR_value



if (LDR_value==LOW) //if LDR_value value is low that means its dark outside so LED should be turn on
{
   digitalWrite(LED, HIGH);//turn on the LED
   
  
  Serial.println("its dark turn on the LED");// display "its dark turn on the LED" on serial monitor and set the cursor to the new line
  Serial.println("LDR value:");//display "LDR value:" on serial monitor and set the cursor to the new line
  Serial.println(LDR_value);// print the value of sensor
 
}
else //if LDR_value value is high that means its bright outside so LED should be turn off
{
   digitalWrite(LED, LOW);//turn off the LED
   Serial.println("its bright turn off the LED");// display "its bright turn off the LED" on serial monitor and set the cursor to the new line
   Serial.println("LDR value:");//display "LDR value:" on serial monitor and set the cursor to the new line
    Serial.println(LDR_value);// print the value of sensor
}
delay(200);// delay

}

Output

check out the video, it will help you to understand the output in real-time.

How to operate LDR with Arduino (using digital input)


Hope you like the article.

Leave a Reply

Your email address will not be published. Required fields are marked *