Arduino with LDR

LDR( light dependent resistor)

In order to detect the intensity of light or darkness, we use a sensor called an LDR (light dependent resistor). The LDR is a special type of resistor that allows higher voltages to pass through it (low resistance) whenever there is a high intensity of light, and passes a low voltage (high resistance) whenever it is dark. We can take advantage of this LDR property and use it in our DIY Arduino LDR sensor project.

How Does It Work? 

 

This system works by sensing the intensity of light in its environment. The sensor that can be used to detect light is an LDR. It’s inexpensive, and you can buy it from any local electronics store or online.

The LDR gives out an analog voltage when connected to VCC (5V), which varies in magnitude in direct proportion to the input light intensity on it. That is, the greater the intensity of light, the greater the corresponding voltage from the LDR will be. Since the LDR gives out an analog voltage, it is connected to the analog input pin on the Arduino. The Arduino, with its built-in ADC (analog-to-digital converter), then converts the analog voltage (from 0-5V) into a digital value in the range of (0-1023). When there is sufficient light in its environment or on its surface, the converted digital values read from the LDR through the Arduino will be in the range of 800-1023.

ComponentsRequired

  • Arduino UNO
  • LDR (Light Dependent Resistor)
  • Resistor (100k-1;330ohm-1)
  • LED – 1
  • Connecting wires
  • Breadboard

Circuit Diagram

First, you need to connect the LDR to the analog input pin 2 on the Arduino. You have to use a voltage divider configuration to do this. The connection diagram for the Arduino is as given below.

nxzFwam6UpisJ0BLmTEsvfwTgaEPEsdf3Ys04qcY.jpeg

One leg of the LDR is connected to VCC (5V) on the Arduino, and the other to the analog pin 2 on the Arduino. A 100K resistor is also connected to the same leg and grounded.

Testing the Code for the Arduino LDR Sensor 

After connecting the LDR to your Arduino, you can check for the values coming from the LDR via the Arduino. To do this, connect the Arduino via USB to your PC and open up the Arduino IDE or software. Next, paste this code and upload it to your Arduino:

Code

/*  LDR using Arduino Tutorial
 *  www.genbays.com 
 */
int sensorPin = A2; // select the input pin for LDR

int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
Serial.begin(9600); //sets serial port for communication
}
void loop() {
sensorValue = analogRead(sensorPin); // read the value from the sensor
Serial.println(sensorValue); //prints the values coming from the sensor on the screen

delay(100);

}

After uploading the code, click the button on the Arduino IDE called “Serial monitor”. This will open a new window, which prints different values on the screen. Now, test out the sensor by blocking its surface from light and see what values you get on the serial monitor. This is how the serial monitor looks:

XMcK9UVAobRacM2A5g7cR3dJbbjZStlKcgKUHMym.png

Arduino LDR Sensor – Serial Monitor

Connecting LED to the Arduino 

Image result for ldr using arduino

After connecting the LDR and LED to your Arduino,upload this code to your Arduino:

Code

/*  PIR sensor tester using LCD Tutorial
 *  www.genbays.com 
 */


int sensorPin = A2;   // select the input pin for ldr
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  pinMode(11, OUTPUT); //pin connected to the relay
  Serial.begin(9600); //sets serial port for communication
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);    
  Serial.println(sensorValue); //prints the values coming from the sensor on the screen
  
  if(sensorValue < 700) //setting a threshold value
  digitalWrite(11,HIGH); //turn relay ON
  
  else digitalWrite(11,LOW); //turn relay OFF
  
  delay(100);                  
}