• Dhanush kumar S

Interfacing LDR Sensor with Arduino

Updated: Aug 21, 2020

While watching the cricket match you must have heard, Bad light stopped the play. Do you know how they are determining it? Also, while going to shopping malls you would have noticed the doors will be opening automatically when you go near it. Have you ever wondered how these are happening? It's all because of the Light Dependent Sensors (LDR). They are also called as photoresistors.

This article gives you a brief explanation of how the #LDR sensor works. The output of these sensors can be viewed only with the help of #Arduino boards. The article provides step by step instructions involved in this pretty simple interfacing, starting from the list of #electronic components required.


Apparatus Required :


The list provides all the hardware components required for the #interfacing.


Hardware components:


1.LDR Sensor:

This is a cadmium-sulfide cell works based on the principle of Photoconductivity. It is basically a resistor whose resistance value changes depending upon the intensity of light. When the light falls on the surface of the senor, material conductivity varies as a result of which resistance also varies.


2.ARDUINO BOARD:

The ARDUINO UNO board can be powered from the computer by fixing appropriate cable in the USB port. It can also be powered using AC power supply with the help of Barrel Jack port. The voltage regulator present in the Arduino board resists the excessive flow of voltage to the microprocessor as it may get damaged. Here, we are using the Arduino board to connect the LDR sensor and display the output.

  • 3.3 V pin- Helps in supplying 3.3 V output voltage.

  • 5 V pin- Helps in supplying 5 V output voltage.

  • GND pin- Helps to ground the circuit.

  • VIN pin- To connect the AC power supply.



3.Jumper wires:

These are simple connecting wires, that are used to connect the ends without any soldering. The male wires have the ends that can be connected to other ends and the female wires are the ones that have the receiving end.

4.Resistor:

The resistor prevents the damage of the sensor as well as the Arduino board by preventing the excessive flow of current at any situation. The value of the resistor used in this interfacing circuit is 100k.

Circuit:


In this section, let's see the connections made to the interface of the LDR sensor to the Arduino board.


What goes where?

  • At first, one terminal of the LDR sensor is connected to the 5V pin of the Arduino board. Another end of the sensor is connected to the analog pin A0 of the Arduino.

  • The same end which is connected to the analog pin A0 is connected to the resistor of value 100k ohm.

  • The other terminal of the resistor is connected to the ground (GND) pin of the Arduino board. All these connections between the pins are carried out with the help of jumper wires.

After completing the connections, the Arduino board is connected to a computer and the device gets boots up.


Code:


In the first line of code, the LDR #terminal is assigned to the analog pin A0 of the Arduino. And then, a variable is being declared to store the output values.

int ldr=A0; //LDR to arduino pin A0
int output=0; //variable to store the output of the sensor

Inside the void setup, the default baud rate of about 9600 is given. As the function of the LDR sensor should be performed repeatedly, a loop is assigned for the operation. Inside the loop, analog values are read from the sensor and get stored in the output variable. Serial.println function prints the output value. All these operations are done with a delay of about 0.1 seconds.

int ldr=A0; //LDR to arduino pin A0
int output=0; //variable to store the output of the sensor

void setup(){
Serial.begin(9600);
}

void loop(){
output=analogRead(ldr); //read the value from the sensor
Serial.println(output); //prints the values
delay(100);//delay of 100 msec
}

Download the code here,

code
.txt
TXT • 290B




NOTE: The code file is the Arduino IDE file and should be changed into .ino file after downloading the code.


Working:

After connecting the Arduino to the computer, the device gets on and the code is compiled in our computer system. Based on our room light, the values are stored in the output variable. By placing a finger over the LDR sensor, the intensity of the light gets decreased followed by a reduction in the resistance value. For increasing the intensity of the light, a mobile flashlight can be placed over the sensor. Then the value of the resistance increases.

By opening the serial monitor in your computer, we can able to see the change in resistance values (0- 1024) recorded by the LDR #sensor depending on the intensity of the light.


The setup in OFF condition.


Practical implementation of this interfacing is also available as a video.

Video Credits: Abhishek Goud.


This interfacing can also be achieved through various platforms but the Arduino has way more controlling options such as mobile SMS, Internet, Bluetooth. The cost of the Arduino board is also reliable so that everyone could purchase and make use of it.


See also:

  1. Article on interfacing a buzzer with Arduino

  2. Smart lighting system- using LEDs, LDR and raspberry pi

  3. Article on LCD interfacing with raspberry pi



Learn electronics from Learn Electronics!

45 views